原始的安装库可以从这里下载: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的代码,比上面一片文章的代码则又简单了不少(这段代码来自于上面的实例中,版权归原作者所有):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #undef UNICODE #include <cstdio> #include <winsock2 .h> #include <ws2tcpip .h> #include <windows .h> #include <detours .h> //*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); } </detours></windows></ws2tcpip></winsock2></cstdio> |
原创文章,转载请注明: 转载自 obaby@mars