HelpHow to get a process Exit Code?

Posts 1–8 of 8 · Page 1 of 1
How to get a process Exit Code?
Hello guys c:

So, I'm trying to start a process from VB and get the ExitCode when the process end.
This process is a C program that can return 0 in case of sucess, -1 or -2 in case of error.
I would like to manipulate my VB program according to the exit code to show the user what happens and why.

I tryed using MyProcess.ExitCode (MyProcess is a Process variable), but it says the program should exit before using this code. Then I tryed using MyProcess.Close() and then MyProcess.ExitCode, but it says there was no process attached to MyProcess.

Anyone know a way to do it?

Thank you guys and sorry for any grammar error.
"So, I'm trying to start a process from VB and get the ExitCode when the process end." - Please post code related to process start / exit code checking.

"I tryed using MyProcess.ExitCode (MyProcess is a Process variable), but it says the program should exit before using this code."
Is that a problem, or a question? Because it makes sense...it wouldn't HAVE an exit code, until it exits ofc.


"Then I tryed using MyProcess.Close() and then MyProcess.ExitCode" - what happened?
Did the target process close? Did the code not compile? Runtime Exception? unexpected value returned?

Code:
If MyProcess.HasClosed Then ''//or was  it HasExited? or IsStillRunning? ..find the name.
'we can get the error code
Dim _targetProcStatusResult as Int32 = MyProcess.ExitCode
Else
'process still running, can't get error code yet.
End IF
''maybe
Code:
Dim p() As Process = Process.GetProcesses
        For Each i As Process In p
            If i.ProcessName = "processname" Then
                
            End If
Next i
That'll be how you would get the process by anme, id, ect..

Code:
If MyProcess.HasExited = True Then

            End If
process exited :l
op: "So, I'm trying to start a process from VB"

Code:
Dim p() As Process = Process.GetProcesses
        For Each i As Process In p
            If i.ProcessName = "processname" Then
                
            End If
Next i
using the above code would return the 'first instance' of the process found running (in-case you have multiple copies open, ever)

op is probably using Process.Start("C:/theGame.exe") and assigned the return value of that function to some variable. I want to see though. Post some code op :b

Code:
MyProcess = Process.Start("C:/theGame.exe")
...
If MyProcess.HasExited = True Then
'safe to check MyProcess.ExitCode
    ''''return 0 in case of sucess, -1 or -2 in case of error
    If MyProcess.ExitCode = 0 Then
    Msgbox("Target Program closed with 0 errors - All to the good.")
    Else
    MsgBox("Target Program Crashed / Reported Error - ERROR")
    End If
Else
'don't check it yet
End If
-----------

"I tryed using MyProcess.ExitCode"
How/where did you call it? Inside a timer? Or a button_click, or ?
Idk, but I think as long as you check (apparently have to) .HasExited() = True, it should be ok.

edit: you can use @Xzevos's code, later, to find the ExitCode of other processes, not just ones you* start. useful.
Idk enough information to know how to use the code. He gotta respond now.
Code:
Dim p As New Process
p.StartInfo.FileName = "blah.exe"
p.StartInfo.Arguments = "/blah"
p.Start
p.WaitForExit 
MessageBox.Show(p.ExitCode)
You probably forgot WaitForExit or something.
Thanks mate, now everything is running OK c:
The problem was that WaitForExit thing.
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?