I have some problems with a simple hook. It's just a rip-off from Iczelions tutorial 24 but still I can't get it to work properly. I have a working dll and a working exe that calls Installhook. The problem is just that my program doesn't get all messages. It seems like when my program is out of focus, it suddenly stops recieving them. The dll however recieves them all. They manage to dissappear on the way to my program :confused:

Here is the code that I am using

The dll:

.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib


hInstance dd 0

hHook dd ?
hWnd dd ?

DllEntry proc hInst:HINSTANCE, reason:DWORD, reserved1:DWORD
push hInst
pop hInstance
mov eax,TRUE
DllEntry Endp

[B]ShellProc proc nCode:DWORD,wParam:DWORD,lParam:DWORD

.if nCode<0
invoke CallNextHookEx,hHook,nCode,wParam,lParam
mov eax,[wParam]
invoke PostMessage,hWnd,WM_SHELLHOOK,eax,0

xor eax,eax
ShellProc endp

InstallHook proc hwnd:DWORD
push hwnd
pop hWnd
invoke SetWindowsHookEx,WH_SHELL,addr ShellProc,hInstance,NULL
mov hHook,eax
InstallHook endp

UninstallHook proc
invoke UnhookWindowsHookEx,hHook
UninstallHook endp

From the exe:

DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM

mov eax,uMsg
invoke InstallHook,hWin

[B].elseif eax==WM_SHELLHOOK
PrintText "Hello"[/B] ; It prints out "Hello" only when it's own window, hWin, is created :(

.elseif eax==WM_CLOSE
invoke UninstallHook
invoke EndDialog,hWin,0
mov eax,FALSE
mov eax,TRUE

DlgProc endp

That's about it...
Posted on 2003-02-26 13:23:44 by Delight
OK, I solved it. The .dll needs to be in a shared section to work properly. I just added "/SECTION:.bss,S" when linking and now it works as expected.
Posted on 2003-02-26 13:47:29 by Delight
well, i liked your thread subject title ;)
Posted on 2003-02-26 14:21:35 by jademtech

well, i liked your thread subject title ;)

I thought the same ;)
Posted on 2003-02-26 14:37:11 by bazik