I wish to dump the memory contents of a pid that is not the current pid.
Then you will need to use OpenProcess() to get a handle to the process in question.
CreateToolhelp32Snapshot() doesn't snapshot the memory. It will give you lists of the modules (DLLs) that are a part of your process image which you can then iterate using Module32First/Next() and obtain the load addresses and extents.
It will also give you a list of the heaps, runtime allocated memory used by alloc/free and stacks. You then use Heap32ListFirst/Next to iterate those and obtain their start addresses and extents.
You then use those addresses with ReadProcessMemory() to actually read the ram.
It's a fairly involved process, but reasonably well documented. Remember you will need appropriate permissons.
Also, be sure to suspend the target process otherwise things could change the moment after you obtain your information. If the target process contains threads, you have another sets of loops to jump through.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |
Also, be sure to suspend the target process otherwise things could change
This is all very fascinating stuff. I'm not accustomed to windows at all. But this statement is somewhat curious, since CreateToolhelp32Snapshot() seems to take a "snapshot," so you don't have to worry about state changes — according to the MSDN pages...
Do I definitely need to suspend it? I'll re-read that tomorrow. How accurate are those pages? They seem pretty detailed, even if they're a bit tricky to navigate. I've never really read them...
Lastly, thanks for your help. (It would seem this isn't very perl related.)
| [reply] |
... since CreateToolhelp32Snapshot() seems to take a "snapshot," so you don't have to worry about state changes ...
Well sure. You ask for a snapshot and get it. As you process that snapshot, you are told there is a read/write chunk of memory 39046kb long starting at 0x00x6580000. So you go to read that with ReadProcessMemory(), but in the meantime, the process called VirtualFree() and oh, sorry all gone. Or it dynamically loads a new DLL, or releases an old one, or creates or destroys a thread. Or just reaches the end of its program and terminates.
A snapshot is just that, a moment in time. Self-consistant at that moment in time, but otherwise still a static representation of a complex, running process, subject to change. Suspend the process before you get your snapshot and nothing will change until you resume it.
(It would seem this isn't very perl related.)
Hm. It can all be done from Perl, and to a Perl process. So it is at least as relevant as all the many, many posts here on how to explore the /dev/proc/... virtual filespaces on *nix, and nobody bats an eye at questions about or answers that detail its use.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] [select] |