Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem using SetWindowsHookEx in NT.

Posted on 1999-01-07
5
Medium Priority
?
278 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 300 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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