HINSTANCE g_hInstance;
HWND g_hdlg;
HHOOK hHook;
INT_PTR CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam); //对话框函数
LRESULT CALLBACK JournalRecordProc(int code, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
g_hInstance = hInstance;
DialogBox(hInstance, (LPCTSTR)DLG_MAIN, NULL, (DLGPROC)DlgProc); //创建并显示对话框
return 0;
}
//处理对话框消息
INT_PTR CALLBACK DlgProc(HWND hdlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
g_hdlg = hdlg;
if(hHook = SetWindowsHookEx(WH_JOURNALRECORD, JournalRecordProc, (HINSTANCE)g_hInstance, NULL)) //安装钩子
{
MessageBox(0, "安装成功", "", MB_OK);
}
return TRUE;
case WM_CLOSE:
UnhookWindowsHookEx(hHook); //关闭钩子
EndDialog(hdlg, NULL);
return TRUE;
default:
return FALSE;
}
}
//钩子函数
LRESULT CALLBACK JournalRecordProc(int code, WPARAM wParam, LPARAM lParam)
{
if(code == HC_ACTION)
{
if(((PEVENTMSG)lParam)->message == WM_KEYDOWN)
{
MessageBox(g_hdlg, "hello", "", MB_OK); //运行后不停的弹出此对话框直到卡死
}
}
return 1;
}
我感觉是:你这弹出一个对话框,就会安装一次钩子,是不是和MessageBox(g_hdlg,...)g_hdlg这个参数有关,你试试改成0吖。
改成 0 也是一样的结果