;本文改编自网上的c/c++代码。
;本文的功能是没有dll实现全局的hook.
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
.data?
buffer db 512 dup (?)
.code
hstdout dd 0
hstdin dd 0
x dd 0
correy db "made by correy",0
g_kb_hook dd 0
stMsg MSG <>
keydown db "keydown - vkCode %04x, scanCode %04x",13,10,0
keyup db "keyup - vkCode %04x, scanCode %04x",13,10,0
syskeydown db "syskeydown - vkCode %04x, scanCode %04x",13,10,0
syskeyup db "syskeyup - vkCode %04x, scanCode %04x",13,10,0
LowLevelKeyboardProc proc code:UINT, wParam:WPARAM, lParam:LPARAM
mov eax,lParam
.if wParam == WM_KEYDOWN
invoke wsprintf,addr buffer,addr keydown,dword ptr [eax],dword ptr [eax + 4]
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0
.elseif wParam == WM_KEYUP
invoke wsprintf,addr buffer,addr keyup,dword ptr [eax],dword ptr [eax + 4]
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0
.elseif wParam == WM_SYSKEYDOWN
invoke wsprintf,addr buffer,addr syskeydown,dword ptr [eax],dword ptr [eax + 4]
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0
.elseif wParam == WM_SYSKEYUP
invoke wsprintf,addr buffer,addr syskeyup,dword ptr [eax],dword ptr [eax + 4]
invoke lstrlen,addr buffer
invoke WriteFile,hstdout,addr buffer,eax,addr x,0
.else
nop
.endif
invoke CallNextHookEx,g_kb_hook,code,wParam,lParam
ret
LowLevelKeyboardProc endp
start:
invoke GetStdHandle,-10
mov hstdin,eax
invoke GetStdHandle,-11
mov hstdout,eax
invoke SetConsoleTitle,addr correy
invoke SetConsoleScreenBufferSize,hstdout,01000099h;高字是高度,低字是宽度。
invoke GetModuleHandle,0
invoke SetWindowsHookEx,WH_KEYBOARD_LL, addr LowLevelKeyboardProc,eax,0
again:invoke GetMessage,addr stMsg,0,0,0
cmp eax,0
je exit
invoke DispatchMessage,addr stMsg
jmp again
exit:
invoke UnhookWindowsHookEx,g_kb_hook
invoke ExitProcess,0
end start
;made at 2011.10.23
;//////////////////////////////////////////////////////////////////////////////////
;下面是c/c++代码。
#include <windows.h>
HHOOK g_kb_hook = 0;
LRESULT CALLBACK kb_proc (int code, WPARAM w, LPARAM l)
{
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)l;
const char *info = NULL;
if (w == WM_KEYDOWN) info = "key dn";
else if (w == WM_KEYUP) info = "key up";
else if (w == WM_SYSKEYDOWN) info = "sys key dn";
else if (w == WM_SYSKEYUP) info = "sys key up";
printf ("%s - vkCode [%04x], scanCode [%04x]\n", info, p->vkCode, p->scanCode);
return CallNextHookEx (g_kb_hook, code, w, l);
};
int main (void)
{
g_kb_hook = SetWindowsHookEx ( WH_KEYBOARD_LL, &kb_proc, GetModuleHandle (NULL),0)
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
DispatchMessage (&msg);
};
UnhookWindowsHookEx (g_kb_hook);
return 0;
};
没有评论:
发表评论