I was trying to wirte a small app to test hooks based on Icz's example, here are the dll procedures
proc __DllMainCRTStartup@12,hIst,reason,reserved1

enter
mov edx,[hIst]
mov eax,1
mov [gIst],edx
return

proc _KeyProc@12,nCode,wParam,lParam
enter
invoke CallNextHookEx,[gKey],[nCode],[wParam],[lParam]
invoke PostMessage,[gWnd],WM_HOOK,[wParam],[lParam]

xor eax,eax
return

proc _InstallHook@4,hWnd
enter
invoke SetWindowsHookEx,WH_KEYBOARD,_KeyProc@12,[gIst],0
mov edx,[hWnd]
mov [gKey],eax
mov [gWnd],edx
return

proc _UninstallHook@0
enter
invoke UnhookWindowsHookEx,[gKey]
return


The WM_HOOK message get post just fine if my Window has focus, but they don't get posted at all if the window is in the background. Worse again, sometimes cpu usage goes up to 100% and no keys get set to other apps until I close mine, then they all arrive.

I'm just wondering have I done something silly and wrong above, or do Hooks perhaps work differently under WinXP?

I notice too, something Icz's example doesn't get sent its HOOK message if the window doesnt have focus.
Posted on 2002-08-31 08:13:03 by Eóin
The WM_HOOK message get post just fine if my Window has focus, but they don't get posted at all if the window is in the background.


Every Dll has its own data section.
So, your hWnd is not shareable between instances of your dll.
When you call _InstallHook from your process, hWnd is valid.
But when hooked event occurs in other process, hWnd is not initialized,
because this dll instance has its own brand new data section.
And
invoke PostMessage,,WM_HOOK,,
actually looks like this:
invoke PostMessage,0,WM_HOOK,,

You have to put hWnd in shareable section:
shared SEGMENT
hWnd dd ?
shared ENDS

And tell linker make it readable/writable/shareable:
/SECTION:shared,RWS

It should work.
Posted on 2002-09-01 04:36:39 by Four-F
That worked perfectly, thank you very much Four-F.
Posted on 2002-09-01 05:24:28 by Eóin
Ya thanks Four-F, nice work indeed
Posted on 2004-10-18 00:05:58 by nofrillz
Four-F is one of the King of the Windows System Programming, especially in DDK.

You know that?
So when he answer you about system hook, it will work correctly.

Try to use search engine with user name: Four-F or keyword DDK, hooking system,...

You will increase your knowledge very much.

The world is so big...


LOL :lol:
Posted on 2004-10-20 22:26:25 by nhnpresario