INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Get erros from command line and Display in Application

Get erros from command line and Display in Application

(OP)
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.

RE: Get erros from command line and Display in Application

Try using .RedirectStandardError

RE: Get erros from command line and Display in Application

(OP)
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) 

RE: Get erros from command line and Display in Application

If myString.TrimLeft.TrimRight <> "" Then MessageBox.Show(myString)

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.

RE: Get erros from command line and Display in Application

This should do the trick:
If myString.Replace(Environment.NewLine, "").Replace(vbTab, "").Replace(" ", "") <> "" Then MessageBox.Show(myString)

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

RE: Get erros from command line and Display in Application

(OP)
softhemc,

Worked like a charm!

Thanks..

RE: Get erros from command line and Display in Application

Glad it worked.

RE: Get erros from command line and Display in Application

Surely something like the following is a little cleaner:

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()
    Dim ExitClean As Integer = myProcess.ExitCode
    myProcess.Close()

    'Show Mystring
    If Not ExitClean Then MsgBox(myString) 

RE: Get erros from command line and Display in Application

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.

RE: Get erros from command line and Display in Application

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.

RE: Get erros from command line and Display in Application

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 !!!

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close