Click to See Complete Forum and Search --> : Keyboard Hook (Half)-Broken


Chazwazza
November 30th, 2006, 05:45 PM
I have the following code in a DLL to set a global keyboard hook and it seems to return properly, but it never calls my HookProc.

#include <windows.h>
#include "main.h"

#pragma data_seg(".hookstuff")
HHOOK KeyHook = NULL;
HWND MyHwnd = NULL;
HINSTANCE g_hInstance = NULL;
#pragma data_seg()

#pragma comment(linker, "/section:.hookstuff,rws")

BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}

g_hInstance = hInstance;

return TRUE;
}


MYDLL_API void InstallHook(HWND h)
{
MyHwnd = h;
KeyHook = SetWindowsHookEx(WH_KEYBOARD,HookProc,g_hInstance,0);
if(KeyHook == NULL)
{
MessageBox(MyHwnd,"Unable to install hook","Error!",MB_OK);
}
}

MYDLL_API void RemoveHook()
{
UnhookWindowsHookEx(KeyHook);
}

MYDLL_API LRESULT CALLBACK HookProc(int ncode,WPARAM wparam,LPARAM lparam)
{
if(ncode>=0)
{
if((lparam & 0x80000000) == 0x00000000)//Check whether key was pressed(not released).
{
SendMessage(MyHwnd,WM_USER+755,wparam,lparam);//Send info to app Window.
}
}
return (CallNextHookEx(KeyHook,ncode,wparam,lparam) );//pass control to next hook in the hook chain.
}

Chazwazza
December 3rd, 2006, 02:21 AM
Does anyone have any idea why this isn't working?

rossbob
December 10th, 2006, 01:45 AM
i think its this line here...surprised the compiler didnt pick it up...soz if this is way off the mark since i am new to programming;

KeyHook = SetWindowsHookEx(WH_KEYBOARD,HookProc,g_hInstance,0);

HookProc isnt a correct call to your method..should be HookProc(<VARS>)

is that what its meants to be.

hope this is it.

Marc G
December 11th, 2006, 03:13 PM
i think its this line here...surprised the compiler didnt pick it up...soz if this is way off the mark since i am new to programming;

KeyHook = SetWindowsHookEx(WH_KEYBOARD,HookProc,g_hInstance,0);

HookProc isnt a correct call to your method..should be HookProc(<VARS>)

is that what its meants to be.

hope this is it.
No, what he has is correct.
SetWindowsHookEx requires a pointer to a function, so you pass it HookProc and not HookProc(<VARS>) since that last one would call the function and pass the result of that function to SetWindowsHookEx and that is not what is wanted.

Your hook installer seems find to me.
Are you sure that your if "if((lparam & 0x80000000) == 0x00000000)" is correct? Try by posting a message to your mainwindow right after "if(ncode>=0)"