Blog Home  Home Feed your aggregator (RSS 2.0)  
Eric Malamisura's Blog - Sunday, January 29, 2006
Geek Ramblings
 
 Sunday, January 29, 2006

Have you ever tried to install a system hook?  It seems its much harder than what it should be, but the real issue is the sheer lack of documentation on how to do it the right way.  I found a couple examples in forums and various sites which none of them worked, and in some instances they actually had the Proc code in the Hook DLL itself.  Well thats just not right, it should be the Hook DLL captures the message, sends it to your EXE and then passes it onto the next hook on the hook chain.

For some reason I cannot for the life of me get this to work, granted ive only spent about 5 hours working on it so eventually I believe I will master it.  But to show you exactly what I am talking about I have compiled the following rather cryptic code below (note this code is pure prototype code): 

main.cpp - The main code for the HookDLL.dll


#
include <windows.h>

#ifdef HOOKDLL_EXPORTS
#define HOOKDLL_API __declspec(dllexport)
#else
#define HOOKDLL_API __declspec(dllimport)
#endif

#define WM_HOOK WM_USER +100

// Shared data segment
#pragma data_seg ("Shared")
HHOOK g_hHook = NULL;
HWND g_hWndNotify = NULL;
#pragma data_seg ()

// KeyboardProc()
HOOKDLL_API LRESULT CALLBACK KeyboardProc( int nCode, WPARAM wParam, LPARAM lParam )

   if (nCode == HC_ACTION && !(lParam & 0x80000000)) 
   { 
         { 
            PostMessage (g_hWndNotify, WM_HOOK, wParam, lParam); 
            return 0; 
         } 
   } 
   return nCode < 0 ? CallNextHookEx(g_hHook, nCode, wParam, lParam) : 0;
}

// Function used to share variables
HOOKDLL_API int SetHookDLL( HHOOK hLocalHook, HWND hLocalWnd)

   g_hHook = hLocalHook; 
   g_hWndNotify = hLocalWnd; 
   return 1;
}

hookdll.def - The definition file.


LIBRARY "HookDLL"
EXPORTS
KeyboardProc
SetHookDLL
SECTIONS
Shared READ WRITE SHARED

So thats the end of the HookDLL.dll code, next is the main application which currently is setup only for testing.  This is the part that doesnt work and its most likely something stupid that I have done on my part.

 

The main program, EXE for testing the HookDLL.dll


// Main definitions
#include <windows.h>
#include <string>

using namespace std;

#define WM_HOOK WM_USER +100
HHOOK hHook = NULL;
HMODULE hDll;
BOOL InstallHook(HWND hWndNotify);
BOOL UninstallHook(void);
BOOL bHooked;
// For SetHookDLL
typedef VOID (*LOADPROC)(HHOOK hHook, HWND hWndNotify);
HINSTANCE hinst;
HWND hwndMain;


// Install hook (after having done : hDll = LoadLibrary("hookdll.dll" ); )
BOOL InstallHook(HWND hWndNotify)

   HMODULE hDLL = GetModuleHandle("HookDLL");

   LOADPROC fPtrFcnt; 
   hHook = SetWindowsHookEx(WH_KEYBOARD , (HOOKPROC)GetProcAddress(hDLL, "KeyboardProc"), hDLL, 0); 
      if ( hHook == NULL ) 
         return FALSE; 
      else 
         fPtrFcnt = (LOADPROC)GetProcAddress(hDLL,"SetHookDLL"); 
         fPtrFcnt(hHook, hWndNotify); 
         return TRUE;
}

// Unistall hook
BOOL UninstallHook(void
   { 
      if ( hHook ) 
      { 
         if (UnhookWindowsHookEx(hHook)) 
         { 
            hHook = NULL; 
            return TRUE; 
         } 
         return FALSE; 
      } 
   return FALSE;
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)

   MSG msg;
   BOOL bRet; 

   LoadLibrary("HookDLL.dll");
   InstallHook(hwndMain);

   // Show the window and paint its contents. 

   ShowWindow(hwndMain, nCmdShow); 
   UpdateWindow(hwndMain); 

   // Start the message loop. 

   while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
   { 
         TranslateMessage(&msg); 
         DispatchMessage(&msg); 
   } 

   return 0;
}

LRESULT CALLBACK KeyboardProc(int code, WPARAM wParam, LPARAM lParam)
{
   MessageBox(0, "Hook Intercept", "Test", 0);  //should popup a message if a key is pressed but doesnt...
   return CallNextHookEx(hHook, 0, wParam, lParam);
}

So mainly I am trying to figure out what the issue is here, my goal is to have a program with hotkeys.  Thats my intention and its turning out to be rather difficult to accomplish this, I guess I could use .NET to do this since Keyboard hooking is possible, or perhaps I am going about this the wrong way entirely?  I do believe a Hook is needed to capture a keyboard event systemwide unless I am mistaken.

Sunday, January 29, 2006 4:51:47 PM (GMT Standard Time, UTC+00:00)  #    Comments [0]    | 
Copyright © 2008 Eric Malamisura. All rights reserved.
DasBlog 'Portal' theme by Johnny Hughes.