我一直在为从 Windows XP 开始的 Windows 版本开发驱动程序。为此,我使用例程 PsSetCreateThreadNotifyRoutine。在注册的回调中,我试图访问线程的上下文,但我没有得到。查到信息说当回调函数被调用时,线程还不在将要使用的上下文中。
我想知道是否有像 kewaitforsingleobject 这样的函数会在这些线程获得上下文时发出信号?
我尝试为出现的每个线程创建一个新线程并让它们等待,但我认为这不是最佳解决方案。
在这种情况下,等待线程接收其上下文的最佳方式是什么?
最佳答案
由于MSDN :
The driver's thread-notify routine runs at IRQL = PASSIVE_LEVEL. When a thread is created, the thread-notify routine runs in the context of the thread that created the new thread. When a thread is deleted, the thread-notify routine runs in the context of this thread when the thread exits.
因此在创建新线程时您可以访问父线程的上下文。 我不确定,但似乎您也可以在删除时访问删除线程的上下文。 (或者可能又是 parent )
因此,您可以使用一个列表来存储线程 ID,并让工作线程检查每个 TID。 不要忘记互斥。您可以使用 MUTEX 或 SPINLOCK(不知道它们在 WDK 中的实际名称)。
关于c# - 监控线程创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19470548/