VS2008安装Detours库 【Windows 7 64bit】

原始的安装库可以从这里下载:http://research.microsoft.com/sn/detours(下载后是个msi的安装包,完全傻瓜式的安装),在公司使用的win7 32bit系统上安装编译很正常,但是在家里的64位系统上安装编译却出现了不少问题。今天晚上重新看了一下发现默认的那个vs命令行工具貌似是64位的,用兼容性的命令行工具就可以编译了,效果如下图所示:



编译完成之后会生成两个lib文件和一个Dll文件,这些都是运行Detours程序所必需的。上面的只是一些64为环境下的安装需要注意的问题,最后记录一下通用的安装方法吧:

1.当然是下载了,下载链接上面也说过了:http://research.microsoft.com/sn/detours ,下载后是一个msi的安装包,直接安装即可,安装之后没有任何的提示信息。但是可以从下面的路径找到安装后的源代码和文件:C:\Program Files (x86)\Microsoft Research\Detours Express 2.1(如果是32位系统将会是C:\Program Files \Microsoft Research\Detours Express 2.1),对应目录下的三个子目录以及文件功能如下:

samples:程序实例代码,包含了各种函数API的使用实例;

src:detours的接口文件以及头文件;

Detours.chm:帮助文件,包含函数的参数以及使用方法说明等。

2.建议将上面提到的src目录拷贝到vs安装目录下的vc文件夹下(如果是vc6则拷贝到vc98目录下),如果是vs2008则拷贝到vs的默认安装路径C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC即可(64位系统,其他系统请自行调整目录)。并且将src改名为detours以便区分代码用途。

3.运行vs2008 的命令提示行工具,切换到detours目录下,然后执行nmake命令将会自动编译并声称可执行文件、库文件和符号文件(如果是64位系统在命令提示行下无法通过编译则使用本文开始提到的“兼容工具命令提示”即可。)。如果没有错误的话编译完成之后将会生成如下的文件:

在vc的lib目录下生成detoured.lib,detours.lib和detoured.exp

在vc的bin目录下生成detoured.dll和detoured.pdb

将detours.h自动复制到vc下的include文件夹下。

因而在实际使用的过程中只需要将detourd.dll文件复制到system32目录下,并且在程序代码中包含头文件和相关的lib文件即可。

这也是为什么要将detours的src目录拷贝到vc目录下的原因,这样在实际使用的过程中将会减少很多不必要的步骤。

相关的实例可以参考CodeProject的相关工程,代码还是比较完善的,涵盖了较多的函数使用方法:http://www.codeproject.com/KB/DLL/funapihook.aspx

最后还是贴一段Wsock Hook的代码,比上面一片文章的代码则又简单了不少(这段代码来自于上面的实例中,版权归原作者所有):

#undef UNICODE
#include 
#include 
#include 
#include 
#include  //*IMPORTANT: Look at path if compiler error


#pragma comment(lib, "detoured.lib")
#pragma comment(lib, "detours.lib")
#pragma comment(lib, "Ws2_32.lib")

//Prototypes
int (WINAPI *pSend)(SOCKET s, const char* buf, int len, int flags) = send;
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags);
int (WINAPI *pRecv)(SOCKET s, char* buf, int len, int flags) = recv;
int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags);

//Log files
FILE* pSendLogFile;
FILE* pRecvLogFile;

INT APIENTRY DllMain(HMODULE hDLL, DWORD Reason, LPVOID Reserved)
{
	switch(Reason)
	{
	case DLL_PROCESS_ATTACH:	//Do standard detouring
		DisableThreadLibraryCalls(hDLL);
		DetourTransactionBegin();
		DetourUpdateThread(GetCurrentThread());
		DetourAttach(&(PVOID&)pSend, MySend);
		if(DetourTransactionCommit() == NO_ERROR)
			OutputDebugString("send() detoured successfully");
		DetourTransactionBegin();
		DetourUpdateThread(GetCurrentThread());
		DetourAttach(&(PVOID&)pRecv, MyRecv);
		if(DetourTransactionCommit() == NO_ERROR)
			OutputDebugString("recv() detoured successfully");
		break;
	case DLL_PROCESS_DETACH:
		DetourTransactionBegin();	//Detach
        DetourUpdateThread(GetCurrentThread());
		DetourDetach(&(PVOID&)pSend, MySend);
		DetourTransactionCommit();
		DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
		DetourDetach(&(PVOID&)pRecv, MyRecv);
		DetourTransactionCommit();
		break;
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
		break;
	}
	return TRUE;
}

//Open file, write contents, close it
int WINAPI MySend(SOCKET s, const char* buf, int len, int flags)
{
	fopen_s(&pSendLogFile, "C:\\SendLog.txt", "a+");
	fprintf(pSendLogFile, "%s\n", buf);
	fclose(pSendLogFile);
	return pSend(s, buf, len, flags);
}

int WINAPI MyRecv(SOCKET s, char* buf, int len, int flags)
{
	fopen_s(&pRecvLogFile, "C:\\RecvLog.txt", "a+");
	fprintf(pRecvLogFile, "%s\n", buf);
	fclose(pRecvLogFile);
	return pRecv(s, buf, len, flags);
}
☆版权☆

* 网站名称:obaby@mars
* 网址:https://h4ck.org.cn/
* 个性:https://oba.by/
* 本文标题: 《VS2008安装Detours库 【Windows 7 64bit】》
* 本文链接:https://www.h4ck.org.cn/2011/01/2431
* 短链接:https://oba.by/?p=2431
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


猜你喜欢:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注