下载链接:GitHub - microsoft/Detours: Detours is a software package for monitoring and instrumenting API calls on Windows. It is distributed in source code form.
https://github.com/microsoft/Detours
微软的Detours主页:
Detours - Microsoft Research
https://www.microsoft.com/en-us/research/project/detours/
发现Detours的官方文档不够详细,
而且有人在GitHub上的issus也提到了这个问题:Ununderstandable documents (wiki) · Issue #49 · microsoft/Detours · GitHub
https://github.com/microsoft/Detours/issues/49
开发环境
- win10
- Visual Studio 17
- .Net Framework
步骤
- 从github下载zip压缩包,解压
- 打开VS 2017的开发人员命令提示符,cd 进入解压目录,运行命令
nmake
- 如果报错,就像下面这种情况
sn.exe not found · Issue #42 · microsoft/Detours · GitHub
https://github.com/microsoft/Detours/issues/42
sn.exe是.NET Framework里的一个工具,
.NET Framework Tools | Microsoft Docs
https://docs.microsoft.com/en-us/dotnet/framework/tools/
如果电脑里有sn.exe,可以把sn.exe所在目录加入环境变量,这样就能在命令行里访问到sn.exe了。
如果电脑里没有sn.exe,就需要下载最新版本的.NET Framework。
下载链接:Download .NET Framework 4.8 | Free official downloads
https://dotnet.microsoft.com/download/dotnet-framework/net48
我下载的是Developer Pack版本的。
在以下目录查找sn.exe,并把sn.exe所在目录加入环境变量中的path
C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework
C:\Windows\Microsoft.NET
- nmake完成之后,
在“lib.X86”目录获取detours.lib库文件,在“include”目录下获取“detours.h”和“detver.h”两个头文件。
参考链接
- 技术分享 - 使用VS2013编译Detours库
https://www.write-bug.com/article/1939.html - Detours 劫持 - M-Anonymous - 博客园
https://www.cnblogs.com/M-Anonymous/p/9766343.html - 技术分享 - 基于Detours库HOOK API
https://www.write-bug.com/article/1805.html
踩过的坑
- 编译器
用VC6编译使用detours的程序可能会出错,
我用的是VS2017 - 无法编译 PVOID&
PVOID等效于void * —无类型指针
可以使用:
DetourAttach((void **)&OldMessageBoxW, NewMessageBoxW);
参考链接:https://stackoverflow.com/questions/56305389/ms-detours-detourattach-fails
- WINAPI 是这样定义的
#define WINAPI __stdcall
__stdcall 是一种函数参数传递压栈顺序和堆栈平衡的约定
在windows编程中默认的参数传递都是__stdcall方式的