图解Detour安装及简单使用实例(Win7+VC6)
相關下載:
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_FILE static 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,表明撤銷多個函數HOOKDetourDetach(&(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; }成功,最終的運行效果如下;
總結
以上是生活随笔為你收集整理的图解Detour安装及简单使用实例(Win7+VC6)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux原始套接字学习总结
- 下一篇: 图解计算机图形学三维变换算法