代码如下:
#include
#include
#include
#include"detours.h"
#pragma comment(lib,"detours.lib")
static int (WINAPI *OldMessageBoxW)(
HWND hWnd,
LPCWSTR lpText,
LPCWSTR lpCaption,
UINT uType) = MessageBoxW;
int WINAPI NewMessageBoxW(
HWND hWnd,
LPCSTR lpText,
LPCSTR lpCaption,
UINT uType)
{
return 0;
}
//开始拦截
void Hook()
{
DetourRestoreAfterWith();//恢复原来的状态
DetourTransactionBegin();//拦截开始
DetourUpdateThread(GetCurrentThread());//刷新当前线程
//这里可以多次使用DetourAttach,表明Hook多个函数
DetourAttach((void **)&OldMessageBoxW,NewMessageBoxW);//实现函数拦截
DetourTransactionCommit();//拦截生效
}
//取消拦截
void UnHook()
{
DetourTransactionBegin();//拦截开始
DetourUpdateThread(GetCurrentThread());//刷新当前线程
//这里可以多次使用DetourAttach,表明撤销多个函数Hook
DetourDetach((void **)&OldMessageBoxW, NewMessageBoxW);//撤销函数拦截
DetourTransactionCommit();//拦截生效
}
void main()
{
//printf("%p", main);
MessageBoxW(0, L"我就简单试试", L"我就不信不行", 0);
Hook();
MessageBoxW(0, L"原来可以啊", L"我就不信不行", 0);
system("pause");
}
在Debug状态下调试,无法拦截
在Release状态下调试,拦截成功,求解这是为什么?怎样修改能在Debug下也能成功,谢谢各位大神