Jump to content
  • Advertisement
Sign in to follow this  

Global Windows Hook uninstalls automatically?

This topic is 4583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alright, I'm sure you've seen a lot of Posts about Windows Hooks by now, but I've been looking for a solution all over now and I really can't seem to find one - sorry if its actually an obvious Problem. As the whole Project is rather big in all, I'll try to keep the Code-Examples down to a minimum, if someone is willing to help me out but can't seem so because the problem obviously doesn't lie within the lines that I've posted here, then I could email the whole project - as I don't see an attachment feature here. Anyway, I have a DLL that looks like this:
// dll.h
#include <windows.h>
#include <stdio.h>

#define EXPORT extern "C" __declspec (dllexport)

// dll.cpp
#include "main.h"


LRESULT CALLBACK KeyboardProc(int iCode,WPARAM wParam,LPARAM lParam ) {
   return (pCallbackProc)(hHook,iCode,wParam,lParam);
} ;

EXPORT bool InstallHook( int iHookId, CALLBACKHOOKPROC pProc, DWORD dwThreadId ) {
   pCallbackProc = pProc;
   hHook = SetWindowsHookEx(iHookId,KeyboardProc,hInst,dwThreadId);
      return true;
   return false;
EXPORT bool UninstallHook( void ) {
      return UnhookWindowsHookEx(hHook);
   return false;

BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call,LPVOID lpReserved) {
   hInst = (HINSTANCE)hModule;
   return TRUE;

I compile the DLL - and everythings just fine. Now, I do the following from another Program:
//  this here is a Function that will get called out of the DLL, 
//   I'll provide the DLL with a Pointer to this Function later on
static LRESULT CALLBACK KeyHookProc(HHOOK hHook, int iCode, WPARAM wParam, LPARAM lParam)
      return CallNextHookEx(hHook,iCode,wParam,lParam);

   if(iCode == HC_ACTION) { 
      if (!(lParam & 0x40000000)) { 
         return CallNextHookEx(hHook,iCode,wParam,lParam);

   //  somecode... nothing that would disturb the ways of Windows Hooks

   return CallNextHookEx(hHook,iCode,wParam,lParam);

//  ... somewhere within a WM_CREATE Message:
//  CDll is a class that I've written for the purpose of dynamically loading Libraries
//   and calling functions within them - the functions are *definitely* being called,
//   if I can trust the MSVC 8 Debugger that is.
CDll *pMyKeyHook;
pMyKeyHook = new CDll;
pMyKeyHook->LoadLibrary("../Keyhooker - DLL.dll");

What these lines basically do is, they first load the DLL "Keyhooker - DLL.dll", and then call the InstallHook Function with the given params. As soon as the Program finishes I call:
Now, if I start the Program I have the focus on it, during that time everything *works*. The code that is in my KeyHookProc is being executed, all right. But as soon as the program looses the focus, even though it should still work (after all this is supposed to be a global hook) - it just doesn't work. The Hook-Function *in the DLL* isn't being called at all. Even when I go back to the program and focus it, the DLL Hook-Function is no longer being called. Anyone knows a solution? [edit]Replaced code- with source-tags[/edit] Thanks, MHOOO [Edited by - MHOOO on March 6, 2006 10:32:22 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!