加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:42:14 所属栏目:Windows 来源:网络整理
导读:我需要从上下文菜单中启动一个单独的进程/应用程序;我正在使用函数launch_program这样做.一旦它终止,我不关心创建过程的退出代码,我只是想能够启动它.我的问题是:如果变量startup_info和proc_info是通过引用CreateProcess传递的,我可以在它们上面使用CloseH
我需要从上下文菜单中启动一个单独的进程/应用程序;我正在使用函数launch_program这样做.一旦它终止,我不关心创建过程的退出代码,我只是想能够启动它.我的问题是:如果变量startup_info和proc_info是通过引用CreateProcess传递的,我可以在它们上面使用CloseHandle,如果我只是要从函数返回到我的主线程吗?
void launch_program()
{
    STARTUPINFO startup_info;
    PROCESS_INFORMATION proc_info;
    LPCSTR location = "C:Program Files (x86)Internet Exploreriexplore.exe";

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

    CreateProcess(  location,NULL,FALSE,&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.

[编辑]为了强调*应该*关闭部分,这可能在文档中没有足够强烈地说明,这里引用@ 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?)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读