views:

65

answers:

3

Is there a way (in C++ & windows XP) to detect if one process spawns any other processes?

for example,

write.exe in system32 spawns wordpad.exe then disappears, is there a function that tells me if the process is about to do this?

for those interested i solved the problem using this section of msdn:
http://msdn.microsoft.com/en-us/library/aa390425(v=VS.85).aspx

+1  A: 

You can enumerate over the process tree, which identifies running processes and their parents. This is the inverse of what you want (you want to identify child processes, not parent processes). But of course by keeping track of parent process IDs while enumerating, you can identify which sub-processes a given process has spawned.

To do this, call CreateToolhelp32Snapshot and then use Process32First and Process32Next to enumerate the processes. The enumeration will fill in a PROCESSENTRY32 struct that contains a th32ParentProcessID member.

This is a polling method; there may be another way of actually hooking the CreateProcess function, but I don’t have any information about that.

Nate
hi, yes i have that functionality already in the program, but your method would mean iterating over the process tree even when there might not have been child processes spawned. i would rather it was an events based solution i.e something is emitted when one process calls createprocess (or similar functions)
Tom
A: 

I think you would need to make a global hook DLL that attaches itself to every running process. DLL then finds a place where a function call to CreateProcess is mapped to actual CreateProcess from kernel32, and change a table entry to redirect the call to it's own code to "detect" the call to CreateProcess. All this assuming that some user firewall will not prevent your global hook from executing.

Dialecticus
+2  A: 

Nothing in the Win32 API for this. However, it is supported through WMI with the Win32_ProcessStartTrace query. You'll find some C# code that demonstrates the query in my answer in this thread. Writing WMI code in C++ is fairly painful, you'll find a link to boilerplate code you have to write in the MSDN Library article.

Do beware that this isn't particularly fast. It isn't clear to me how much help the WMI provider gets from the kernel to generate the notification but given the speed it quacks like polling. In other words, the process is likely to be well on its way by the time you get the notification. This is otherwise par for the course on a multitasking operating system.

Hans Passant
[ProcessHacker](http://processhacker.sourceforge.net/) does this.
lsalamon
thanks, found this section of msdn that answered my question :)http://msdn.microsoft.com/en-us/library/aa390425(v=VS.85).aspx
Tom