Weird ReadProcessMemory problem



It took some time to track this one down.

Main GUI thread spawns worker thread

Worker thread does a CreateProcess, gets a process handle for the process
Worker thread enters a debug-event loop, does things until process stops
Worker thread does a PostMessage to main GUI thread
Worker thread terminates

Main GUI thread receives notification
Main GUI thread attempts to ReadProcessMemory
ReadProcessMemory fails with "access denied"
Main GUI thread closes process handle

The solution seems to be

Main GUI thread creates event
Main GUI thread spawns worker thread

Worker thread does a CreateProcess
Worker thread enters debug-event loop
Worker thread does PostMessage to main GUI thread
Worker thread waits on event
Worker thread closes event handle

Main GUI thread receives notification
Main GUI thread performs (successfully) ReadProcessMemory
Main GUI thread closes process handle
Main GUI thread does SetEvent to let worker thread finish


Note that this behavior, that ReadProcessMemory will not work if the thread that created
the process terminates (it makes no sense, actually), is undocumented. It may be that it
would work if I had not used WaitForDebugEvent, but if so, this is also undocumented.

(I'm working on a simple performance analyzer. Stay tuned for later publication...)
joe
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Weird ReadProcessMemory problem
    ... ReadProcessMemory is a little stranger. ... but not in the main GUI thread. ... Worker thread does a CreateProcess, ... MVP Tips: http://www.flounder.com/mvp_tips.htm ...
    (microsoft.public.vc.mfc)
  • Re: Weird ReadProcessMemory problem
    ... which the CreateProcess was done, but not in the main GUI thread. ... Worker thread does a CreateProcess, ... Worker thread calls ReadProcessMemory successfully ...
    (microsoft.public.vc.mfc)
  • Re: Weird ReadProcessMemory problem
    ... In my original code, I did the debugging loop in the main GUI thread, and it all worked ... which the CreateProcess was done, but not in the main GUI thread. ... Worker thread does a CreateProcess, ... Worker thread calls ReadProcessMemory successfully ...
    (microsoft.public.vc.mfc)
  • Re: Weird ReadProcessMemory problem
    ... CreateProcess, when the worker calls CreateProcess, it must provide a ... Worker thread does a CreateProcess, gets a process handle for the process ... Worker thread does a PostMessage to main GUI thread ... Main GUI thread attempts to ReadProcessMemory ...
    (microsoft.public.vc.mfc)
  • Re: Weird ReadProcessMemory problem
    ... Note that if this were the issue, then I would not be able to ReadProcessMemory at all; ... since that results in an empty DACL. ... Worker thread does a CreateProcess, gets a process handle for the process ... Worker thread does a PostMessage to main GUI thread ...
    (microsoft.public.vc.mfc)