需求是這樣的:
如圖中XSheel是一個類似于串口或遠程登陸服務器的除錯軟體,視窗不斷接受來自服務器端的資訊,并列印出來。而現在我需要獲取這個輸出框區域的文本內容。(或者僅獲取最后100個字符存到CString str0;中)
使用spy++獲取了相關的內容,發現輸出框區域不是edit控制元件,而是AfxFrameOrView110。
已經獲得句柄記為hCmdWnd1,ID=0x0000E900。并且可以通過句柄向視窗成功發送了文本資訊。
但使用 ::SendMessage(hCmdWnd1,WM_GETTEXT,20,(LPARAM)(LPCTSTR)str0);無法獲取文本內容。
請問各位有什么思路嗎?用管道的話該怎么做呢,用管道獲取過CMD控制臺的資訊,但是還是不知道怎么獲取這個的。



uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
僅供參考:#pragma comment(lib,"user32")
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main() {
SECURITY_ATTRIBUTES sa = {0};
STARTUPINFO si = {0};
PROCESS_INFORMATION pi = {0};
HANDLE hPipeOutputRead = NULL;
HANDLE hPipeOutputWrite = NULL;
HANDLE hPipeInputRead = NULL;
HANDLE hPipeInputWrite = NULL;
BOOL bTest = 0;
DWORD dwNumberOfBytesRead = 0;
DWORD dwNumberOfBytesWrite = 0;
CHAR szMsg[100];
CHAR szBuffer[256];
sa.nLength = sizeof(sa);
sa.bInheritHandle = TRUE;
sa.lpSecurityDescriptor = NULL;
// Create pipe for standard output redirection.
CreatePipe(&hPipeOutputRead, // read handle
&hPipeOutputWrite, // write handle
&sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);
// Create pipe for standard input redirection.
CreatePipe(&hPipeInputRead, // read handle
&hPipeInputWrite, // write handle
&sa, // security attributes
0 // number of bytes reserved for pipe - 0 default
);
// Make child process use hPipeOutputWrite as standard out,
// and make sure it does not show on screen.
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
si.wShowWindow = SW_HIDE;
si.hStdInput = hPipeInputRead;
si.hStdOutput = hPipeOutputWrite;
si.hStdError = hPipeOutputWrite;
CreateProcess (
NULL, "cmd.exe",
NULL, NULL,
TRUE, 0,
NULL, NULL,
&si, &pi);
// Now that handles have been inherited, close it to be safe.
// You don't want to read or write to them accidentally.
CloseHandle(hPipeOutputWrite);
CloseHandle(hPipeInputRead);
// Now test to capture DOS application output by reading
// hPipeOutputRead. Could also write to DOS application
// standard input by writing to hPipeInputWrite.
sprintf(szMsg, "ver\n");
WriteFile(
hPipeInputWrite, // handle of the write end of our pipe
&szMsg, // address of buffer that send data
strlen(szMsg), // number of bytes to write
&dwNumberOfBytesWrite,// address of number of bytes read
NULL // non-overlapped.
);
while(TRUE)
{
bTest=ReadFile(
hPipeOutputRead, // handle of the read end of our pipe
&szBuffer, // address of buffer that receives data
256, // number of bytes to read
&dwNumberOfBytesRead, // address of number of bytes read
NULL // non-overlapped.
);
if (!bTest){
sprintf(szMsg, "Error #%d reading pipe.",GetLastError());
printf("%s",szMsg);
break;
}
// do something with data.
szBuffer[dwNumberOfBytesRead] = 0; // null terminate
printf("%s",szBuffer);
if ('>'==szBuffer[dwNumberOfBytesRead-1]) break;
}
sprintf(szMsg, "chcp\nexit\n");
WriteFile(
hPipeInputWrite, // handle of the write end of our pipe
&szMsg, // address of buffer that send data
strlen(szMsg), // number of bytes to write
&dwNumberOfBytesWrite,// address of number of bytes read
NULL // non-overlapped.
);
while(TRUE)
{
bTest=ReadFile(
hPipeOutputRead, // handle of the read end of our pipe
&szBuffer, // address of buffer that receives data
256, // number of bytes to read
&dwNumberOfBytesRead, // address of number of bytes read
NULL // non-overlapped.
);
if (!bTest){
sprintf(szMsg, "Error #%d reading pipe.",GetLastError());
printf("%s",szMsg);
break;
}
// do something with data.
szBuffer[dwNumberOfBytesRead] = 0; // null terminate
printf("%s",szBuffer);
}
// Wait for CONSPAWN to finish.
WaitForSingleObject (pi.hProcess, INFINITE);
// Close all remaining handles
CloseHandle (pi.hProcess);
CloseHandle (hPipeOutputRead);
CloseHandle (hPipeInputWrite);
return 0;
}
//C:\test>test
//Microsoft Windows [版本 5.2.3790]
//(C) 著作權所有 1985-2003 Microsoft Corp.
//
//C:\test>ver
//
//Microsoft Windows [版本 5.2.3790]
//
//C:\test>chcp
//活動的代碼頁: 936
//
//C:\test>exit
//Error #109 reading pipe.
//C:\test>
uj5u.com熱心網友回復:
我用VS2013執行以下程式,,出現則個情況
uj5u.com熱心網友回復:
崩潰的時候在彈出的對話框按相應按鈕進入除錯,按Alt+7鍵查看Call Stack即“呼叫堆疊”里面從上到下列出的對應從里層到外層的函式呼叫歷史。雙擊某一行可將游標定位到此次呼叫的源代碼或匯編指令處,看不懂時雙擊下一行,直到能看懂為止。轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/95373.html
標籤:進程/線程/DLL
