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

前端之家收集整理的这篇文章主要介绍了windows – 创建新进程后是否需要使用CloseHandle?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要从上下文菜单中启动一个单独的进程/应用程序;我正在使用函数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 Explorer\\iexplore.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?)

原文链接:https://www.f2er.com/windows/363474.html

猜你在找的Windows相关文章