zcc1414

博客园 首页 联系 订阅 管理

Detours是微软开发的一个函数库,可用于捕获系统API。在用其进行程序开发之前,得做一些准备工作:

一.下载Detours

     在http://research.microsoft.com/sn/detours 可免费下载Detours,当前的最新版本是

Detours Express 2.1 isavailable for immediate download under a no-fee, click-through license forresearch, non-commercial, and non-production use on 32-bit code.
Detours Professional 2.1 includes a license for use in production environmentsand the right to distribute detour functions in products. In addition tosupport for 32-bit x86 code, Detours Professional 2.1 includes support for64-bit code on x64 and IA64 processors. For information on licensing DetoursProfessional 2.1 visit Microsoft's IP Licensing Group at www.microsoft.com/iplicensingand search under Detours.

二.安装Detours

    从网上下载的是DetoursExpress.msi,安装该文件

三.生成Detours库

    在安装后的文件夹下找不到直接可以拿来用的LIB文件,但是却有SRC文件(在**\Microsoft Research\DetoursExpress 2.1\src下)。该文件夹下还有Makefile,可以直接用来生成库。

    安装后的文件夹目录

具体生成库的做法是:

     1.将Detours路径下的SCR文件夹拷贝到**\MicrosoftVisual Studio\VC98路径下,注意是整个文件夹

      2.运行**\Microsoft Visual Studio\VC98\Bin下VCVARS32.BAT文件.

        在开始->运行里面输入CMD命令,在出来的命令行窗口里,将路径换至**\MicrosoftVisual Studio\VC98\Bin,再将VCVARS32.BAT文件拖进命令行窗口里就行

3.运行NMAKE命令

         在命令行窗口里将路径换到**\Microsoft Visual Studio\VC98\SRC,然后输入..\bin\nmake指令,回车

4.待此命令运行完后,在**\Microsoft Visual Studio\VC98\Lib文件下就能找到detoured.lib与detours.lib文件.


但是 3.0  只有一个lib文件

#include "stdafx.h"
#include <windows.h>
#include <detours.h> //*IMPORTANT: Look at path if compiler error

#pragma comment(lib, "detours.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 _tmain(int argc, _TCHAR* argv[])
{
	MessageBoxW(0,L"正常消息框",L"测试",0);
	Hook();
	MessageBoxW(0,L"正常消息框",L"测试",0);
	UnHook();

	return 0;
}

但是还是32位的程序·································









posted on 2014-05-14 17:51  zcc1414  阅读(376)  评论(0编辑  收藏  举报