图解Detour安装及简单使用实例(Win7+VC6)

来源:互联网 发布:地图画路线软件 编辑:程序博客网 时间:2024/05/21 20:23

相关下载:

http://pan.baidu.com/s/1o7OEMc6

detour6.rar是本文工程

DetoursExpress30是微软下载的detour安装文件

detoured是编译好的库


参考

http://www.cnblogs.com/weiqubo/archive/2011/06/01/2065534.html
http://blog.csdn.net/evi10r/article/details/6659354
http://blog.csdn.net/donglinshengan/article/details/8544464:

http://research.microsoft.com/en-us/projects/detours/

微软detour下载


Detours是微软开发的一个函数库,可用于捕获系统API。

1 安装






2 生成库

detour下载后是源码,需要自己生成库;


拷贝上图的src文件夹到下图路径;


进入VC的bin执行vcvars32.bat

再进入src,执行nmake


失败;参阅

http://blog.csdn.net/donglinshengan/article/details/8544464

将detours安装目录中的system.mak和Makefile复制到VC目录


又出错;

单独输入cl命令试试,出现下图错误;


下载mspdb60.dll,拷贝到系统目录;注册;出现下面错误;干点活真不容易;


不管了,因为是找不到cl命令,把nmake使用的命令抄下;

cl /W4 /WX /Zi /MTd /Gy /Gm- /Zl /Od /DDETOURS_BITS=32 /DWIN32_LEAN_AND_MEAN /D_WIN32_WINNT=0x403 /Gs /DDETOURS_X86=1 /DDETOURS_32BIT=1 /D_X86_ /DDETOURS_OPTION_BITS=64 /Fd.. \lib.X86\detours.pdb /Foobj.X86\detours.obj /c detours.cpp

进入bin目录直接运行看看;


还是不行;拷贝detour的src文件夹全部内容到bin,再执行上述命令;还是不行;


至此参照网上资料自己编译detour库失败;估计是我的VC版本太低;不过这难不倒咱;网上直接下载编译好的detour库就好了;

3 简单使用例子

参照网上一些例子;

例子中入口是

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)

所以新建如下工程,结果编译了不能链接;


新建如下的工程;


修改原例子代码如下;

// detour6.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "detour6.h"#include <detours.h>#pragma comment(lib, "detours.lib") #pragma comment(lib, "detoured.lib")#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// The one and only application objectCWinApp theApp;using namespace std;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"输入参数已被我老人家HOOK修改",L"[Detour测试]",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 _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){int nRetCode = 0;// initialize MFC and print and error on failureif (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)){// TODO: change error code to suit your needscerr << _T("Fatal Error: MFC initialization failed") << endl;nRetCode = 1;}else{// TODO: code your application's behavior here.MessageBoxW(0,L"正常消息框",L"测试",0);        Hook();        MessageBoxW(0,L"正常消息框",L"测试",0);        UnHook();}return nRetCode;}

成功,最终的运行效果如下;




0 0