Keyboard Hook in MFC -- Minor Syntax
-
Every example of using Keyboard hooks with MFC seems to be in a DLL. Is there any reason for this? I'm trying to do it in a separate class so my program will be all together. How do you do this? I've pasted what I have so far but the error i get is: error C2664: 'SetWindowsHookExA' : cannot convert parameter 2 from 'long (int,unsigned int,long)' to 'long (__stdcall *)(int,unsigned int,long)' None of the functions with this name in scope match the target type //Keyboard hook callback LRESULT CALLBACK CKeyboardHook::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { try { KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam; if (nCode >= 0) { if (wParam == WM_KEYDOWN) { BOOL bControlKeyDown = FALSE; // Check to see if the CTRL key is pressed bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1); if ((pkbhs->vkCode == VK_F) && bControlKeyDown) { SendMessage(hWnd,UWM_SETVISIBLE, 0, 0); ShowWindow(hWnd, SW_RESTORE); } } } } catch(...) { } return CallNextHookEx( hkb, nCode, wParam, lParam ); } BOOL CKeyboardHook::InstallHook() { HINSTANCE ppI = AfxGetInstanceHandle(); hkb=SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc,ppI,0); return TRUE; } ---- and in the header: class CKeyboardHook { public: CKeyboardHook(); LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); BOOL InstallHook(); ... :confused:
-
Every example of using Keyboard hooks with MFC seems to be in a DLL. Is there any reason for this? I'm trying to do it in a separate class so my program will be all together. How do you do this? I've pasted what I have so far but the error i get is: error C2664: 'SetWindowsHookExA' : cannot convert parameter 2 from 'long (int,unsigned int,long)' to 'long (__stdcall *)(int,unsigned int,long)' None of the functions with this name in scope match the target type //Keyboard hook callback LRESULT CALLBACK CKeyboardHook::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { try { KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam; if (nCode >= 0) { if (wParam == WM_KEYDOWN) { BOOL bControlKeyDown = FALSE; // Check to see if the CTRL key is pressed bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1); if ((pkbhs->vkCode == VK_F) && bControlKeyDown) { SendMessage(hWnd,UWM_SETVISIBLE, 0, 0); ShowWindow(hWnd, SW_RESTORE); } } } } catch(...) { } return CallNextHookEx( hkb, nCode, wParam, lParam ); } BOOL CKeyboardHook::InstallHook() { HINSTANCE ppI = AfxGetInstanceHandle(); hkb=SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc,ppI,0); return TRUE; } ---- and in the header: class CKeyboardHook { public: CKeyboardHook(); LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); BOOL InstallHook(); ... :confused:
c121hains wrote: Every example of using Keyboard hooks with MFC seems to be in a DLL. Is there any reason for this? Yes. Global hooks are part of a chain. The hook procedure must be exported in a DLL in order for Windows to call it.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
c121hains wrote: Every example of using Keyboard hooks with MFC seems to be in a DLL. Is there any reason for this? Yes. Global hooks are part of a chain. The hook procedure must be exported in a DLL in order for Windows to call it.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
Ohhh i see. There's a setting in visual studio (statically link objects or something) so that i can compile my EXE and DLL into one EXE right? How do i do this?
"Dynamic linking" is the process that Windows uses to link a function call in one module to the actual function in the library module. "Static linking" occurs at compile/link time when you link various object (.OBJ) modules, run-time library (.LIB) files to create a Windows .EXE file. Dynamic linking occurs at run time. With static linking, the linker copies the code for the function into the application's executable file. With dynamic linking, a link is established when the program is loaded into memory or while it is running. So even though you can static link your DLL to your EXE, you still need a DLL that contains the exported hook function.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
-
"Dynamic linking" is the process that Windows uses to link a function call in one module to the actual function in the library module. "Static linking" occurs at compile/link time when you link various object (.OBJ) modules, run-time library (.LIB) files to create a Windows .EXE file. Dynamic linking occurs at run time. With static linking, the linker copies the code for the function into the application's executable file. With dynamic linking, a link is established when the program is loaded into memory or while it is running. So even though you can static link your DLL to your EXE, you still need a DLL that contains the exported hook function.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
That's not true. You can export the function in your EXE just as well, a DLL and EXE are basically the same, they just have different entry points and are treated differently by Windows' PE loader. I tried the exact same thing with the keyboardhook once this guy is trying and it worked fine, in one EXE. The problem is, when I tried it without MFC for some reason it didn't work, but it did work with MFC. The hook function was simply in my EXE's export table, so windows just loaded the same EXE that made the call for the hooking registration in memory (or used the same copy i dunno what it does exactly) and used that function as the hook procedure. Kuniva --------------------------------------------
-
Every example of using Keyboard hooks with MFC seems to be in a DLL. Is there any reason for this? I'm trying to do it in a separate class so my program will be all together. How do you do this? I've pasted what I have so far but the error i get is: error C2664: 'SetWindowsHookExA' : cannot convert parameter 2 from 'long (int,unsigned int,long)' to 'long (__stdcall *)(int,unsigned int,long)' None of the functions with this name in scope match the target type //Keyboard hook callback LRESULT CALLBACK CKeyboardHook::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { try { KBDLLHOOKSTRUCT *pkbhs = (KBDLLHOOKSTRUCT *) lParam; if (nCode >= 0) { if (wParam == WM_KEYDOWN) { BOOL bControlKeyDown = FALSE; // Check to see if the CTRL key is pressed bControlKeyDown = GetAsyncKeyState (VK_CONTROL) >> ((sizeof(SHORT) * 8) - 1); if ((pkbhs->vkCode == VK_F) && bControlKeyDown) { SendMessage(hWnd,UWM_SETVISIBLE, 0, 0); ShowWindow(hWnd, SW_RESTORE); } } } } catch(...) { } return CallNextHookEx( hkb, nCode, wParam, lParam ); } BOOL CKeyboardHook::InstallHook() { HINSTANCE ppI = AfxGetInstanceHandle(); hkb=SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc,ppI,0); return TRUE; } ---- and in the header: class CKeyboardHook { public: CKeyboardHook(); LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); BOOL InstallHook(); ... :confused:
The keyboard hook proc must be either a global function or a static member of your class. Also, hooks are in dll's if they are global hooks (work no matter where teh input focus is). If the hook is in your .exe then it will only catch keyboard events when the exe has the keyboard focus.
-
That's not true. You can export the function in your EXE just as well, a DLL and EXE are basically the same, they just have different entry points and are treated differently by Windows' PE loader. I tried the exact same thing with the keyboardhook once this guy is trying and it worked fine, in one EXE. The problem is, when I tried it without MFC for some reason it didn't work, but it did work with MFC. The hook function was simply in my EXE's export table, so windows just loaded the same EXE that made the call for the hooking registration in memory (or used the same copy i dunno what it does exactly) and used that function as the hook procedure. Kuniva --------------------------------------------
Kuniva wrote: I tried the exact same thing with the keyboardhook once this guy is trying and it worked fine, in one EXE. Did you use a local or a global hook? The former will work in both a DLL and an EXE, while the latter will only work in a DLL.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown