Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Keyboard Hook in MFC -- Minor Syntax

Keyboard Hook in MFC -- Minor Syntax

Scheduled Pinned Locked Moved C / C++ / MFC
c++regexhelptutorialquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    c121hains
    wrote on last edited by
    #1

    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:

    D A 2 Replies Last reply
    0
    • C c121hains

      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:

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      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

      C 1 Reply Last reply
      0
      • D David Crow

        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

        C Offline
        C Offline
        c121hains
        wrote on last edited by
        #3

        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?

        D 1 Reply Last reply
        0
        • C c121hains

          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?

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          "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

          K 1 Reply Last reply
          0
          • D David Crow

            "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

            K Offline
            K Offline
            Kuniva
            wrote on last edited by
            #5

            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 --------------------------------------------

            D 1 Reply Last reply
            0
            • C c121hains

              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:

              A Offline
              A Offline
              Anonymous
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • K Kuniva

                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 --------------------------------------------

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups