detours是微软的钩子库,以下是简单的使用
#include "pch.h"
#include <Windows.h>
#include "detours.h"
#include <stdio.h>
#pragma comment(lib,"detours.lib")
static int(WINAPI* OLD_MessageBoxW)(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType) = MessageBoxW;
int WINAPI NEW_MessageBoxW(HWND hWnd, LPCWSTR lpText, LPCWSTR lpCaption, UINT uType)
{
//修改输入参数,调用原函数
int ret = OLD_MessageBoxW(hWnd, L"test3", L"test3", uType);
return ret;
}
void Hook()
{
DetourRestoreAfterWith();//恢复原来状态
DetourTransactionBegin();//拦截开始
DetourUpdateThread(GetCurrentThread());//刷新当前线程
DetourAttach((void **)&OLD_MessageBoxW, NEW_MessageBoxW);
DetourTransactionCommit();//拦截生效
}
void UnHook()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((void **)&OLD_MessageBoxW, NEW_MessageBoxW);//撤销拦截函数
DetourTransactionCommit();//
}
int main()
{
MessageBoxW(NULL,L"test", L"test", 0);
Hook();
MessageBoxW(NULL, L"test2", L"test2", 0);
UnHook();
return 0;
}