2
votes

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.

1
Are you escaping the backslashes? `\`David Ruhmann
It is pulling the commands from a SQL database, and the commands inside of the database are valid. It is not a escape issue. After some more testing it seems to only be a problem if there is more than 1 page of output. "cmd /c dir c:\" and "cmd /c dir c:\test" works fine, however "cmd /c dir c:\windows" does not. I wonder why all the hate and down votes.Matt
I was mistaken, it is due to the number of contents in the directly, but i'm not sure the exact amount. With 30+ directories inside c:\test it worked, with 120+ directories it never exited againMatt
I answered your question in the other post. The reason it works with 60 and not with 61 is the size of the output. With 60 it still fits in the buffer, but 61 is too large. You will see the same problem when you type a textfile. A small textfile will work, while a large textfile will cause your program to hang.wimh
thank you very much Wimmel! I happened to coincidentally figure this out on my own about half a hour ago, but had I not, I'd still be banging my head against the wall without your help.Matt

1 Answers

3
votes

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.