Thread: Keyboard Hook Problem

  1. #1
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318

    Keyboard Hook Problem

    DLL main file:
    Code:
    #define EXPORT_HOOK_FUNCS
    #include "dll.h"
    #pragma data_seg(".hook_data")
    HHOOK g_hKeyboardHook = 0;
    HWND  g_hAppWnd       = 0;
    #pragma data_seg()
    #pragma comment(linker, "/SECTION:.hook_data,RWS")
    HINSTANCE g_hInstance = 0;
    
    BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD,LPVOID){
    	g_hInstance = hinstDLL;
    	return TRUE;
    }
    
    HOOK_DECLSPEC BOOL InstallHook(HWND hAppWnd) {
    	if (g_hKeyboardHook != 0){
    		return FALSE;
    	}
    	g_hAppWnd = hAppWnd;
    	g_hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)HookProc, g_hInstance, 0);
    	if (g_hKeyboardHook == 0) {
    		MessageBox(hAppWnd, "Failed to install hook!","Fatal Error", MB_ICONSTOP);
    		return FALSE;
    	}
    	return TRUE;
    }
    
    HOOK_DECLSPEC void UninstallHook() {
    	UnhookWindowsHookEx(g_hKeyboardHook);
    	g_hAppWnd       = 0;
    	g_hKeyboardHook = 0;
    }
    
    HOOK_DECLSPEC LRESULT CALLBACK HookProc(int nCode, WPARAM wParam,LPARAM lParam){
    	if (nCode >= 0)  {
    		if (!(lParam & 0x80000000)){
    			PostMessage(g_hAppWnd, KEYSTROKE_MSG, wParam, lParam);
    		}
    	}
    	return CallNextHookEx(g_hKeyboardHook, nCode, wParam, lParam);
    }
    DLL header file:
    Code:
    #include <windows.h>
    #define KEYSTROKE_MSG (WM_USER + 123)
    #ifdef EXPORT_HOOK_FUNCS
      #define HOOK_DECLSPEC __declspec(dllexport)
    #else
      #define HOOK_DECLSPEC __declspec(dllimport)
    #endif
    
    HOOK_DECLSPEC BOOL InstallHook(HWND hAppWnd);
    HOOK_DECLSPEC void UninstallHook();
    HOOK_DECLSPEC LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam);
    Program:
    Code:
    #include <windows.h>
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    /*  Make the class name into a global variable  */
    char szClassName[ ] = "WindowsApp";
    #pragma comment(lib, "../hook/libProject1.a")
    #include "../hook/dll.h"
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nFunsterStil)
    
    {
        HWND hwnd;               /* This is the handle for our window */
        MSG messages;            /* Here messages to the application are saved */
        WNDCLASSEX wincl;        /* Data structure for the windowclass */
    
        /* The Window structure */
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
        wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        /* Use default icon and mouse-pointer */
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;                 /* No menu */
        wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
        wincl.cbWndExtra = 0;                      /* structure or the window instance */
        /* Use Windows's default color as the background of the window */
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        /* Register the window class, and if it fails quit the program */
        if (!RegisterClassEx (&wincl))
            return 0;
    
        /* The class is registered, let's create the program*/
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Windows App",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               544,                 /* The programs width */
               375,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        /* Make the window visible on the screen */
        ShowWindow (hwnd, nFunsterStil);
    	InstallHook(hwnd);
        /* Run the message loop. It will run until GetMessage() returns 0 */
        while (GetMessage (&messages, NULL, 0, 0))
        {
            /* Translate virtual-key messages into character messages */
            TranslateMessage(&messages);
            /* Send message to WindowProcedure */
            DispatchMessage(&messages);
        }
    
        /* The program return-value is 0 - The value that PostQuitMessage() gave */
        return messages.wParam;
    }
    
    
    /*  This function is called by the Windows function DispatchMessage()  */
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)                  /* handle the messages */
        {
            case WM_DESTROY:
                PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
                break;
            default:                      /* for messages that we don't deal with */
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
    
        return 0;
    }
    It can't install the hook. SetWindowsHookEx fails.

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I don't notice anything wrong with this code. Help?

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    GetLastError.

    Please.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    1428 ERROR_HOOK_NEEDS_HMOD
    Cannot set nonlocal hook without a module handle.

    What does it mean by that?
    Last edited by maxorator; 09-08-2006 at 08:49 AM.

  5. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Ok, cracked the nut. Somehow g_hInstance wasn't set to the correct handle and I just used GetModuleHandle(NULL) instead of that. Thanks Tonto, that was very good advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Do I need to use Keyboard Hooks to solve my problem?
    By BenPage in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2005, 10:10 AM
  3. Global hook only works on one window?
    By CornedBee in forum Windows Programming
    Replies: 5
    Last Post: 01-19-2004, 12:25 PM
  4. How to keep static data in a keyboard hook?
    By junbin in forum Windows Programming
    Replies: 1
    Last Post: 01-19-2003, 03:24 AM
  5. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM