c++ - MS Detours Express 3.0 没有正确挂接 CreateFile win32 API 函数

标签 c++ winapi hook createfile detours

我正在尝试使用 MS Detours Hook win32 API 函数“CreateFile”,但是当我通过使用 MS Word 打开 *.doc 文件对其进行测试时,对 DLL 的 CreateFile 调用以及 MS Word 加载的字体文件和目录被重定向到我的迂回函数,但不是那个 *.doc 文件,但是当我使用记事本打开 *.txt 文件时,对该 *.txt 文件的 CreateFile 调用来到我的迂回函数。

我正在使用以下代码来挂接 CreateFile:

static HANDLE (WINAPI *Real_CreateFile)(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) = CreateFile;

HANDLE WINAPI Routed_CreateFile(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile)
{
OutputDebugString(lpFileName);
return Real_CreateFile(lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, hTemplateFile);
}

BOOL APIENTRY DllMain( HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved )
{
LONG Error;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:

    OutputDebugString(L"Attaching MyDLL.dll");
    OutputDebugString(strInfo);
    DetourRestoreAfterWith();
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourAttach(&(PVOID&)Real_CreateFile, Routed_CreateFile);
    Error = DetourTransactionCommit();

    if (Error == NO_ERROR)
        OutputDebugString(L"Hooked Success");
    else
        OutputDebugString(L"Hook Error");

    break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
    OutputDebugString(L"De-Attaching MyDLL.dll");
    DetourTransactionBegin();
    DetourUpdateThread(GetCurrentThread());
    DetourDetach(&(PVOID&)Real_CreateFile, Routed_CreateFile);
    Error = DetourTransactionCommit();

    if (Error == NO_ERROR)
        OutputDebugString(L"Un-Hooked Success");
    else
        OutputDebugString(L"Un-Hook Error");

    break;
}
return TRUE;
}

提前致谢。

最佳答案

我认为你在这之后错过了一个break:

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
    break;  // Not interested in thread messages
case DLL_PROCESS_DETACH:

您是否只是在调用绕行之前分离绕行?也许打开 .doc 会创建一个新线程,但 .txt 不会,从而触发此代码路径。

关于c++ - MS Detours Express 3.0 没有正确挂接 CreateFile win32 API 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14235482/

相关文章:

c++ - 在 C++ 类中初始化单例

c++ - unsigned long*double 乘法中的奇怪错误

c++ - 子对话框 - SetWindowTextA 或 SendMessageA 使程序崩溃 - MFC

hook - 重定向到 liferay 登录页面

c++ - MOD 运算是否比乘法更占用 CPU?

c++ - 解析 int 和 string

c# - C# 中用于获取 CPU ID 和驱动器/卷序列号的 API

c++ - 尝试在 Windows 上重新启动打印机后台处理程序时,“net start spooler”失败并显示退出代码 2

windows - 使用 SetWindowsHookEx 的进程范围的钩子(Hook)

adobe - 以编程方式读取 Adob​​e 游戏变量/与游戏/游戏机器人交互