butter7372 发表于 2015-5-20 09:11:20

win7生成生成Detours.lib以及简单使用实例

  Detours是微软开发的一个函数库,可用于捕获系统API。在用其进行程序开发之前,得做一些准备工作:
一.下载Detours
   在http://research.microsoft.com/sn/detours 可免费下载Detours
二.安装Detours
      一路NEXT
三.生成Detours库
      在安装后的文件夹下找不到直接可以拿来用的LIB文件,但是却有SRC文件(在**\Microsoft Research\Detours Express 2.1\src下)。该文件夹下还有Makefile,可以直接用来生成库。
      将Detours路径下的SCR文件夹拷贝到**\Microsoft Visual Studio 9.0\VC路径下,注意是整个文件夹(其它版本VC自己照着复制)
      在system32目录找到cmd右键以管理员身份运行,切换至 c:\Program Files\Microsoft Visual Studio 9.0\VC\bin目录运行vcvars32.bat
      切换到\Microsoft Visual Studio9.0\VC\SRC,然后输入..\bin\nmake指令,编译成功后在\Microsoft Visual Studio9.0\VC\Lib文件下就能找到detoured.lib与detours.lib文件了。

  附使用简单例子:HOOK MessageBoxW函数


#include "stdafx.h"
#include "DetourHook.h"
#include
#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;
      
}  
页: [1]
查看完整版本: win7生成生成Detours.lib以及简单使用实例