The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Tam on April 04, 2007, 12:52:07 PM

Title: About Hook
Post by: Tam on April 04, 2007, 12:52:07 PM
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

MessageProc proto nCode:DWORD, wParam:WPARAM, lParam:LPARAM

.const
    MM_CHAR  equ WM_USER + 432
.data
    hInst   dd 0
.data?
    hhk     dd ?
    hwnd    dd ?
   
.code

DllEntry proc hInstance:DWORD, dwReason:DWORD, dwReserved:DWORD
    .if dwReason == DLL_PROCESS_ATTACH
        push hInstance
        pop hInst
    .endif
    mov eax, TRUE
    ret
DllEntry Endp

SetHook proc hWnd:DWORD
    push hWnd
    pop    hwnd
    invoke SetWindowsHookEx, WH_GETMESSAGE, MessageProc, hInst, NULL
    mov hhk, eax
    ret
SetHook Endp

UnHook proc
    invoke UnhookWindowsHookEx, hhk
    ret
UnHook Endp

MessageProc proc nCode:DWORD, wParam:WPARAM, lParam:LPARAM
    mov edx, lParam
    assume edx:ptr MSG
    .if [edx].message == WM_CHAR
        invoke PostMessage, hwnd, MM_CHAR, [edx].wParam, [edx].hwnd
    .endif
    invoke CallNextHookEx, hhk, nCode, wParam, lParam
MessageProc Endp

End DllEntry




compile command line:
\masm32\bin\ml /c /coff MsgHook.asm
\masm32\bin\Link /section:.bss,S /SUBSYSTEM:WINDOWS /DLL /DEF:MsgHook.def MsgHook.obj


I do as Iczelion said, but why it can't work.
when I call "SetHook", it return the handle of hook, but I call "UnHook", it return FALSE, why ?
in fact, I don't understand why "/section:.bss,S", it's ".bbs", but in codes, it's .data:
.data?
    hhk     dd ?
    hwnd    dd ?
Title: Re: About Hook
Post by: Tam on April 05, 2007, 03:17:58 PM
oh, MessageProc lost a instruction: ret
ha ha, I am so careless.
Thank you, everybody.
Title: Re: About Hook
Post by: realcr on April 08, 2007, 12:53:47 AM
Quote

UnHook proc
    invoke UnhookWindowsHookEx, hhk
    ret
UnHook Endp

Why do u wrap simple functions with more simple functions? Maybe I am missing something , but I think it just makes everything slower and less clear when trying to understand the code.

realcr.
Title: Re: About Hook
Post by: PBrennick on April 08, 2007, 02:12:07 PM
I think his idea is a good one, start simple so it is easy to see where the mistakes are. Once he gets a grip on how it works, it will be on to bigger and better things.

Paul
Title: Re: About Hook
Post by: Tam on April 09, 2007, 07:05:47 AM
QuoteWhy do u wrap simple functions with more simple functions?
You must know that it's a DLL project, EXE may not know the handle of hook(hhk), so I must encapsullation a export function for EXE call.