• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

problems with hooks in windows 98

Hi, I've trying to make a keyboard hook work and I thought I had succeded (because it worked fine in Win 2000) but when I tested the program in Windows 98 I realized I hadn't.

The executable just calls the DLL with LoadLibrary and the DLL should do all the job. The problem in Windows 98 is that when i run the program and I press any key (so the callback function of the SetWindowsHookEx is run by Windows) i get a Windows serious message about a memory problem, and then I have to reboot the system because everything gets slow (even thought I have closed the program!!).
I did the same thing not using a DLL, just putting all the code in an executable and it works fine but it doesn't get all the keystrokes, that's why I need to do it with a DLL.
I hope you can help me, give a clue or something.
Here it is the skeleton. With only this, the program crashs when I touch a key:

#include <windows.h>
#include <fstream.h>
#include <string.h>
LRESULT CALLBACK callb(int nCode, WORD wParam, DWORD lParam );

HHOOK hook;
static HINSTANCE hDLLInstance;

int juega()

       hook = SetWindowsHookEx(WH_KEYBOARD,
       if (hook == NULL) MessageBox(NULL,"Hook = NULL","Error",MB_OK);

return 0;

LRESULT CALLBACK callb(int nCode, WORD wParam, DWORD lParam )

     LRESULT NextHook = CallNextHookEx( hook, nCode, wParam, lParam );


return NextHook;


                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved

    switch (ul_reason_for_call)
          case DLL_PROCESS_ATTACH:
        hDLLInstance = hInstance;

          case DLL_THREAD_ATTACH:
          case DLL_THREAD_DETACH:
          case DLL_PROCESS_DETACH:
return TRUE;

  • 3
  • 2
1 Solution
i opened my keyboard hook dll i wrote awhile ago and just compared, only differences i notice are that you call your starthook form the DLLMain, i call my StartHook (your juega()) function from the application. its easier for me cause im not using loadlibrary, i export the functions and import the lib in the application, i can call it as normal like it was in a header.
i dont see UnhookWindowsHookEx, you might have just forgot to post it, if not then make sure you can Unhook the hook. if you dont your going to cause problems. that obviouslly wouldn't cause the problem you're having though.
your setting CallNextHook....(...) to an LRESULT and then returning it, then sleeping?
the sleep i dont understand,, the set then return is ok but why not just use:
return CallNextHookEx(.....);
same thing, no need for that sleep.
basically code looks fine except for one thing which i think should be a problem on any 32 bit windows.
your passing in data in the callback as
int ncode is right
even DWORD on lparam is right , though don't use DWORD use LPARAM lParam
problem is on WPARAM a WPARAM isn't a word anymore, in 16 bit windows is was 16bits, but in 32bit windows it was widened to 32bits, so change that to WPARAM wParam
when windows updates itll change the WPARAM to be what its supposed to be thats why its no good to say its a DWORD or WORD for a type even if the size matches.
bolludobarbaroAuthor Commented:
Thank you very much for your comments, PlanetCpp. I fixed the code with your suggestions, but it still doesn't work.
But may be the problem is in the first thing you say:
that i am calling the StartHook from the DLL, and you from your application. In the MSDN library they advise to do it in your way, but since I am a newbie in programming I don't know how to do it.
I tried it in this way but it didn't work:

In the DLL I added:


just before LRESULT CALLBACK callb(int nCode, WPARAM wParam, LPARAM lParam )

In the excutable I wrote this:

HHOOK hook;
HOOKPROC getproc;
BOOL pio=0;

HINSTANCE hinstDLL = LoadLibrary((LPCTSTR) "c:\\mydll.dll");

getproc = (HOOKPROC)GetProcAddress(hinstDLL, "callb");
       hook = SetWindowsHookEx(WH_KEYBOARD,
pio = FreeLibrary(hinstDLL);

But it will never work, because the GetProcAddress retrieves NULL.
Where is the mistake?

oh ok, to export i would make a header for neatness sake, and include this header in your app, or write up top.. doesn't matter
what you can do is something like
#define EXPORT _declspec(dllexport)
EXPORT int myfunction(int,long);
EXPORT void function2();

like that, this will make the compiler create a lib file
along with the dll. now when i said i called it from outside i didnt mean the SetWindowsHookEx was outside, i've done that but more often i keep a function in the dll called StartHook() and one called EndHook()
those have the hook code and those are called from the exe.
but once you export a function you can import it into your exe and use it as normal.
so lets say you exported the int myfunction(int,long);
make a header in your exe apps project, in it you import
#define IMPORT _declspec(dllimport)
IMPORT int myfunction(int,long);
now include that into your exe and under it link to the lib
---in exe cpp
#include "import.h" //the one you made right above
#pragma comment(lib,"mydll.lib")
the mydll.lib is the lib file the dll project makes (look in debug folder, or release folder whichever is current setting)
take that copy and paste it into the applications folder (not the debug/release but where the cpp files are)
take the dll that was created and copy it into the debug/release folder of your app
now you can use myfunction as if it werent in the dll at all. do not loadlibrary or getprocaddress, just use it as normal.
so you can export the callback, or do what i usually do is export two function to start and stop the hook.
also make sure that each time you build the exe you have to copy and overwrite the old one that you linked to in the debug folder of the exe project. the lib file can stay the same as long as you didn't change any export code.
if you forget you'll be using the old dll thinking that the new code is still causing an error
bolludobarbaroAuthor Commented:
Thank you!!

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now