Sign in to follow this  

Global Windows Hook uninstalls automatically?

This topic is 4300 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"

HINSTANCE hInst = NULL;
HHOOK hHook = NULL;
typedef LRESULT (CALLBACK* CALLBACKHOOKPROC)(HHOOK,int code, WPARAM wParam, LPARAM lParam);
CALLBACKHOOKPROC pCallbackProc = NULL;

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);
   if(hHook)
      return true;
   return false;
};
EXPORT bool UninstallHook( void ) {
   if(hHook)
      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)
{
   if(iCode<0)
      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");
pMyKeyHook->Function<bool>("InstallHook",WH_KEYBOARD,(HOOKPROC)KeyHookProc,NULL);

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:
pMyKeyHook->Function<bool>("UninstallHook");
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

This topic is 4300 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this