problems with hooks in windows 98

Posted on 2003-03-03
Medium Priority
Last Modified: 2012-05-04
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;

Question by:bolludobarbaro
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
  • 3
  • 2

Accepted Solution

PlanetCpp earned 300 total points
ID: 8062786
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.

Author Comment

ID: 8063139
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?


Expert Comment

ID: 8063198
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.

Expert Comment

ID: 8063211
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

Author Comment

ID: 8063302
Thank you!!

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

770 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