keyboard hookup stopped working after reinstalling Windows

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the following code that I used to create the hookup under Win7 x64.
After I reistalled Windows and Visual C++ and recompiled everything, the hookup is not working anymore.
Does anyone knows what might be the reason for that?
Thanks!
DLL:
<pre class="prettyprint" style=" #include <windows.h>

#pragma data_seg(".SHARDAT")
static HHOOK g_hkb=NULL;
static HWND g_notifyWnd = NULL;
UINT uKeypressID = ::RegisterWindowMessage("WM_GLOBAL_KEYPRESS");
#pragma data_seg()
#pragma comment(linker, "-section:.SHARDAT,rws")

HINSTANCE g_hInst = NULL;

LRESULT __declspec(dllexport)__stdcall CALLBACK KeyboardProc(
int nCode,
WPARAM wParam,
LPARAM lParam)
{
if(nCode == HC_ACTION && g_notifyWnd)
{

SendMessage(g_notifyWnd,uKeypressID,wParam,lParam);
}
return CallNextHookEx( g_hkb, nCode, wParam, lParam );
}

extern "C" BOOL __declspec(dllexport) installhook(HWND notifyWnd)
{
if(!g_hInst) return FALSE;

g_notifyWnd = notifyWnd;
g_hkb = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,g_hInst,0);
return (BOOL)g_hkb;
}

extern "C" BOOL __declspec(dllexport) unhook()
{
return UnhookWindowsHookEx(g_hkb);
}


BOOL APIENTRY DllMain( HINSTANCE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
if(ul_reason_for_call == DLL_PROCESS_ATTACH)
{
g_hInst = hModule;
}
return TRUE;
}[/code]
Main file:
<pre class="prettyprint" style=" HMODULE g_hMod = NULL;
const UINT uKeypressID = RegisterWindowMessage(_T("WM_GLOBAL_KEYPRESS"));
BOOL LoadHook();

// ...

void loadHookCall() {
if(!LoadHook()) {
// error
} else {
MessageBox(hWnd, _T("OK"), _T("OK"), 0);
}
}

BOOL LoadHook()
{
if(g_hMod != NULL) return FALSE;

typedef BOOL (*FUNCTION_TYPE)(HWND);
FUNCTION_TYPE installhook;

g_hMod = LoadLibrary("hookup.dll");
if(!g_hMod)
return FALSE;

installhook = (FUNCTION_TYPE)GetProcAddress(g_hMod,"installhook");

if(!installhook) return FALSE;

return installhook(hWnd);
}

// ...

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{

if(message == uKeypressID) {
MessageBox(hWnd, _T("Test"), _T("Test"), 0);
return 0;
} else {

// ...[/code]
<br/>
Everything compiles w/o errors and I get "OK" message from loadHookCall() but never get the "Test" one after any key pressed.<br/>

The only thing that has changed is its on another PC with the same Win7 x64 and Visual C++2010 express
<br/>

<br/>
<br/>
<br/>

View the full article
 
Back
Top