Solved

Problem using SetWindowsHookEx in NT.

Posted on 1999-01-07
5
250 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now