Does anyone knpow how to hook a wm-command of a DialogBox, created with DialogBoxParam?
I tried to use WH_SYSMSGFILTER to hook it, but it doesn't work to good.
Posted on 2001-08-20 03:40:58 by Belkot
To install the hook :

InstallHook proc hwnd:DWORD
push hwnd
pop hWnd
invoke FindWindowEx,0,0, ADDR ClassName, 0
invoke GetWindowThreadProcessId, eax, 0
invoke SetWindowsHookEx,WH_GETMESSAGE,ADDR GetMsgProc,hInstance,eax
mov hHook,eax
ret
InstallHook endp

hook proc :

GetMsgProc proc nCode:DWORD,wParam:DWORD,lParam:DWORD
mov edx,lParam
assume edx:PTR MSG
cmp .message, WM_COMMAND
jne fini
cmp .wParam, WMESSAGE
jne fini
cmp .lParam, LMESSAGE
je ca_va
cmp .lParam, 0
jne fini
ca_va:
mov .message, WM_MOUSEMOVE
mov .wParam, 0
mov .lParam, 0
invoke PostMessage,hWnd,WM_HOOK,0,lParam
assume edx:nothing
fini:
invoke CallNextHookEx,hHook,nCode,wParam,lParam
ret
GetMsgProc endp

This prog hook WM_COMMAND message, and if it is the good msg (i check wParam and lParam, i pass to the prog a fake WM_MOUSEMOVE msg)

hope this can help u

(s)
Posted on 2001-08-20 06:19:16 by (scalp)
i tried your sugestion but it still dosent work, when i run HookLoader.exe and a DialogBox.exe is execute and when i tried to send a message to this dialogbox (moving a mouse over or somthing) all i get is a message from windows saing DalogBox.exe has cause ane error in USER32.DLL IP 0177:bff53c87.

here are the sources:

----DialogBox.Asm----
.386p
.MODEL FLAT,STDCall
OPTION CASEMAP:NONE

INCLUDE \MASM32\INCLUDE\WINDOWS.INC
INCLUDE \MASM32\INCLUDE\USER32.INC
INCLUDE \MASM32\INCLUDE\KERNEL32.INC
INCLUDE \MASM32\INCLUDE\MASM32.INC

INCLUDELIB \MASM32\LIB\USER32.LIB
INCLUDELIB \MASM32\LIB\KERNEL32.LIB
INCLUDELIB \MASM32\LIB\MASM32.LIB

; #########################################################################
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
DlgProc proto :DWORD,:DWORD,:DWORD,:DWORD


; #########################################################################
.DATA
DlgName DB "MyDialog", 0

; #########################################################################
.DATA?

; #########################################################################
;.CONST

; #########################################################################
.CODE

START:
Invoke GetModuleHandle, 0
Invoke DialogBoxParam, Eax, ADDR DlgName, 0, ADDR DlgProc, 0

Invoke ExitProcess, 0


; #########################################################################
; Procedury
; #########################################################################

DlgProc Proc hWnd :DWORD,
uMsg :DWORD,
wParam :DWORD,
lParam :DWORD

; -----------------------------
; Process control messages here
; -----------------------------

.If uMsg == WM_INITDIALOG

.ElseIf uMsg == WM_CLOSE
Invoke EndDialog, hWnd, 0

.Else
Mov Eax, FALSE
Ret

.EndIf

Mov Eax, TRUE
Ret

DlgProc EndP

END START ;Koniec KODU
----DialogBox.Asm----

----HookDLL.ASM----
.486p
.MODEL FLAT,STDCall
OPTION CASEMAP:NONE

INCLUDE \MASM32\INCLUDE\WINDOWS.INC
INCLUDE \MASM32\INCLUDE\USER32.INC
INCLUDE \MASM32\INCLUDE\KERNEL32.INC

INCLUDELIB \MASM32\LIB\USER32.LIB
INCLUDELIB \MASM32\LIB\KERNEL32.LIB

; #########################################################################
DllEntry Proto hInst:DWORD, reason:DWORD, reserved1:DWORD

InstallHook Proto hwnd:DWORD
UninstallHook Proto
GetMsgProc Proto nCode:DWORD, wParam:DWORD, lParam:DWORD

; #########################################################################
WM_REVMSGHOOK = WM_USER + 100

; #########################################################################
.DATA
hInstance DD 0

RevClassName DB "#32770", 0
RevTitleText DB "DialogBox", 0


; #########################################################################
.DATA?
hHook DD ?
hWnd DD ?
RevhWnd DD ?

; #########################################################################
;.CONST

; #########################################################################
.CODE

DllEntry Proc hInst:DWORD, reason:DWORD, reserved1:DWORD
Push hInst
Pop hInstance
Mov Eax, TRUE
Ret

DllEntry Endp

InstallHook Proc hwnd:DWORD
Push hwnd
Pop hWnd

Invoke FindWindow, ADDR RevClassName, ADDR RevTitleText
Mov RevhWnd, Eax
Invoke GetWindowThreadProcessId, RevhWnd, 0
Invoke SetWindowsHookEx, WH_GETMESSAGE, ADDR GetMsgProc, hInstance, Eax
Mov hHook, Eax
Ret

InstallHook EndP

UninstallHook Proc
Invoke UnhookWindowsHookEx, hHook
Ret

UninstallHook EndP

GetMsgProc Proc nCode:DWORD, wParam:DWORD, lParam:DWORD
Mov Edx, lParam
ASSUME Edx:PTR MSG

Mov Ebx, RevhWnd
.If .hwnd == Ebx
.If .message == WM_COMMAND
.If .wParam == 900 || .wParam == 901
Mov .message, WM_MOUSEMOVE
Mov .lParam, 0
Mov .wParam, 0
Invoke PostMessage, hWnd, WM_REVMSGHOOK, 0, 0

.EndIf

.ElseIf .message == WM_CLOSE
Mov .message, WM_MOUSEMOVE
Mov .lParam, 0
Mov .wParam, 0
Invoke PostMessage, hWnd, WM_DESTROY, 0, 0

.EndIf

.EndIf

ASSUME Edx: NOTHING
Invoke CallNextHookEx, hHook, nCode, wParam, lParam
Ret

GetMsgProc EndP

END DllEntry ;Koniec KODU
----HookDLL.ASM----

----HookLoader.ASM----
.486p
.MODEL FLAT,STDCall
OPTION CASEMAP:NONE

INCLUDE \MASM32\INCLUDE\WINDOWS.INC
INCLUDE \MASM32\INCLUDE\USER32.INC
INCLUDE \MASM32\INCLUDE\KERNEL32.INC
INCLUDE \MASM32\INCLUDE\MASM32.INC

INCLUDELIB \MASM32\LIB\USER32.LIB
INCLUDELIB \MASM32\LIB\KERNEL32.LIB
INCLUDELIB \MASM32\LIB\MASM32.LIB
INCLUDELIB HOOKDLL.LIB

; #########################################################################
WinMain proto hInst:DWORD, hPrevInst:DWORD, CmdLine:DWORD, CmdShow:DWORD
WndProc proto hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD

InstallHook Proto hwnd:DWORD
UninstallHook Proto
GetMsgProc Proto nCode:DWORD, wParam:DWORD, lParam:DWORD

; #########################################################################
WM_REVMSGHOOK = WM_USER + 100

; #########################################################################
; Tu zaczynaj? si? nasze DANE
.DATA
ClassName DB "HookLoader_Class", 0
AppName DB "HookLoader",0

AppPathRev1 DB "DialogBox.exe", 0

txt DB "It worked!!!", 0

; #########################################################################
.DATA?
hInstance DD ?
hInstanceRev1 DD ?

CommandLine DD ?

RevhWnd DD ?

; #########################################################################
;.CONST

; #########################################################################
; a tu nasz KOD
.CODE

START:
Invoke GetModuleHandle, 0
Mov hInstance, Eax
Invoke GetCommandLine
Mov CommandLine, Eax
Invoke WinMain, hInstance, 0, Eax, SW_HIDE
Invoke ExitProcess, Eax


; #########################################################################


WinMain Proc hInst:DWORD, hPrevInst:DWORD, CmdLine:DWORD, CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND

Mov wc.cbSize, SIZEOF WNDCLASSEX
Mov wc.style, CS_HREDRAW + CS_VREDRAW
Mov wc.lpfnWndProc, OFFSET WndProc
Mov wc.cbClsExtra, 0
Mov wc.cbWndExtra, 0
Push hInstance
Pop wc.hInstance
Mov wc.hbrBackground, COLOR_WINDOW + 1
Mov wc.lpszMenuName, 0
Mov wc.lpszClassName, OFFSET ClassName
Invoke LoadIcon, NULL, IDI_APPLICATION
Mov wc.hIcon, Eax
Mov wc.hIconSm, Eax
Invoke LoadCursor, 0, IDC_ARROW
Mov wc.hCursor, Eax

Invoke RegisterClassEx, ADDR wc
Invoke CreateWindowEx, 0, ADDR ClassName, ADDR AppName,\
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInst, 0
Mov hwnd, Eax

Invoke ShowWindow, hwnd, CmdShow
Invoke UpdateWindow, hwnd

.While TRUE
Invoke GetMessage, ADDR msg, 0, 0, 0
.Break .If (!Eax)
Invoke TranslateMessage, ADDR msg
Invoke DispatchMessage, ADDR msg
.EndW
Mov Eax, msg.wParam
Ret

WinMain EndP

WndProc Proc hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD

.If uMsg == WM_DESTROY
Invoke UninstallHook
Invoke PostQuitMessage, 0

.ElseIf uMsg == WM_CREATE
Invoke WinExec, ADDR AppPathRev1, SW_SHOW
Invoke InstallHook, hWnd

.ElseIf uMsg == WM_REVMSGHOOK
Invoke MessageBox, 0, ADDR txt, ADDR txt, 0

.Else
Invoke DefWindowProc,hWnd,uMsg,wParam,lParam
Ret

.EndIf
Xor Eax, Eax
Ret

WndProc EndP

END START ;Koniec KODU
----HookLoader.ASM----

Maybe u'll find some bug in it or somthing
Posted on 2001-08-20 19:07:22 by Belkot
I think that the problem is in the class name.
With a dialog box, the class name change every time you start it,
so the class you search ("#32770") is probably wrong !
Search only for the title, it should work.

(s)
Posted on 2001-08-21 04:07:41 by (scalp)
i think you R wrong, run exemple from Iczelion's Tutorial 24: Windows Hooks, and when U push Hook button u can get a class name of window on which pointer of a mouse is.:grin:
i checked few dialogboexes (i.e. Start/Run...) and the class name is ever time "#32770" :alright:
Posted on 2001-08-21 05:32:53 by NEMO
For all dialog boxes, even after reboot ?
Damn, i didn't know that !
Thanx

(s)
Posted on 2001-08-21 05:59:54 by (scalp)
all dialogboxes that are created with DialogBoxParam function
Posted on 2001-08-21 07:19:47 by NEMO