News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Problems monitoring WM_xxx messages

Started by Ar0n, August 23, 2014, 04:11:54 PM

Previous topic - Next topic

Ar0n

Hello guys could anyone tell me the problem with my DLL, unable to get WM_xxx messages:

very simple app:
.586
.model flat,stdcall
option casemap:none

include masm32rt.inc

WinMain PROTO hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD

.data
    ClassName db "MainWinClass",0
    AppName  db "Main Window",0
    hInstance HINSTANCE 0
    clsbutton db "button",0
    sethook db "Set hook",0
    button1 db "button1",0
    button2 db "button2",0
    clsedit db "edit",0
    edit1 db "edit1",0
    edit2 db "edit2",0
    mydll db "dll.dll",0
    exp db "SetMyHook",0
.code


; ---------------------------------------------------------------------------


start:
invoke GetModuleHandle, NULL
mov    hInstance,eax

invoke WinMain, hInstance,NULL,NULL,SW_SHOWDEFAULT
invoke ExitProcess,eax

WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND

mov   wc.cbSize,SIZEOF WNDCLASSEX
mov   wc.style, CS_HREDRAW or CS_VREDRAW
mov   wc.lpfnWndProc, OFFSET WndProc
mov   wc.cbClsExtra,NULL
mov   wc.cbWndExtra,NULL
push  hInstance
pop   wc.hInstance
mov   wc.hbrBackground,COLOR_BTNFACE+1
mov   wc.lpszMenuName,NULL
mov   wc.lpszClassName,OFFSET ClassName

invoke LoadIcon,NULL,IDI_APPLICATION
mov   wc.hIcon,eax
mov   wc.hIconSm,eax

invoke LoadCursor,NULL,IDC_ARROW
mov   wc.hCursor,eax

invoke RegisterClassEx, addr wc
INVOKE CreateWindowEx,NULL,ADDR ClassName,ADDR AppName,\
           WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 200, NULL, NULL, hInstance, NULL
mov   hwnd,eax

invoke ShowWindow, hwnd,SW_SHOWNORMAL
invoke UpdateWindow, hwnd

.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.ENDW

mov     eax,msg.wParam
ret
WinMain endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL ps:PAINTSTRUCT

.IF uMsg==WM_DESTROY
invoke PostQuitMessage,NULL
.ELSEIF uMsg==WM_CREATE
invoke CreateWindowEx,NULL,addr clsbutton,addr sethook,WS_CHILD or WS_VISIBLE,250,100,100,50,hWnd,1,hInstance,NULL
        invoke CreateWindowEx,NULL,addr clsbutton,addr button1,WS_CHILD or WS_VISIBLE,10,10,100,80,hWnd,10,hInstance,NULL
        invoke CreateWindowEx,NULL,addr clsbutton,addr button2,WS_CHILD or WS_VISIBLE,110,10,100,80,hWnd,20,hInstance,NULL
        invoke CreateWindowEx,NULL,addr clsedit,addr edit1,WS_CHILD or WS_VISIBLE or WS_BORDER,210,10,100,80,hWnd,30,hInstance,NULL
        invoke CreateWindowEx,NULL,addr clsedit,addr edit2,WS_CHILD or WS_VISIBLE or WS_BORDER,310,10,100,80,hWnd,40,hInstance,NULL

    .ELSEIF uMsg==WM_COMMAND
        mov ax,word ptr wParam
        .if ax == 1
            invoke LoadLibrary,addr mydll
            .if eax != 0
                ; get address of the exported function
                invoke GetProcAddress,eax,addr exp
                .if eax != 0
                    call eax    ; set hook
                .endif
            .endif
        .endif
.ELSE
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.ENDIF

xor eax,eax
ret
WndProc endp
end start


DLL:
.386
.model flat,stdcall
include masm32rt.inc

CallWndProc PROTO nCode:UINT, wParam:WPARAM , lParam:LPARAM


.data
hInstance HINSTANCE 0
hWndProcHook dd 0
sWM_CREATE db "WM_CREATE",0
sWM_COMMAND db "WM_COMMAND",0
sWM_CHAR db "WM_CHAR",0
sWM_KEYDOWN db "WM_KEYDOWN",0
sWM_MOUSEMOVE db "WM_MOUSEMOVE",0

.code

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

SetMyHook proc
    invoke SetWindowsHookEx,WH_CALLWNDPROC,CallWndProc,hInstance,0
    mov hWndProcHook,eax
    xor eax,eax
    ret
SetMyHook endp

CallWndProc proc nCode:UINT, wParam:WPARAM , lParam:LPARAM
   
    .if nCode == HC_ACTION
        mov edi,lParam
        ; for some reason assume is not working here, i don't why
        .if dword ptr [edi+8] == WM_CREATE
            invoke OutputDebugString,addr sWM_CREATE
        .elseif dword ptr [edi+8] == WM_COMMAND
            invoke OutputDebugString,addr sWM_COMMAND
        .elseif dword ptr [edi+8] == WM_CHAR
            invoke OutputDebugString,addr sWM_CHAR
        .elseif dword ptr [edi+8] == WM_KEYDOWN
            invoke OutputDebugString,addr sWM_KEYDOWN
        .elseif dword ptr [edi+8] == WM_MOUSEMOVE
            invoke OutputDebugString,addr sWM_MOUSEMOVE
        .endif
       
    .endif
    invoke CallNextHookEx,hWndProcHook, nCode, wParam, lParam
    ret

CallWndProc endp


end DllEntry


hutch--

Perhaps if you told us what you are trying to do with a DLL trying to hook system messages, then we may be able to help you.

Ar0n

Quote from: hutch-- on August 23, 2014, 05:02:44 PM
Perhaps if you told us what you are trying to do with a DLL trying to hook system messages, then we may be able to help you.
Trying to capture messages of all controls in the application using a DLL ( Only for an application, which will load the DLL. ).

App code is just a test simulating controls ( The controls messages that I want to capture ). the DLL code is not capturing messages  :icon_confused: