我是否需要在 CreateProcess 之后关闭标准 I/O 的句柄? [英] Do I need to close handles to standard I/O after CreateProcess?
问题描述
我在调用 CreateProcess
时使用了以下 STARTUPINFO
结构.进程结束后是否需要在 hStdError
和 hStdInput
上调用 CloseHandle
?
I use the following STARTUPINFO
structure in my call to CreateProcess
. Do I need to call CloseHandle
on hStdError
and hStdInput
after the process ends?
startupInfo.cb = sizeof(startupInfo);
startupInfo.cb = sizeof(si);
startupInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
startupInfo.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
startupInfo.hStdOutput = NULL;
startupInfo.hStdError = GetStdHandle(STD_ERROR_HANDLE);
startupInfo.wShowWindow = SW_HIDE;
推荐答案
既然你没有打开那些句柄(那不是GetStdHandle
确实),您不需要关闭它们(也许您想要关闭它们出于其他原因,但不太可能).(注意:即使你做了打开句柄,您不必等待进程退出在关闭它们之前:一旦它们被继承,在父级中关闭它们过程对孩子没有影响).
Since you didn't open those handles (that's not what GetStdHandle
does), you don't need to close them (maybe you want to close them
for some other reason, but it's unlikely). (Note: even if you did
open the handles, you don't have to wait for the process to exit
before you close them: once they are inherited, closing them in parent
process has no effect on the child).
注意 hStdOutput
应该是 INVALID_HANDLE_VALUE
而不是NULL
:这是在 STARTUPINFO
中传递没有句柄的约定.
Note that hStdOutput
should be INVALID_HANDLE_VALUE
instead of
NULL
: that's the convention for passing the absence of a handle in STARTUPINFO
.
这篇关于我是否需要在 CreateProcess 之后关闭标准 I/O 的句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!