Detou简单使用实例

打开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;
        
}
参考:

http://blog.csdn.net/evi10r/article/details/6659354

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值