Keyboard Hook Lost
I'm trying to create a small app to catch keypresses and enable keyboard shortcuts to frequently used Desktop Resolutions. When using the computer w/tv-out to watch movies its a hassle changing it manually all the time.
Still I have only written the hook functionality, but the problem is that it seem to be lost sometimes. I start the app and it works perfectly, but when I start media player, invoke the hook by pressing a key, and then close WMP again, the hook is lost. I have to reinstall the hook by re-running the application. Anyone else with such problems? Any ideas?
The code:
//Data segment
#pragma data_seg("SHARED")
static HWND ghWndMain = 0;
static HHOOK ghKeyHook = NULL;
#pragma data_seg()
#pragma comment(linker, "/section:SHARED,RWS")
HINSTANCE ghInstance = 0;
HOOKPROC glpfnHookProc = 0;
//Exports
LRESULT EXP_DLL_FUNC CALLBACK ScreenHookProc (int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION ) {
PostMessage(ghWndMain,WM_USER+20,wParam,lParam);
}
return ::CallNextHookEx(ghKeyHook,nCode,wParam,lParam);
}
BOOL EXP_DLL_FUNC InstallHook ( HWND hWnd )
{
BOOL ok = FALSE;
if ( !ghKeyHook ) {
ghWndMain = hWnd;
glpfnHookProc = (HOOKPROC) ScreenHookProc;
ghKeyHook = ::SetWindowsHookEx(WH_KEYBOARD,glpfnHookProc,ghInstance,NULL);
ok = ( ghKeyHook != NULL );
}
return ok;
}
BOOL EXP_DLL_FUNC RemoveHook ()
{
if ( ghKeyHook ) {
if ( ::UnhookWindowsHookEx(ghKeyHook) )
ghKeyHook = NULL;
}
return ghKeyHook == NULL;
}

