#include <Windows.h>
#include <stdio.h>
typedef LRESULT (*TestHookProc)(int code, WPARAM wParam, LPARAM lParam);
int main()
{
HWND hwnd = FindWindow(NULL, L"SetWindowHook");
DWORD dwThreadId = 0;
DWORD dwProcessId = 0;
HMODULE hModule = LoadLibrary(L"H:\\Debug\\SetWindowHook\\Debug\\HookDll.dll");
TestHookProc lpfn = (TestHookProc)GetProcAddress(hModule, "TestHookproc");
DWORD dwError = GetLastError();
dwThreadId = GetWindowThreadProcessId(hwnd, &dwProcessId);
HHOOK hook = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)lpfn, hModule, dwThreadId);
getchar();
UnhookWindowsHookEx(hook);
return 0;
}
LRESULT TestHookproc(int code, WPARAM wParam, LPARAM lParam)
{
return CallNextHookEx(0, code, wParam, lParam);
}
SetWindowsHookEx的原理就是在console里加载dll,利用SetWindowsHookEx函数来将其注入到目标进程中,也就是说,在Console和目标进程中都有同一份dll。Hook函数要写在dll中,SetWindowsHookEx函数写在Console中。
|