我正在尝试使用 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/