kampi
kampi

Reputation: 2484

How to get the PIDs of the child processes?

Is there a way, to get the PIDs of the child processes? I mean, if i open a cmd prompt using CreateProcess, i know its PID because i can get it from the returned ProcessInformation structure. But is it possible to get the PIDs of the processes too, which were opened from this command prompt?

Thanks!

Upvotes: 0

Views: 1664

Answers (2)

Harry Johnston
Harry Johnston

Reputation: 36318

Create a job using CreateJobObject and use SetInformationJobObject to associate the job with an I/O completion port; see JOBOBJECT_ASSOCIATE_COMPLETION_PORT for more details. Create the process with CREATE_SUSPENDED and use AssignProcessToJobObject to make the new process part of the job.

Keep in mind that sometimes the new grandchild process will have exited already by the time you see the process ID, and that process can be reused. If you use OpenProcess to get a handle to the grandchild process, make sure you process errors properly, and if you do open a handle successfully make sure you call IsProcessInJob to verify that the handle is indeed pointing to one of the grandchild processes.

Since processes cannot be nested prior to Windows 8/2012, you might need to use the CREATE_BREAKAWAY_FROM_JOB flag when creating the child process.

Upvotes: 0

RRUZ
RRUZ

Reputation: 136391

you can use the CreateToolhelp32Snapshot function passing the TH32CS_SNAPPROCESS value, then call the Process32First method , and finally you must iterate over the collection returned and compare the value of the th32ParentProcessID field against the PID of the cmd.exe. Another option is use the Win32_Process WMI Class using the ParentProcessId property to filter the results.

Upvotes: 1

Related Questions