简体   繁体   English

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

[英]Do I need to use CloseHandle after creating a new process?

I need to launch a separate process/application from a context menu; 我需要从上下文菜单中启动一个单独的进程/应用程序; I'm using the function launch_program to do so. 我正在使用函数launch_program这样做。 I don't care about the exit code from the created process once it terminates, I just want to be able to launch it. 一旦它终止,我不关心创建过程的退出代码,我只是想能够启动它。 My question is: if the variables startup_info and proc_info are being passed by reference to CreateProcess am I able to use CloseHandle on them if I'm just going to return from the function to my Main Thread? 我的问题是:如果变量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);

}

I used https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx for reference. 我使用https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx作为参考。

PS I just used Internet Explorer as a filler PS我只是使用Internet Explorer作为填充程序
[EDIT] For Completeness: [编辑]完整性:

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

Yes, you can and should close those handles when you no longer need them, including right away if you'll never need them. 是的,您可以并且应该在不再需要它们时关闭这些手柄,包括如果您永远不需要它们的话。

From the page you linked 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 . 当您不再需要这些句柄时,请使用CloseHandle函数关闭它们


[ EDIT ] To emphasize the *should* close part, which maybe isn't stated strongly enough in the docs, here is a quote from @RaymondChen's blog: [编辑]为了强调*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? ) 你确实记得CloseHandle你传递给CreateProcess函数的PROCESS_INFORMATION结构中返回的句柄,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM