Solved

Problem using SetWindowsHookEx in NT.

Posted on 1999-01-07
5
254 Views
Last Modified: 2013-12-03
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.

0
Comment
Question by:devfredde
  • 2
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
ATucker earned 100 total points
ID: 1418300
You have to change the name of your entry point to DllMain (unless you specify LibMain on the linker command line) and use your hInstance variable as the next to last parameter in SetWindowsHookEx.  
0
 

Author Comment

by:devfredde
ID: 1418301
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 ?


0
 
LVL 86

Expert Comment

by:jkr
ID: 1418302
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...)
0
 

Author Comment

by:devfredde
ID: 1418303
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.


0
 
LVL 86

Expert Comment

by:jkr
ID: 1418304
Well, i'm using NT4 SP3 also, and it works (no idea why it shouldn't work without SP3...)
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

775 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question