我正在使用远程调用在目标地址空间中注入(inject) .NET SpyLib。我需要向通过 GetWindowThreadProcessId
创建该远程进程的窗口线程发送消息。
我在 Windows 7 上工作。不幸的是,它大部分时间都返回 0。有时它会提供适当的线程 ID(在重新启动 Windows 之后)。
为什么我在使用 GetWindowThreadProcessId
时会出现这种奇怪的行为?
最佳答案
GetWindowThreadProcessId 的 MSDN 文档有一个社区内容部分,DDeBen 在您提出问题之前两年就已经在其中回答了您的问题。 :)
GetWindowThreadProcessId returns 0 and the variable pointed to by lpdwProcessId is not modifed if the handle hWnd is not valid. GetLastError() returns 87 (ERROR_INVALID_PARAMETER).
将来,当您不确定故障状态时,记住 GetLastError()
通常是您需要在 Win32 API 中使用的函数可能会有所帮助。 MSDN 通常会给出一个链接,但即使没有,也可以检查一下。 但是,请注意在托管代码中 GetLastError()
需要 special treatment否则您最终会得到虚假的结果,最终只会让您感到困惑。
关于.net - 为什么 GetWindowThreadProcessId 有时会返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5827954/