Solved

Problem using SetWindowsHookEx in NT.

Posted on 1999-01-07
5
264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

If you have ever found yourself doing a repetitive action with the mouse and keyboard, and if you have even a little programming experience, there is a good chance that you can use a text editor to whip together a sort of macro to automate the proce…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

696 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