-
detours
(函數庫)
鎖定
Detours是微軟開發的一個函數庫,可用於捕獲系統API。
- 外文名
- detours
- 定 義
- 微軟開發的一個函數庫
- 類 型
- 函數庫
- 作 用
- 捕獲系統API
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文件了。
detours簡單例子
HOOK MessageBoxW函數
- #include "stdafx.h"
- //#include "DetourHook.h"
- #include <detours.h>
- #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. Detours .microsoft[引用日期2013-04-27]
- 詞條統計
-
- 瀏覽次數:次
- 編輯次數:10次歷史版本
- 最近更新: 阿妧云