使用 detours 框架 hook 函数
生活随笔
收集整理的這篇文章主要介紹了
使用 detours 框架 hook 函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
detours是微軟的hook框架,可以去 github 下載源碼編譯:
https://github.com/microsoft/Detours
編譯得到 detours.lib ,然后把 detours.lib 和 detours.h 加到你的項目文件夾中:
接下來就可以無腦 hook 啦!我這里寫一個小demo,實現了一個加法函數,大家不需要理解加法的原理,這里只是為了演示:
void __stdcall Add(int a, int b) {printf("%d + %d = %d\n", a, b, a + b); }然后使用這個 detours 把這個函數hook掉,下面給出完整代碼:
#include <windows.h> #include <stdio.h> #include "detours.h" #pragma comment(lib, "detours.lib")void __stdcall Add(int a, int b) {printf("%d + %d = %d\n", a, b, a + b); }static void (__stdcall *OldAdd)(int a, int b) = Add;void __stdcall NewAdd(int a, int b) {printf("%d - (-%d) = %d\n", a, b, a + b); }VOID Hook() {DetourRestoreAfterWith();DetourTransactionBegin();DetourUpdateThread(GetCurrentThread());//這里可以連續多次調用DetourAttach,表明HOOK多個函數DetourAttach(&(PVOID &)OldAdd, NewAdd);DetourTransactionCommit(); }VOID UnHook() {DetourTransactionBegin();DetourUpdateThread(GetCurrentThread());//這里可以連續多次調用DetourDetach,表明撤銷多個函數HOOKDetourDetach(&(PVOID &)OldAdd, NewAdd);DetourTransactionCommit(); }int main() {Add(3, 4);Hook();Add(3, 4);UnHook();return 0; }怎么樣,是不是很神奇呀?
總結
以上是生活随笔為你收集整理的使用 detours 框架 hook 函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xp扫雷简单逆向
- 下一篇: directx 游戏模拟键盘输入