【发布时间】:2012-02-16 18:41:12
【问题描述】:
我正在使用 C#,并且我已经让程序成功记录了使用 SetWindowsHookEx 和 WH_JOURNALRECORD 的日志消息。
到了该停下来的时候,我的问题就来了。文档显示,如果用户按下 CTRL-ESC 或 CTRL-ALT-DELETE,将发布 WM_CANCELJOURNAL 消息,我可以观看以了解何时停止。我的应用程序未绑定,但我似乎从未收到 WM_CANCELJOURNAL。
我有两个钩子设置。一个做日志记录的钩子和一个检查取消消息的钩子:
IntPtr hinstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);
JournalRecordProcedure = JournalRecordProc;
journalHook = SetWindowsHookEx(WH_JOURNALRECORD, JournalRecordProcedure, hinstance, 0);
GetMessageProcedure = GetMessageProc;
messageHook = SetWindowsHookEx(WH_GETMESSAGE, GetMessageProcedure, hinstance, 0);
------
public static int JournalRecordProc(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode < 0) return CallNextHookEx(journalHook, nCode, wParam, lParam);
EventMsgStruct msg = (EventMsgStruct) Marshal.PtrToStructure(lParam, typeof (EventMsgStruct));
script.Add(msg); //just a quick way to record for now
return CallNextHookEx(journalHook, nCode, wParam, lParam);
}
public static int GetMessageProc(int code, IntPtr wParam, IntPtr lParam)
{
//it comes here but how do I test if it's WM_CANCELJOURNAL ??
//code always seems to be equal to zero.. I must be missing something
return CallNextHookEx(journalHook, code, wParam, lParam);
}
【问题讨论】:
标签: c# windows winapi setwindowshookex