سؤال

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;
هل كانت مفيدة؟

المحلول

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).

Note that hStdOutput should be INVALID_HANDLE_VALUE instead of NULL: that's the convention for passing the absence of a handle in STARTUPINFO.

نصائح أخرى

To the question "do you need to?" - the simple answer is "no". When a process ends all handles connected to it are destroyed in Windows OSs.. if you are creating a process within a process (I don't even want to try that in C, but it's quite simple in C#) then it would be safer to make sure the child process cleans up after itself before (or on-) termination.

A way to test this would be to try and access your handle after you've terminated your child process in your parent process.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top