打开vs2008 -》新建 -》项目-》Win32项目(输入DetourHook) -》确定 -》下一步 -》Windows应用程序(其它默认)-》完成。
把下面代码完全拷贝到DetourHook.cpp中,DetourHook.cpp中原来代码完全删除。编译。通过。。有警告忽略处理。。。。。
#include "stdafx.h"
#include "DetourHook.h"
#include <detours.h>
#pragma comment(lib, "detours.lib")
#pragma comment(lib, "detoured.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"输入参数已修改",L"[测试]",uType);
return ret;
}
VOID Hook()
{
DetourRestoreAfterWith();
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
//这里可以连续多次调用DetourAttach,表明HOOK多个函数
DetourAttach(&(PVOID&)OLD_MessageBoxW,NEW_MessageBoxW);
DetourTransactionCommit();
}
VOID UnHook()
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
//这里可以连续多次调用DetourDetach,表明撤销多个函数HOOK
DetourDetach(&(PVOID&)OLD_MessageBoxW,NEW_MessageBoxW);
DetourTransactionCommit();
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
MessageBoxW(0,L"正常消息框",L"测试",0);
Hook();
MessageBoxW(0,L"正常消息框",L"测试",0);
UnHook();
return 0;
}
参考: