.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\user32.lib
includelib \masm32\lib\kernel32.lib
.data
hInstance dd 0
ClassName db 'Notepad',0
mes_err db 'Please, launch Notepad first!',0
box_name db 'Warning',0
.data?
hHook dd ?
hWnd dd ?
.code
DllEntry proc hInst:HINSTANCE, reason:DWORD, reserved1:DWORD
push hInst
pop hInstance
mov eax,TRUE
ret
DllEntry Endp
HookProc proc nCode:DWORD,wParam:DWORD,lParam:DWORD
invoke CallNextHookEx,hHook,nCode,wParam,lParam
mov edx,lParam
assume edx:PTR MSG ; позиционируем edx на структуру сообщения MSG
.if [edx].message==WM_CHAR
mov eax,[edx].wParam
invoke PostMessage,hWnd,my_msg,eax,0
.endif
assume edx:nothing
xor eax,eax
ret
HookProc endp
InstallHook proc hwnd:DWORD
push hwnd
pop hWnd
invoke FindWindow, addr ClassName, NULL
test EAX,EAX
jne window_ok
invoke MessageBoxA, NULL, addr mes_err, addr box_name, MB_ICONERROR
xor EAX, EAX
jmp exit_hook
window_ok:
invoke GetWindowThreadProcessId, EAX, NULL
invoke SetWindowsHookEx,WH_GETMESSAGE,addr HookProc,hInstance,EAX
exit_hook:
mov hHook,eax
ret
InstallHook endp
UninstallHook proc
invoke UnhookWindowsHookEx,hHook
ret
UninstallHook endp
End DllEntry
проблема с хуком
у меня был код на асме, решил переписать под си, а он не пашет. каждую строчку проверил, вроде совпадает все. уже глаз намылен, невижу в чем проблема.
Хук на нажатие клавиш в блокноте, результат пишется в диалоговую прошку
АСМ
библиотека
Код:
клиент
Код:
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib dll_hook.lib
InstallHook proto :DWORD
UninstallHook proto
HookProc proto :DWORD,:DWORD,:DWORD
DlgFunc proto :DWORD,:DWORD,:DWORD,:DWORD
my_msg equ WM_USER
.data
dialog_name db "DIAL",0
hInstance dd ?
buffer db 10 dup (0)
key_code dd 0
flag dd 0
msg1 BYTE 'Presset key',0
msg2 BYTE 'Key ASCII code',0
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,addr dialog_name,NULL,addr DlgFunc,NULL
invoke ExitProcess,NULL
DlgFunc proc hDlg:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
.if uMsg==WM_CLOSE
invoke UninstallHook
invoke EndDialog,hDlg,NULL
.elseif uMsg == WM_INITDIALOG
invoke InstallHook,hDlg
.if eax == 0
invoke ExitProcess,0
.endif
.elseif uMsg==my_msg
mov eax,wParam
mov key_code,eax
.if flag>0
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr msg1
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr key_code
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr msg2
invoke dw2hex,key_code,addr buffer
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr buffer
.endif
.if flag==0
inc flag
.endif
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgFunc endp
end start
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\masm32.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
includelib dll_hook.lib
InstallHook proto :DWORD
UninstallHook proto
HookProc proto :DWORD,:DWORD,:DWORD
DlgFunc proto :DWORD,:DWORD,:DWORD,:DWORD
my_msg equ WM_USER
.data
dialog_name db "DIAL",0
hInstance dd ?
buffer db 10 dup (0)
key_code dd 0
flag dd 0
msg1 BYTE 'Presset key',0
msg2 BYTE 'Key ASCII code',0
.code
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,addr dialog_name,NULL,addr DlgFunc,NULL
invoke ExitProcess,NULL
DlgFunc proc hDlg:DWORD,uMsg:DWORD,wParam:DWORD,lParam:DWORD
.if uMsg==WM_CLOSE
invoke UninstallHook
invoke EndDialog,hDlg,NULL
.elseif uMsg == WM_INITDIALOG
invoke InstallHook,hDlg
.if eax == 0
invoke ExitProcess,0
.endif
.elseif uMsg==my_msg
mov eax,wParam
mov key_code,eax
.if flag>0
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr msg1
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr key_code
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr msg2
invoke dw2hex,key_code,addr buffer
invoke SendDlgItemMessage,hDlg,1,LB_ADDSTRING,NULL,addr buffer
.endif
.if flag==0
inc flag
.endif
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgFunc endp
end start
СИ
библиотека
Код:
#include <windows.h>
#pragma argsused
__declspec(dllexport) int InstallHook(HWND);
__declspec(dllexport) int UninstallHook();
HINSTANCE hInstance=0;
LRESULT CALLBACK HookProc(int, WPARAM, LPARAM);
HHOOK hHook;
HWND hWnd;
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved) {
hInstance = hinstDLL;
return 1;
}
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) {
MSG * msg = (MSG*)lParam;
if (msg->message == WM_CHAR)
PostMessage(hWnd, WM_USER, msg->wParam, 0);
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
__declspec(dllexport) int InstallHook(HWND hwnd) {
HWND hNotepad;
hWnd = hwnd;
hNotepad=FindWindow("Notepad", 0);
if (hNotepad) {
hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)HookProc, hInstance,GetWindowThreadProcessId(hNotepad, 0));
}
else {
MessageBoxA(0, "Notepad is not run", 0, MB_ICONERROR);
hHook = -1;
}
return hHook;
}
__declspec(dllexport) int UninstallHook() {
UnhookWindowsHookEx(hHook);
return 0;
}
#pragma argsused
__declspec(dllexport) int InstallHook(HWND);
__declspec(dllexport) int UninstallHook();
HINSTANCE hInstance=0;
LRESULT CALLBACK HookProc(int, WPARAM, LPARAM);
HHOOK hHook;
HWND hWnd;
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved) {
hInstance = hinstDLL;
return 1;
}
LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) {
MSG * msg = (MSG*)lParam;
if (msg->message == WM_CHAR)
PostMessage(hWnd, WM_USER, msg->wParam, 0);
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
__declspec(dllexport) int InstallHook(HWND hwnd) {
HWND hNotepad;
hWnd = hwnd;
hNotepad=FindWindow("Notepad", 0);
if (hNotepad) {
hHook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)HookProc, hInstance,GetWindowThreadProcessId(hNotepad, 0));
}
else {
MessageBoxA(0, "Notepad is not run", 0, MB_ICONERROR);
hHook = -1;
}
return hHook;
}
__declspec(dllexport) int UninstallHook() {
UnhookWindowsHookEx(hHook);
return 0;
}
клиент
Код:
#include <windows.h>
#include <stdio.h>
#pragma hdrstop
#include <tchar.h>
#pragma argsused
INT_PTR CALLBACK DlgFunc(HWND, UINT, WPARAM, LPARAM);
#pragma comment(lib,"dll.lib")
__declspec(dllimport) int InstallHook(HWND);
__declspec(dllimport) int UninstallHook(void);
BYTE buffer[10];
DWORD key_code;
DWORD flag;
WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow) {
DialogBoxParam(hInstance, "DIAL", 0, DlgFunc, 0);
ExitProcess(0);
return 0;
}
INT_PTR CALLBACK DlgFunc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch(uMsg) {
case WM_CLOSE:
UninstallHook();
EndDialog(hDlg, 0);
break;
case WM_INITDIALOG:
if (!InstallHook(hDlg))
ExitProcess(0);
break;
case WM_USER:
if (flag > 0) {
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0, (long)"Key");
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0, (long) & wParam);
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0,
(long)"Key ASCII code");
sprintf(buffer, "%d", wParam);
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0, (long)buffer);
}
if (!flag)
flag++;
break;
default:
return 0;
}
return 1;
}
#include <stdio.h>
#pragma hdrstop
#include <tchar.h>
#pragma argsused
INT_PTR CALLBACK DlgFunc(HWND, UINT, WPARAM, LPARAM);
#pragma comment(lib,"dll.lib")
__declspec(dllimport) int InstallHook(HWND);
__declspec(dllimport) int UninstallHook(void);
BYTE buffer[10];
DWORD key_code;
DWORD flag;
WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow) {
DialogBoxParam(hInstance, "DIAL", 0, DlgFunc, 0);
ExitProcess(0);
return 0;
}
INT_PTR CALLBACK DlgFunc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch(uMsg) {
case WM_CLOSE:
UninstallHook();
EndDialog(hDlg, 0);
break;
case WM_INITDIALOG:
if (!InstallHook(hDlg))
ExitProcess(0);
break;
case WM_USER:
if (flag > 0) {
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0, (long)"Key");
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0, (long) & wParam);
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0,
(long)"Key ASCII code");
sprintf(buffer, "%d", wParam);
SendDlgItemMessage(hDlg, 1, LB_ADDSTRING, 0, (long)buffer);
}
if (!flag)
flag++;
break;
default:
return 0;
}
return 1;
}
компилятор rad studio 2010