CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 1999
    Posts
    1

    Keyboard Hook only works the first time

    I have a system wide Keyboard Hook located in a DLL that is called by another application. The Hook is set and removed by two functions inside the DLL.

    The first time I set the hook, it works great. If I remove the hook and then restart it (from the calling application) it only works if the calling application has the input focus or until I close the application and restart it.

    I am using Visual C++ 6 on Windows 95.

    If anyone has any advice I would appreciate it.

    Thanks
    Yaakov


    Below is a snipit of the code from the DLL.

    [ccode]
    #pragma data_seg(".THOOKS")
    HHOOK hKBHook = NULL;
    HWND hHookUpWnd = NULL;
    HINSTANCE hInstance = NULL;
    #pragma data_seg()

    BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpReseved)
    {
    hInstance = hinstDll;
    return (TRUE);
    }

    __declspec(dllexport) void HookUp(HWND hWnd)
    {
    if( !hKBHook) {
    MessageBeep(-1);
    hHookUpWnd = hWnd;
    hKBHook = SetWindowsHookEx( WH_KEYBOARD, KeyboardFunc, hInstance, (DWORD) NULL);
    assert( hKBHook != NULL);
    }
    }

    __declspec(dllexport) void UnHookUp()
    {
    if( hKBHook) {
    UnhookWindowsHookEx( hKBHook);
    hKBHook = NULL;
    hHookUpWnd = NULL;
    MessageBeep(-1);
    }
    }


    LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam )
    {
    MessageBeep(-1);
    MessageBeep(-1);
    MessageBeep(-1);
    return 1;

    }

    [ccode]





  2. #2
    Guest

    Re: Keyboard Hook only works the first time

    Hi Yaakov
    Your problem is that your hook procedure KeyBoardfunc must return 0 not 1.
    If you want your code to be corect you must use next source:

    LRESULT CALLBACK KeyboardProc (int nCode, WPARAM wParam, LPARAM lParam )
    {
    MessageBeep(-1);
    MessageBeep(-1);
    MessageBeep(-1);

    return CallNextHookEx(hKBHook, code, wParam, lParam);
    }


    Martin
    e-mail: [email protected]


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured