Link to home
Start Free TrialLog in
Avatar of devfredde
devfredde

asked on

Problem using SetWindowsHookEx in NT.

I have done this dll:

#include <windows.h>
#include <malloc.h>
#include <string.h>

#pragma comment(linker, "-section:.shared,rws")
#pragma data_seg(".shared")
HHOOK hHook = NULL;
#pragma data_seg()

HINSTANCE hInstance = NULL; // Current DLL instance handle

__declspec(dllexport) LRESULT CALLBACK KeyboardFunc (int nCode, WPARAM wParam, LPARAM lParam );
wParam, LPARAM lParam );

char      strKeyStroke[4];
int            nKeyPress = 0;

BOOL CALLBACK LibMain(HANDLE hInst, DWORD dwReason, LPVOID lpReserved)
{
    UNREFERENCED_PARAMETER( lpReserved);
     
    if ( dwReason == DLL_PROCESS_ATTACH )
    {
        DisableThreadLibraryCalls( (HINSTANCE)hInst );
        hInstance = (HINSTANCE)hInst;  // Save DLL instance handle
    }
    return TRUE; // Success
}

__declspec(dllexport) BOOL SetHook(DWORD dwThreadId)
{
    if (hHook != NULL)
        return FALSE;
     
    hHook = SetWindowsHookEx( WH_KEYBOARD, (HOOKPROC)KeyboardFunc, NULL, NULL );
    return (hHook != NULL);
}

__declspec(dllexport) BOOL UnHook(void)
{
    BOOL rc;
     
    rc = UnhookWindowsHookEx(hHook);
    if (rc)
        hHook = NULL;
     
    return rc;
}

__declspec(dllexport) LRESULT CALLBACK KeyboardFunc(int nCode, WPARAM wParam, LPARAM lParam )
{
      MessageBeep( 0xFFFFFFFF );

      if ( nCode >= 0 )
      {
      wsprintf( strKeyStroke,"%d", wParam );
      MessageBox( NULL, strKeyStroke, "Key", MB_OK );
            }
      }

    return CallNextHookEx(hHook, nCode, wParam, lParam);
}

It would not work in NT. I have not test it on win 95,98.
I call SetHook( NULL ) from the main program and get a true.

But even if SetWindowsHookEx return TRUE I can not see what key is pressed. Please help me.

ASKER CERTIFIED SOLUTION
Avatar of ATucker
ATucker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of devfredde
devfredde

ASKER

It don't work.

I have done something like this before in the main program and have no dll and it works fine in win98. But not in NT.

This program will take over the keyboard of every running program.

Do you know why it doesn't work ?


Avatar of jkr
The reason is simple - change

    hHook = SetWindowsHookEx( WH_KEYBOARD, (HOOKPROC)KeyboardFunc, NULL, NULL );

to read


    hHook = SetWindowsHookEx( WH_KEYBOARD, (HOOKPROC)KeyboardFunc, hInstance , NULL );

You have to provide the DLL instance handle when setting your hook!

(BTW: Also rename 'LibMain()' to 'DllMain()', i don't know whether all compilers ignore the spelling difference...)
I have done that but it will only work on win95 & 98, but when I try at home on my WIndows NT 4.0 SP3 it works but not my computer at the work, running Windows NT 4.0.

How to return the keystroke to the main program.


Well, i'm using NT4 SP3 also, and it works (no idea why it shouldn't work without SP3...)