gpt4 book ai didi

windows - 创建新进程后是否需要使用 CloseHandle?

转载 作者:可可西里 更新时间:2023-11-01 13:06:53 28 4
gpt4 key购买 nike

我需要从上下文菜单启动一个单独的进程/应用程序;我正在使用函数 launch_program 来执行此操作。一旦创建的进程终止,我不关心退出代码,我只希望能够启动它。我的问题是:如果变量 startup_infoproc_info 通过引用传递给 CreateProcess 我可以使用 CloseHandle 如果我只是要从函数返回到我的主线程,就可以使用它们吗?

void launch_program()
{
STARTUPINFO startup_info;
PROCESS_INFORMATION proc_info;
LPCSTR location = "C:\\Program Files (x86)\\Internet Explorer\\iexplore.exe";

ZeroMemory( &startup_info,sizeof(startup_info));
startup_info.cb = sizeof(startup_info);
ZeroMemory( &proc_info,sizeof(proc_info));

CreateProcess( location,
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&startup_info,
&proc_info);

}

我用了https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx以供引用。

PS 我只是用 Internet Explorer 作为填充物
[编辑] 为了完整性:

CloseHandle(proc_info.hProcess);
CloseHandle(proc_info.hThread);

最佳答案

是的,您可以而且应该在不再需要它们时关闭它们,包括在您永远不需要它们时立即关闭它们。

从您链接的页面Creating Processes :

The thread and process handles are created with full access rights, although access can be restricted if you specify security descriptors. When you no longer need these handles, close them by using the CloseHandle function.


[ 编辑 ] 为了强调 *should* close 部分,文档中的说明可能不够强烈,这里引用 @RaymondChen 的博客:

Why do some process stay in Task Manager after they’ve been killed?

After all the drivers have acknowledged the death of the process, the "meat" of the process finally goes away. All that remains is the "process object", which lingers until all handles to the process and all the threads in the process have been closed. (You did remember to CloseHandle the handles returned in the PROCESS_INFORMATION structure that you passed to the CreateProcess function, didn't you?)

关于windows - 创建新进程后是否需要使用 CloseHandle?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236771/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com