我有几个关于 Microsoft Detours Library 的快速问题。我以前(成功地)使用过它,但我只是想到了这个功能:
LONG DetourUpdateThread(HANDLE hThread);
我在别处读到,这个函数实际上会挂起线程,直到事务完成。这似乎很奇怪,因为大多数示例代码调用:
DetourUpdateThread(GetCurrentThread());
无论如何,显然这个函数“登记”线程,这样,当事务提交(并且绕道)时,如果它们的指令指针位于“目标函数或蹦床函数的重写代码内”,它们的指令指针就会被修改。
我的问题是:
当事务提交时,当前线程的指令指针是否会在 DetourTransactionCommit 函数中?如果是这样,我们为什么要费心去更新它呢?
此外,如果登记的线程被挂起,当前线程如何继续执行(假设大多数示例代码调用 DetourUpdateThread(GetCurrentThread());)?
最后,您能否暂停当前进程的所有线程,避免竞争条件(考虑到线程可能随时被创建和销毁)?也许这是在交易开始时完成的?这将使我们能够更安全地枚举线程(因为似乎不太可能创建新线程),但 CreateRemoteThread() 呢?
谢谢,
保罗
作为参考,以下是简单示例的摘录:
// DllMain function attaches and detaches the TimedSleep detour to the
// Sleep target function. The Sleep target function is referred to
// through the TrueSleep target pointer.
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
{
if (dwReason == DLL_PROCESS_ATTACH) {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)TrueSleep, TimedSleep);
DetourTransactionCommit();
}
else if (dwReason == DLL_PROCESS_DETACH) {
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)TrueSleep, TimedSleep);
DetourTransactionCommit();
}
return TRUE;
}