1. 程式人生 > >通過安裝WH_CALLWNDPROC全域性鉤子將DLL注入所有進行視窗過程的程序

通過安裝WH_CALLWNDPROC全域性鉤子將DLL注入所有進行視窗過程的程序

// HookDll.cpp : 定義 DLL 應用程式的匯出函式。
//

#include "stdafx.h"
#include <windows.h>

static HHOOK hHook=NULL;
HINSTANCE hInstance; //鉤子函式所在模組的控制代碼

BOOL APIENTRY DllMain( HANDLE hModule, 
					  DWORD  ul_reason_for_call, 
					  LPVOID lpReserved
					  )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		hInstance = (HINSTANCE)hModule;
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		break;
	default:
		break;
	}

	return TRUE;
}


LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{

	// 一般來說,所有執行的程序(有視窗過程的)都會載入這個 鉤子過程了
	return CallNextHookEx(hHook, nCode, wParam, lParam );
}

BOOL SetHook()
{
	hHook = SetWindowsHookEx(WH_CALLWNDPROC, HookProc, hInstance,NULL);
	return hHook != NULL;  
}

BOOL EndHook()
{
	return UnhookWindowsHookEx(hHook);
}