Hi,
I have a simple dll hook problem,will appreciate help.
Idea: appl inserts a menu item to Notepad & choosing this item a DB appears.
WM_CLOSE and any other messages work but WM_COMMAND doesnt.
Thanx in advance.


!!!.asm
--------
include !!!.inc

_InsertMenuItem PROTO
_SetHook        PROTO
_Unhook        PROTO


WinMain proto :DWORD,:DWORD,:DWORD,:DWORD

.DATA                         

AppName db "Our First Window",0
szIconName db "1.ico",0

.DATA?                 
hInstance HINSTANCE ? 
CommandLine LPSTR ?

.CODE               
start:
invoke GetModuleHandle, NULL                             
mov hInstance,eax
invoke GetCommandLine 
         
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, 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_WINDOW+1

    mov  wc.lpszMenuName,OFFSET AppName 
    mov  wc.lpszClassName,OFFSET AppName
    invoke LoadImage,hInstance,addr szIconName,IMAGE_ICON,0,0,\
                            LR_LOADFROMFILE or LR_DEFAULTSIZE
    mov  wc.hIcon,eax
    mov  wc.hIconSm,eax
    invoke LoadCursor,NULL,IDC_ARROW
    mov  wc.hCursor,eax
    invoke RegisterClassEx, addr wc 
    invoke CreateWindowEx,0,\
                ADDR AppName,\
                addr AppName,\
                WS_OVERLAPPEDWINDOW,\
                CW_USEDEFAULT,\
                CW_USEDEFAULT,\
                CW_USEDEFAULT,\
                CW_USEDEFAULT,\
                NULL,\
                NULL,\
                hInst,\
                NULL
    mov  hwnd,eax

    invoke ShowWindow, hwnd,CmdShow
    invoke UpdateWindow, hwnd

    .WHILE TRUE  ; Enter message loop
      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

    .IF uMsg==WM_CREATE 
        INVOKE _InsertMenuItem 
        INVOKE _SetHook 
         
    .ELSEIF uMsg==WM_DESTROY
        INVOKE _Unhook
        invoke PostQuitMessage,NULL
    .ELSE
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam
        ret
    .ENDIF
    xor eax,eax

    ret
WndProc endp

end start

---------------------------
!MyDLL.asm
--------------

;
;---------------------------------------------------------
include !!!.inc

IDM_MINE    EQU  1000h


.data

NotepadWndClassName db  "NotePad",0
MyMenuElement      db  "MyMenuItem",0
lpCallWndProc      BYTE "CallWndProc",0

hNotePadWnd HWND    0
hMenu HMENU        0
hPopupMenu HMENU    0

hDll        DWORD       0
dThreadID  DWORD       0
dCallWndProcAdr    DWORD       0
hHook      DWORD       0


.data?

dSaveID    DWORD ?

.code
start:
;---------------------------------------

DllEntry proc hInstDLL:HINSTANCE, reason:DWORD, reserved1:DWORD

.if reason==DLL_PROCESS_ATTACH

push hInstDLL
pop  hDll

.endif     
 
    mov  eax,TRUE
    ret

DllEntry ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

CallWndProc proc nCode:DWORD,wParam:DWORD,lParam:DWORD

INVOKE CallNextHookEx,hHook,nCode,wParam,lParam 


assume ESI:PTR CWPSTRUCT
MOV ESI,lParam

MOV edx,hNotePadWnd

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; start ;;;;;;;;;;;;;;;;;;

.if .hwnd == edx   
                 
        .if .message == WM_COMMAND                               
                     
                .if word ptr.wParam == IDM_MINE
                    MsgBox "At last!",0
                .endif

        .elseif .message == WM_CLOSE
                    MsgBox "WM_CLOSE",0     

        .endif

.endif 

assume esi:nothing
xor eax,eax   

ret
CallWndProc endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

_InsertMenuItem PROC

invoke FindWindowEx,0,0,addr NotepadWndClassName,0 
mov hNotePadWnd,eax

push hNotePadWnd
call GetMenu         
mov hMenu,eax

invoke GetSubMenu,hMenu,0 
mov hPopupMenu,eax

invoke InsertMenu,hPopupMenu,0FFFFFFFFh,MF_BYPOSITION,IDM_MINE, OFFSET MyMenuElement   
invoke DrawMenuBar,hNotePadWnd       

         
RET
_InsertMenuItem ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

_SetHook PROC

INVOKE GetProcAddress, hDll,ADDR lpCallWndProc
MOV dCallWndProcAdr,EAX

INVOKE GetWindowThreadProcessId,hNotePadWnd,0
MOV dThreadID,EAX

INVOKE SetWindowsHookEx,WH_CALLWNDPROC,dCallWndProcAdr,hDll,dThreadID
MOV hHook,EAX     

 
RET
_SetHook ENDP

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

_Unhook PROC

INVOKE UnhookWindowsHookEx,hHook
         
RET
_Unhook  ENDP


End DllEntry



















































































































Posted on 2010-06-20 01:53:17 by ZapLeston
I "don't do Windows", so I dunno. I read in an article about debugging Linux:


Why doesn't my program work?



Because something you believe to be true is not true.


Should work for Windows, too! I'd start by checking the return values from your API calls, probably starting with SetWindowsHookEx and working backwards... Good luck with it!

Best,
Frank

Posted on 2010-06-21 09:39:51 by fbkotler