1.安裝
今天突然心血來潮,想隨便研究些東西。網上看到這個庫就下載安裝了一下。
整體還算順利
首先官網下載此庫
進去之后選擇download。
下載文件到本地之后目錄下面有個src文件夾,還有一些簡單的實例。
這里就不看了,直接進行編譯。
我的開發環境是vs2013,編譯的采用markfile編寫的。用微軟出的nmake編譯。
首先進入vs目錄下的vc目錄,C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin下面有個vcvars32.bat
是設置vc編譯環境變量的。通過dos先執行此批處理。
執行后切換到src目錄,執行命令
>nmake
編譯成功后會在父級目錄生成兩個文件夾,include ,obj.x86
這兩個文件夾.
通過vs打開后
創建一個項目,配置項目屬性 引用目錄 > inlcude目錄.
添加預處理 命令.
#pragma comment(lib,"detours.lib")
2.使用
DetourRestoreAfterWith(); //不知道什么用 DetourTransactionBegin(); //不知道什么用 DetourUpdateThread(GetCurrentThread()); //不知道什么用 DetourAttach(&(PVOID&)OldMessageBoxA, MyMessageBoxA); //Hook跳轉實現 error = DetourTransactionCommit(); // 錯誤返回.
下面是一個完成的代碼。hook MessageBoxA
算是自己做一個備忘吧。
#include <windows.h> #include "detours.h" #include <string> #pragma comment(lib,"detours.lib") static int (WINAPI * OldMessageBoxA)( _In_opt_ HWND hWnd, _In_opt_ LPCSTR lpText, _In_opt_ LPCSTR lpCaption, _In_ UINT uType) = MessageBoxA; int WINAPI MyMessageBoxA( _In_opt_ HWND hWnd, _In_opt_ LPCSTR lpText, _In_opt_ LPCSTR lpCaption, _In_ UINT uType){ return OldMessageBoxA(NULL, "Hello Hook","hello", MB_OK); } int __stdcall WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { long error; MessageBoxA(NULL, "Hello wolrd!", "hello", MB_OK); DetourRestoreAfterWith(); DetourTransactionBegin(); DetourUpdateThread(GetCurrentThread()); DetourAttach(&(PVOID&)OldMessageBoxA, MyMessageBoxA); error = DetourTransactionCommit(); if (error == NO_ERROR) { OutputDebugStringA("Done"); } else { OutputDebugStringA("Error"); } switch (error) { case ERROR_INVALID_BLOCK: OutputDebugStringA("ERROR_INVALID_BLOCK"); break; case ERROR_INVALID_HANDLE: OutputDebugStringA("ERROR_INVALID_HANDLE"); break; case ERROR_INVALID_OPERATION: OutputDebugStringA("ERROR_INVALID_OPERATION"); break; case ERROR_NOT_ENOUGH_MEMORY: OutputDebugStringA("ERROR_NOT_ENOUGH_MEMORY"); break; default: break; } MessageBoxA(NULL, "Hello wolrd!", "hello", MB_OK); return 0; }