HookDLL.asm
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
.data
hInstance dd ?
;共享数据段
.data?
hWnd dd ?
hHook dd ?
dwMessage dd ?
szAscii db 4 dup(?)
.code
;Entry
DLLEntry proc _hInstance,_dwReason,_dwReserved
push _hInstance
pop hInstance
mov eax,TRUE
ret
DLLEntry endp
HookProc proc _dwCode,_wParam,_lParam
local @szKeyState[256]:byte
invoke CallNextHookEx,hHook,_dwCode,_wParam,_lParam
invoke GetKeyboardState,addr @szKeyState
invoke GetKeyState,VK_SHIFT
mov @szKeyState + VK_SHIFT,al
mov ecx,_lParam
shr ecx,16
invoke ToAscii,_wParam,ecx,addr @szKeyState,addr szAscii,0
mov byte ptr szAscii[eax],0
invoke SendMessage,hWnd,dwMessage,dword ptr szAscii,NULL
xor eax,eax
ret
HookProc endp
InstallHook proc _hWnd,_dwMessage
push _hWnd
pop hWnd
push _dwMessage
pop dwMessage
invoke SetWindowsHookEx,WH_KEYBOARD,addr HookProc,hInstance,NULL
mov hHook,eax
ret
InstallHook endp
UninstallHook proc
invoke UnhookWindowsHookEx,hHook
ret
UninstallHook endp
End DLLEntry
Main.asm
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include HookDLL.inc
includelib HookDLL.lib
IDD_MAIN equ 1000
IDC_TEXT equ 1001
WM_HOOK equ WM_USER + 100h
.data
hInstance dd ?
.code
_ProcDlgMain proc uses ebx esi edi hWnd,uMsg,wParam,lParam
local @dwTemp
mov eax,uMsg
.if eax == WM_CLOSE
invoke UninstallHook
invoke EndDialog,hWnd,NULL
.elseif eax == WM_INITDIALOG
invoke InstallHook,hWnd,WM_HOOK
.if !eax
invoke EndDialog,hWnd,NULL
.endif
.elseif eax == WM_HOOK
mov eax,wParam
.if al == 0dh
mov eax,0a0dh
.endif
mov @dwTemp,eax
invoke SendDlgItemMessage,hWnd,IDC_TEXT,EM_REPLACESEL,0,addr @dwTemp
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
_ProcDlgMain endp
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,IDD_MAIN,NULL,addr _ProcDlgMain,NULL
;invoke MessageBeep,0FFFFFFFFh
invoke ExitProcess,NULL
end start
HookDLL.rc
#define IDD_MAIN 1000
#define IDC_TEXT 1001
#include <resource.h>
IDD_MAIN DIALOGEX 10,10,198,78
CAPTION "键盘钩子demo"
FONT 10,"MS Serif",400,255,0
STYLE 0x90CF0000
BEGIN
CONTROL "",IDC_TEXT,"Edit",0x50010000,0,3,198,75,0x00000200
END
编译命令
ml /c /coff HookDLL.asm
link /DLL /subsystem:windows /section:.bss,S /def:HookDLL.def HookDLL.obj
rc HookDLL.rc
ml /c /coff Main.asm
link /subsystem:windows HookDLL.res Main.obj