Why GetWindowThreadProcessId some times returns 0?
I am injecting a .NET SpyLib in the target address space using remote invocation. I need to send a message to that window's thread which created that remote process via GetWindowThreadProcessId
.
I am working on Windows 7. Unfortunately it returns 0 most of the time. Sometimes it gives the appropriate thread id (after restarting Windows).
Why am I getting t开发者_运维技巧his strange behavior with GetWindowThreadProcessId
?
The MSDN documentation for GetWindowThreadProcessId has a Community Content section, in which DDeBen has already answered your questions two years before you even asked your question. :)
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).
In the future it might help to remember that GetLastError()
is often the function you need to go to in the Win32 API when you have failure states you are unsure of. MSDN will generally give a link to it, but even if it does not, it can't hurt to check it out. However, be aware that in managed code GetLastError()
requires special treatment or you will end up with bogus results that will only end up confusing you.
精彩评论