Sign in to follow this  

Conflict between hooks and dialog boxes?

This topic is 4659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Does anybody know of any conflicts between hooks and dialog boxes? I am writing a Win32 dialog app in C++, using global hooks. I, essentially (this is in WinMain), initialize the hook, DialogBox(...) to run the dialog box, and destroy the hook. Always, as the program ends, I get the error "Failed to release hook," specifying error code 1401 (Invalid Hook Handle). I get no errors along the lines of "Failed to create hook" in the initialization stage. When I comment out the DialogBox(...) macro, however, then I get no errors at all (Although my program just quits in one second). I have tried creating a modeless dialog box, too, and I get the same errors. Does anyone know what might be the matter? Thanks in advance.

Share this post


Link to post
Share on other sites
I hope its not too obfuse -- basically, in the try...catch block, I get the module, set up the hook, ect. Then I create my dialog box, do the message loop. Afterwards, I release all of the stuff. Every time I try this, however, as said before, I get an error when releasing the hook.

try {
g_hModule = LoadLibrary("../TestHook/Debug/TestHook.dll");
if(!g_hModule) throw "Failed to load Module";
g_prHook = (HOOKPROC)GetProcAddress(g_hModule, "?TestHook@@YAHHIJ@Z");
if(!g_prHook) throw "Failed to get hook proc";
g_hHook = SetWindowsHookEx(WH_GETMESSAGE, (g_prHook), g_hModule, 0);
if(!g_hHook) throw "Failed to create hook";
} catch(char *ErrorString) {
char Format[128];
sprintf(Format, "%s, Error %i", ErrorString, GetLastError());
MessageBox(0, Format, "Error", 0);
g_bExit = true;
}
g_hMain = CreateDialog(hInstance,
MAKEINTRESOURCE(IDD_MAIN), 0, TestMain);
ShowWindow(g_hMain, nShowCmd);
MSG Msg;
while(true) {
if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
if(Msg.message == WM_QUIT) { break; }
TranslateMessage(&Msg);
DispatchMessage(&Msg);
} else {
Sleep(1);
}
}
if(g_hHook) {
if(!UnhookWindowsHookEx(g_hHook)) {
char Format[128];
sprintf(Format, "Failed to release hook, Error %i", GetLastError());
MessageBox(0, Format, "Error", 0);
}
}
if(g_hModule) {
if(!FreeLibrary(g_hModule)) {
char Format[128];
sprintf(Format, "Failed to free module, Error %i", GetLastError());
MessageBox(0, Format, "Error", 0);
}
}


Share this post


Link to post
Share on other sites

This topic is 4659 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this