PDA

View Full Version : Read console output from



MarkDynna
08-07-2014, 03:50 PM
Is there a way to capture the output written to the console from a Run program statement?

linder
08-08-2014, 02:26 AM
Mark,

"Run Command Line" is a wrapper around the CreateProcess API. In Windows, you always use ">" to redirect output from a console app to a text file.

Hope this helps.

Friedrich

MarkDynna
08-08-2014, 08:47 AM
And then the text file can be read back into SetupBuilder?

linder
08-08-2014, 08:53 AM
Yes. For example, via the "Handle Text File Operation..." script function.

Friedrich

MarkDynna
08-08-2014, 08:54 AM
Yes. For example, via the "Handle Text File Operation..." script function.

Friedrich

That seems to read it one line at a time. Is there any way to just read the whole text file at once?

linder
08-08-2014, 09:23 AM
Just LOOP through the file and then process the lines. Not sure what you are trying to do with the output result. Are you looking for specific error messages from the console application?

Friedrich

MarkDynna
08-08-2014, 09:26 AM
Kind of, we're just looking to display the contents to the user. We have a secondary install as part of our application and we found that when the secondary installer fails, it dumps the details of the error out to the console. So, we'd like to grab that and display it so we have some idea what went wrong. All we get right now is the not-helpful error code number returned back to SetupBuilder.

linder
08-08-2014, 10:23 AM
Okay. Then LOOP through the file and process the lines (look for error codes or something like that; e.g. using "Handle String Operations..."). Or just display the result.txt as a whole (using notepad or another default app).

Friedrich

MarkDynna
08-08-2014, 01:50 PM
Ok, that all works. Now the issue is that "Run Command Line" always returns error code 0, even if the installer that was run reported an error. Is this something that I'll have to handle manually?

linder
08-09-2014, 02:32 AM
Mark,

It returns "0" because the executable started without any problem (operation succeeded).

Return Values - If the function succeeds, the %_SB_ERRORCODE% return value is zero.

To catch the exit code (if the application returns any; not all do), you have to use "Run Program...".

BTW, if you need a demo project, just let me know.

Friedrich

linder
08-09-2014, 02:59 AM
I have developed and uploaded a "catch exit code" demo for you:

http://www.lindersoft.com/forums/showthread.php?p=78240#post78240

Friedrich

MarkDynna
08-11-2014, 10:41 AM
So, is there a way to run an external program, get the return code and the console output?

linder
08-11-2014, 10:51 AM
This should do it:

http://www.lindersoft.com/forums/showthread.php?p=78220#post78220

The standard ">" Windows command redirects output from a console app to a text file and then just catch the exit code (if the app returns any).

The %_SB_ERRORCODE% return value can also hold the last exit code returned by the executable associated with the work item on its last run.

The %_SB_RETURNEX% variable holds the error code provided by the GetLastError Windows API if the CreateProcess function failed. It holds a system error code if a call to the ShellExecuteEx API failed.

Friedrich

MarkDynna
08-11-2014, 11:29 AM
Right, but I seem to be caught in a no-win situation with the external installer I'm running.

"Run Program" returns the error code back to SetupBuilder, but I can't get the console output: the ">" operator doesn't work.

"Run Command Line" generates the console output, but doesn't return the error code to SetupBuilder.

linder
08-11-2014, 11:50 AM
">" definitely works because it is a Windows "switch" ;-)

Friedrich

MarkDynna
08-11-2014, 12:00 PM
">" definitely works because it is a Windows switch ;-)

Friedrich

Should it work as part of SetupBuilder's "Run Program" statement?

linder
08-11-2014, 12:13 PM
It has nothing to do with SetupBuilder's "Run Program" statement. The statement is just a wrapper around CreateProcess or ShellExecuteEx. Windows handles all the ">" redirection behind the scenes.

See attached screenshots. This "Run Program" function calls the good old PKUNZIP.EXE from 1993 and redirects the console output to the RESULT.TXT file.

Friedrich

MarkDynna
08-11-2014, 02:21 PM
Ok, not sure what I'm doing wrong then. My Run Program command looks a lot like the one you have above. The command line parameters are really long, is there some sort of "hidden character limit" I'm running into. If I take the exact same line and run it manually in a command window, the console output is redirected properly. Running the same command through SetupBuilder doesn't redirect the console output.

linder
08-11-2014, 02:32 PM
Please note that there is a fundamental difference between running an application from an OS "Command Prompt" and via the CreateProcess or ShellExecuteEx API !!!

BTW, the comment line limit is 1,024 bytes.

Friedrich

linder
08-11-2014, 03:31 PM
Mark,

I have created a simple quick-and-dirty demo for you on how to catch the return code and redirect console messages into out.txt (from a "Run Program..." command):

http://www.lindersoft.com/projects/markd.zip

Unzip the archive and compile and RUN the "MarkD.sb8" project file. It will launch a dummy "console1.exe" and displays its exit code (should display "951"). The redirection writes "Hello, this is a test output from a Win32 console app." to a "out.txt" ASCII text file. This method "should" work fine with DOS, Win32/64 and MFC console apps. But no guarantee because it depends on a secret/undocumented Windows feature ;)

Friedrich