Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get erros from command line and Display in Application 1

Status
Not open for further replies.

Shift838

IS-IT--Management
Jan 27, 2003
987
US
I am coding a GUI Interface to execute an executable that executes via a command line.

I want to retrieve any errors that when running the executable with arguments that it may generate.

the code below seem to work to display the last message if it runs successfully when you exit the program, but any errors it does not show.

Code:
Dim myProcess As New Process()
        Dim myProcessStartInfo As New ProcessStartInfo("c:\mame0166b\mame64")
        myProcessStartInfo.WorkingDirectory = "c:\mame0166b"
        myProcessStartInfo.Arguments = "ti99_4a single"
        myProcessStartInfo.UseShellExecute = False
        myProcessStartInfo.RedirectStandardOutput = True
        myProcess.StartInfo = myProcessStartInfo
        myProcess.Start()

        Dim myStreamReader As StreamReader = myProcess.StandardOutput
        ' Read the standard output of the spawned process.
        Dim myString As String = myStreamReader.ReadToEnd()
        myProcess.Close()

        'Show Mystring
        MsgBox(myString)

What can I change in order to allow it to retrieve the errors. when it gets an error I just get blank value.
 
using the below code with 'softhemc' recommendation works, but now I would only like to display it if it errors. So currently with the below code it will display it if it errors or if you exit normally it will send blank string.

How can I only get it to work if it receives an actual error.

Code:
 Dim myProcess As New Process()
        Dim myProcessStartInfo As New ProcessStartInfo("c:\mame0166b\mame64")
        myProcessStartInfo.WorkingDirectory = "c:\mame0166b"
        myProcessStartInfo.Arguments = "ti99_4a -gromport single -peb:slot3 32kmem"
        myProcessStartInfo.UseShellExecute = False
        'myProcessStartInfo.RedirectStandardOutput = True
        myProcessStartInfo.RedirectStandardError = True
        myProcess.StartInfo = myProcessStartInfo
        myProcess.Start()

        Dim myStreamReader As StreamReader = myProcess.StandardError
        ' Read the standard output of the spawned process.
        Dim myString As String = myStreamReader.ReadToEnd()
        myProcess.Close()

        'Show Mystring
        MsgBox(myString)
 
[tt]If myString.TrimLeft.TrimRight <> "" Then MessageBox.Show(myString)[/tt]

I'm not currently running VS so check the .Trim - I think it is correct!!

You may also need to check for newline (and possibly tab) - but will only show the messagebox if the string is not empty.
 
This should do the trick:
[tt]If myString.Replace(Environment.NewLine, "").Replace(vbTab, "").Replace(" ", "") <> "" Then MessageBox.Show(myString)[/tt]

In other other words if myString is not empty after removing all whitespace then display it.
 
softhemc,

Worked like a charm!

Thanks..

 
Surely something like the following is a little cleaner:
Code:
[blue]    Dim myProcess As New Process()
    Dim myProcessStartInfo As New ProcessStartInfo("c:\mame0166b\mame64")
    myProcessStartInfo.WorkingDirectory = "c:\mame0166b"
    myProcessStartInfo.Arguments = "ti99_4a -gromport single -peb:slot3 32kmem"
    myProcessStartInfo.UseShellExecute = False
    [green]'myProcessStartInfo.RedirectStandardOutput = True[/green]
    myProcessStartInfo.RedirectStandardError = True
    myProcess.StartInfo = myProcessStartInfo
    myProcess.Start()
    Dim myStreamReader As StreamReader = myProcess.StandardError
    [green]' Read the standard output of the spawned process.[/green]
    Dim myString As String = myStreamReader.ReadToEnd()
    [bold][COLOR=#CC0000]Dim ExitClean As Integer = myProcess.ExitCode[/color][/bold]
    myProcess.Close()

    [green]'Show Mystring[/green]
    [bold][COLOR=#CC0000]If Not ExitClean Then[/color][/bold] MsgBox(myString)[/blue]
 
strongm

I've just double checked this on Ralf Brown's interrupt list to confirm.

There are two exit mechanisms for DOS:

Int 20h (normally used for .com files but can be used with .exe files) - it automatically sets ERRORLEVEL to 0
Int 21h AH = 42h (the normal .exe termination) - This allows the ERRORLEVEL to be set in AL, but from experience many programs just terminate with AX = 4200h, thus setting the ERRORLEVEL to 0, despite reporting errors on STDERR.

This is why I chose not to suggest your approach because we do know from Shift838's comments that STDERR is used.

So whilst I agree with you there was method in my madness.
 
Erm ... Int 21 42H is the call to move a file pointer. Did you mean 4CH, which is terminate with return code?

However, not sure why you have brought DOS interrupts into this; MAME is not a DOS program and so does not call DOS interrupts (and the Windows CLI is not DOS, hasn't been for years). MAME is a Win32 (or, in this case Win64) console application, and correctly sets exit codes.
 
strongm

4Ch is correct. Typing error [blush]

I just assumed that MAME was a DOS program. I downloaded it to have a look after reading your reply - but no luck, the .exe claims to expand ito a specific folder but after completion the folder is empty !!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top