CreateProcess返回的句柄与已启动的Chrome.exe不同 [英] CreateProcess returns handle different than launched Chrome.exe

查看:127
本文介绍了CreateProcess返回的句柄与已启动的Chrome.exe不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CreateProcess并将Chrome.exe作为参数.

I am using CreateProcess and giving Chrome.exe as the argument.

我正在处理使用 PROCESS_INFORMATION 创建的过程的过程,该过程内部具有 hProcess

I am getting the handle of the process I created using PROCESS_INFORMATION which internally has hProcess

当我使用 GetProcessId(handle)打印PID时,得到的PID与任务管理器中显示的PID不同.

When I print the PID using GetProcessId(handle) I am getting a different PID than the ones showing in the task manager.

我尝试将回调函数设置为在Chrome.exe存在后触发,但是无论如何都会触发.这是预期的(不希望的),因为Chrome.exe PID不同.

I have tried setting callback function to trigger after Chrome.exe exists, but it triggers anyway. This is expected (not desired) since Chrome.exe PID is different.

似乎当我在Chrome.exe上使用CreateProcess时,chrome需要自由地启动其自己的新进程并使所有控件无效.

It seems like when I use CreateProcess on Chrome.exe, chrome takes liberty to start its own new process and render all my control useless.

我尝试将其与FireFox.exe一起使用,并且效果很好,我得到了指向正确过程的句柄.

I have tried using it with FireFox.exe and it worked well, I got the handle it pointed to the correct process.

是否无法获取我产生的Chrome进程的句柄?

Is it not possible to get handles to Chrome processes I spawn?

推荐答案

使用 CreateProcess()生成的Chrome进程反过来又生成了自己的子进程,然后终止本身.您的Firefox并没有这样做,至少在最初没有这样做(Firefox确实将子进程用于浏览器选项卡-大多数现代的浏览器都这样做,以确保安全性和稳定性).

The Chrome process you are spawning with CreateProcess() is, in turn, spawning its own child process(es) and then terminating itself. Your Firefox is not doing that, at least not initially (Firefox does use child processes for browser tabs - most modern browsers do, for security and stability).

因此,您从 CreateProcess()获得的Chrome PID/句柄虽然有效,但寿命很短,显然对您的需求毫无用处.

So, the Chrome PID/handle you get from CreateProcess(), albeit valid, is short-lived and clearly useless for your needs.

但是,一切并没有丢失.您可以收到有关Chrome本身产生的子PID的通知.将您生成的Chrome进程添加到作业对象,然后使用 SetInformationJobObject() 将该作业分配给I/O完成端口,然后使用 GetQueuedCompletionStatus() 以便从作业中接收事件,尤其是在作业中创建新进程时,尤其是 JOB_OBJECT_MSG_NEW_PROCESS ,以及作业中的所有进程均已结束时,则为JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO .请参见如何等待作业中的所有进程都退出?/a>以获得更多详细信息.

But, all is not lost. You can get notified about the child PID(s) that Chrome itself spawns. Add your spawned Chrome process to a job object, then use SetInformationJobObject() to assign that job to an I/O completion port, then use GetQueuedCompletionStatus() to receive events from the job, in particular JOB_OBJECT_MSG_NEW_PROCESS whenever a new process is created in the job, and JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO when all processes in the job have ended. See How do I wait until all processes in a job have exited? for more details.

这篇关于CreateProcess返回的句柄与已启动的Chrome.exe不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆