SetWindowsHook() 是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息,而且所监视的窗口可以是其他进程所创建的。当消息到达后,在目标窗口处理函数之前处理它。
钩子机制允许应用程序截获处理window消息或特定事件。
HHOOK WINAPI SetWindowsHookEx( __in int idHook, \\钩子类型 __in HOOKPROC lpfn, \\回调函数地址 __in HINSTANCE hMod, \\实例句柄 __in DWORD dwThreadId); \\线程ID
使用API函数SetWindowsHookEx()把一个应用程序定义的钩子子程安装到钩子链表中。 SetWindowsHookEx函数总是在Hook链的开头安装Hook子程。当指定类型的Hook监视的事件发生时,系统就调用与这个Hook关联的 Hook链的开头的Hook子程。每一个Hook链中的Hook子程都决定是否把这个事件传递到下一个Hook子程。Hook子程传递事件到下一个 Hook子程需要调用CallNextHookEx函数。
SetWindowsHookEx()函数的最后一个参数决定了此钩子是系统钩子还是线程钩子。
#include "stdafx.h" #include <Windows.h> #include<TlHelp32.h> #include<iostream> using namespace std; BOOL InjectDllBySetWindowsHook(ULONG32 ulTargetProcessID); DWORD getThreadID(ULONG32 ulTargetProcessID); int main() { ULONG32 ulTargetProcessID; cout << "请输入目标进程ID:"; cin >> ulTargetProcessID; if (!InjectDllBySetWindowsHook(ulTargetProcessID)) { cout << "Set Hook Unsuccess!\r\n" << endl; return 0; } cout << "Inject Success!\r\n" << endl; return 0; return 0; } BOOL InjectDllBySetWindowsHook(ULONG32 ulTargetProcessID) { HANDLE TargetProcessHandle = NULL; TargetProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, ulTargetProcessID); if (NULL == TargetProcessHandle) { printf("Couldn‘t get Target Process Handle\r\n"); return FALSE; } HMODULE DllModule = LoadLibrary(L"Dll.dll"); if (DllModule == NULL) { printf("cannt find dll\r\n"); return FALSE; } //获取Dll中导出的函数的地址 HOOKPROC Sub_1Address = NULL; Sub_1Address = (HOOKPROC)GetProcAddress(DllModule, "MyMessageProcess"); if (Sub_1Address == NULL) { printf("cannt found MyMessageProcess"); return FALSE; } DWORD ThreadID = getThreadID(ulTargetProcessID); HHOOK Handle = SetWindowsHookEx(WH_KEYBOARD, Sub_1Address, DllModule, ThreadID); if (Handle == NULL) { printf("cannt hook\r\n"); return FALSE; } printf("hook success\r\n"); getchar(); getchar(); getchar(); UnhookWindowsHookEx(Handle); FreeLibrary(DllModule); } DWORD getThreadID(ULONG32 ulTargetProcessID) { HANDLE Handle = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (Handle != INVALID_HANDLE_VALUE) { THREADENTRY32 te; te.dwSize = sizeof(te); if (Thread32First(Handle, &te)) { do { if (te.dwSize >= FIELD_OFFSET(THREADENTRY32, th32OwnerProcessID) + sizeof(te.th32OwnerProcessID)) { if (te.th32OwnerProcessID == ulTargetProcessID) { HANDLE hThread = OpenThread(READ_CONTROL, FALSE, te.th32ThreadID); if (!hThread) { printf("Couldn‘t get thread handle\r\n"); } else { return te.th32ThreadID; } } } } while (Thread32Next(Handle, &te)); } } CloseHandle(Handle); return (DWORD)0; }
Dll部分:
#pragma data_seg(SHARD_SEG_NAME) static HHOOK g_hHook; #pragma data_seg() BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: { MessageBox(NULL, L"Inject Success!", L"1", 0); } case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE; } extern "C" __declspec(dllexport)LRESULT MyMessageProcess(int Code, WPARAM wParam, LPARAM lParam) { // //你自己对消息的处理 // MessageBox(NULL, L"GetMessage!", L"Message", 0); return CallNextHookEx(g_hHook, Code, wParam, lParam); }
可参考百度百科
原文:http://www.cnblogs.com/HsinTsao/p/6457877.html