win32asm Hook demo

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
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值