我有一些关于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;
}
Run Code Online (Sandbox Code Playgroud)