SetWindowsHookEx(WH_JOURNALRECORD, ..) 有时会挂起系统 [英] SetWindowsHookEx(WH_JOURNALRECORD, ..) sometimes hang the system
问题描述
我正在尝试升级旧应用程序以在 Windows 7 上运行,但我在使用 Journal Hooks 的宏录制"功能方面遇到了问题.我遵循了在 Windows 7 上进行这项工作所需的每一个步骤,即设置 uiAccess=true、签署 exe 并从 Program Files 目录运行它.
I'm trying upgrade an old application to work on Windows 7 and I'm having problems with the "Macro recording" functionality, which is using Journal Hooks. I have followed every step necessary to make this work on Windows 7, that is, setting the uiAccess=true, signing the exe and running it from the Program Files directory.
它通常有效,但有时,没有明显的原因,似乎 SetWindowsHookEx 函数正在等待某些东西并以一种奇怪的方式挂起整个系统:没有输入发送到任何应用程序.我可以摆脱这种挂起的唯一方法是执行 ctrl-alt-del,这会强制卸载挂钩.
It usually works, but sometimes, for no apparent reason, it seems like the SetWindowsHookEx function is waiting for something and hang the whole system in a weird way : no input is send to any application. The only way I can get out of this hang is by doing ctrl-alt-del, which force uninstall the hook.
我用一个简单的应用程序复制了这个问题.使用 Visual Studio 默认生成的 Win32 模板,我修改了关于对话框回调以注册和取消注册钩子:
I've replicated the problem with a simple application. Using the default generated Win32 template of Visual Studio, I've modified the About dialog callback to register and unregister the hook :
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
{
// added this
recordHook = ::SetWindowsHookEx(WH_JOURNALRECORD, JournalRecordCallback, hInst, 0);
return (INT_PTR)TRUE;
}
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
// and this
UnhookWindowsHookEx(recordHook);
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
我的钩子回调函数很简单
My hook callback function is simply
LRESULT CALLBACK JournalRecordCallback(int nCode, WPARAM wParam, LPARAM lParam)
{
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
而且,对于较大的应用程序,有时 SetWindowsHookEx 调用会挂起.
And, as with the larger application, sometimes, the SetWindowsHookEx call hang.
有人遇到过这种情况吗?我想可能是在 MessageLoop 内安装钩子导致挂起,我试图将它移到另一个线程,但仍然挂起.
Does anybody has experienced this? I thought that maybe installing the hook inside the MessageLoop was causing the hang and I tried to move it to another thread but still got the hang.
我做错了吗?谢谢
推荐答案
安装了 JournalRecordProc 挂钩过程的应用程序应注意 VK_CANCEL 虚拟键代码(在大多数键盘上实现为 CTRL+BREAK 组合键).应用程序应将此虚拟键码解释为用户希望停止日志记录的信号.应用程序应通过结束记录序列并移除 JournalRecordProc 钩子过程来做出响应.去除很重要.它通过挂在钩子过程中来防止日志应用程序锁定系统.
An application that has installed a JournalRecordProc hook procedure should watch for the VK_CANCEL virtual key code (which is implemented as the CTRL+BREAK key combination on most keyboards). This virtual key code should be interpreted by the application as a signal that the user wishes to stop journal recording. The application should respond by ending the recording sequence and removing the JournalRecordProc hook procedure. Removal is important. It prevents a journaling application from locking up the system by hanging inside a hook procedure.
这篇关于SetWindowsHookEx(WH_JOURNALRECORD, ..) 有时会挂起系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!