How to get process ID of browser child process after BrowserChildProcessHostImpl->Launch

StackOverflow https://stackoverflow.com/questions/21731184

  •  10-10-2022
  •  | 
  •  

In the context of the Chromium open source code, I'm developing a class which extends from content::BrowserChildProcessHostDelegate and IPC::Sender. Inside one of the methods of my class I launch a child process like so:

scoped_ptr<BrowserChildProcessHostImpl> process_;
...
process_->Launch(
#if defined(OS_WIN)
     new MySandboxedProcessLauncherDelegate,
#elif defined(OS_POSIX)
     false, // don't use zygote
     env,
#endif
     cmd_line);

Question: How can one get the PID of the child process which is being created by this Launch function?

EDIT: Simply calling the following function right after Launch does not work:

base::ProcessHandle child_handle = process_->GetHandle();
有帮助吗?

解决方案

I solved this by calling process_->GetHandle(); inside my class, in the overridden method: virtual void OnProcessLaunched() OVERRIDE; as shown in the code below:

void MyProcessHost::OnProcessLaunched() {
      MyProcessHost::child_process_handle_ = this->process_->GetHandle();
}

I used a static field: static base::ProcessHandle child_process_handle_ to store the value of the handle, but you can of course, use a non-static field as well.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top