You cannot read from StdErr and StdOut in the script engine this way, since there is no non-blocking I / O, as Code Master Bob says. If the called process fills the buffer (about 4 KB) on StdErr when you try to read from StdOut or vice versa, then you will lock / hang. You will starve while waiting for StdOut, and it will block you from reading StdErr.
The practical solution is to redirect StdErr to StdOut as follows:
sCommandLine = """c:\Path\To\prog.exe"" Argument1 argument2" Dim oExec Set oExec = WshShell.Exec("CMD /S /C "" " & sCommandLine & " 2>&1 """)
In other words, what is passed to CreateProcess is the following:
CMD /S /C " "c:\Path\To\prog.exe" Argument1 argument2 2>&1 "
This calls CMD.EXE, which interprets the command line. /S /C calls a special parsing rule, so that the first and last quotes are deleted, and the remainder is used as-is and CMD.EXE is executed. So, CMD.EXE does the following:
"c:\Path\To\prog.exe" Argument1 argument2 2>&1
Spell 2>&1 redirects prog.exe StdErr to StdOut. CMD.EXE will distribute the exit code.
You can now succeed by reading StdOut and ignoring StdErr.
The disadvantage is that the output from StdErr and StdOut is mixed. As long as they are recognized, you can probably work with this.
Ben Jan 30 '12 at 11:30 2012-01-30 11:30
source share