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:


.386
.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

.const
WM_SHELLHOOK equ WM_USER+6

.data
hInstance dd 0

.data?
hHook dd ?
hWnd dd ?

.code
DllEntry proc hInst:HINSTANCE, reason:DWORD, reserved1:DWORD
.if reason==DLL_PROCESS_ATTACH
push hInst
pop hInstance
.endif
mov eax,TRUE
ret
DllEntry Endp

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

.if nCode<0
invoke CallNextHookEx,hHook,nCode,wParam,lParam
ret
.elseif nCode==HSHELL_WINDOWCREATED
mov eax,[wParam]
invoke PostMessage,hWnd,WM_SHELLHOOK,eax,0
.endif

xor eax,eax
ret
ShellProc endp
[/B]

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

UninstallHook proc
invoke UnhookWindowsHookEx,hHook
ret
UninstallHook endp



From the exe:



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

mov eax,uMsg
.if eax==WM_INITDIALOG
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
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret

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