Matt
Matt

Reputation: 774

cmd.exe will not terminate under certain conditions when launched with the CreateProcess function

I am executing batch commands via a C program with the CreateProcess function. I check to see if the process has terminated with the GetExitCodeProcess function, by checking to see if the process exit code equals anything except STILL_ACTIVE. This works fine with commands like "cmd /c echo Hello World", but for commands like "cmd /c dir c:\windows" or "cmd /c dir c:\windows & exit", cmd.exe will never terminate. Either command when launched thru the command prompt will terminate as expected. Does anyone know why it never terminates when launched via CreateProcess? Thanks a lot.

Upvotes: 1

Views: 1537

Answers (1)

Matt
Matt

Reputation: 774

Apparently I have to start to read the stdout/stderr pipe in the loop which checks to see if the process is still running, as well as read it after the process has terminated. Rather than just read it after the process has terminated. Many thanks to Wimmel for posting the answer.

Upvotes: 2

Related Questions