截至目前,我尝试过SetWindowsHookEx
,但似乎无法从控制台窗口捕获消息。
说明这一点的代码:
#include <iostream>
#define _WIN32_WINNT 0x501
#include <windows.h>
using namespace std;
HHOOK hHook = 0;
LRESULT CALLBACK Callback(int code,WPARAM wParam,LPARAM lParam) {
cout << "tick: " << GetTickCount() << ", code: " << code << ", wParam: "
<< wParam << ", lParam: " << lParam << endl;
return CallNextHookEx(hHook,code,wParam,lParam);
}
int main() {
int idHook[14] = { WH_CALLWNDPROC, WH_CALLWNDPROCRET, WH_CBT, WH_DEBUG,
WH_FOREGROUNDIDLE, WH_GETMESSAGE, WH_JOURNALPLAYBACK, WH_JOURNALRECORD,
WH_KEYBOARD, WH_KEYBOARD_LL, WH_MOUSE, WH_MOUSE_LL, WH_SHELL,
WH_SYSMSGFILTER };
for (int i=0 ; i<=14 ; i++) {
hHook = SetWindowsHookEx( idHook[i] , (HOOKPROC)Callback,
NULL, GetCurrentThreadId());
}
system("dir");
//here appeared nothing insead `dir` output, but why ?
system("pause");
MessageBox(NULL, "Now creating window ...", "... and messages appear.", 0);
}
我查看的另一种方式是控制台本身的 api,特别是ReadConsoleInput()
函数。但它不能通过回调工作,因此它仅可用于捕获键或鼠标事件。此处的示例代码:http: //msdn.microsoft.com/en-us/library/windows/desktop/ms685035%28v=vs.85%29.aspx
但我需要的是掌握由通过system
or创建的进程发出的控制台调用CreateProcess
。通过调用,我的意思是例如在printf()
子进程到达我的进程拥有的控制台之前捕获每个发出的子进程。
这有可能吗?