News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

About Hook

Started by Tam, April 04, 2007, 12:52:07 PM

Previous topic - Next topic

Tam

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

Tam

oh, MessageProc lost a instruction: ret
ha ha, I am so careless.
Thank you, everybody.

realcr

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.

PBrennick

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
The GeneSys Project is available from:
The Repository or My crappy website

Tam

#4
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.