08-14-2009, 09:21 AM
My VB6 program opens a program which takes 20 or 30 seconds to run and then sets the exit code to a specific value depending on certain conditions (basically pass, fail, or an error code). I am not very familiar with the DOS exit code but is there a way to check this code after the called program is finished? I am using the Shell command to run the called program like this:
Shell ("<Called Program> 200")
Thanks for your help.
08-14-2009, 01:20 PM
Got this code from here: http://www.vbforums.com/showpost.php?p=2031843&postcount=2
17. How to Shell and wait? Option Explicit
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Const INFINITE = -1&
Private Function shellAndWait(ByVal fileName As String) As Long
Dim executionStatus As Long
Dim processHandle As Long
Dim returnValue As Long
'Execute the application/file
executionStatus = Shell(fileName, vbNormalFocus)
'Get the Process Handle
processHandle = OpenProcess(&H100000, True, executionStatus)
'Wait till the application is finished
returnValue = WaitForSingleObject(processHandle, INFINITE)
'Send the Return Value Back
shellAndWait = returnValue
Private Sub Command1_Click()
'launch the application and wait
Dim fileName As String
Dim retrunValue As Long
fileName = "EXCEL.EXE"
retrunValue = shellAndWait(fileName)
If retrunValue = 0 Then
MsgBox "Application executed and was finished"
Hope this helps.
08-18-2009, 10:20 AM
Thanks for the link EM. Unfortunately this code always returns an exit code of 0. If I run the called program from a batch file and then use the "if errorlevel 1" command to retrieve the exit code, everything works fine. I tested this by setting the exit code to 0 and 1 in the called program.
Does anybody know why the code pasted above by EM doesn't return the correct exit code? My code is basically identical to the example program.
08-18-2009, 12:34 PM
That's because the code I gave you uses Excel and it will only return a non-zero if it crashes. :) If you use your own program that returns an exit code, like your batch file, then it works fine. I actually did this once as a proof of concept (used a C program as the shelled program, if I remember correctly) and it does work when the process being called actually returns a value.
Replace fileName = "EXCEL.EXE"
with fileName = "<your application here>"
08-18-2009, 12:56 PM
I did replace the "EXCEL.EXE" with my application's name. Let me explain further what I'm trying to do. There is a terminal emulator program called Tera Term. This is similar to Hyper Terminal where you can control a serial port connection or Telnet session but it also allows custom scripts to run. I am starting this Tera Term program from the VB app. At the end of the Tera Term script, there is a command to set the exit code to a certain value. I am setting this to different values depending on the outcome of running the script. My problem is that even when I change this exit code value, the VB app always thinks its a 0. Since the batch file is able to see different values from the Tera Term program, it seems like the VB app is not reading this exit code correctly.
Thanks for the help EM.
08-19-2009, 09:16 PM
just a suggestion..
try using timer command in vb
08-20-2009, 03:43 PM
After several hours of internet searching, I finally figured out a solution to retrieve the exit code (also called the return code) of a program. Check out this link: http://support.microsoft.com/kb/129796/EN-US/
08-21-2009, 01:45 PM
1 crucial detail makes the code I posted incorrect; the use of OpenProcess . If you replace OpenProcess with CreateProcess and adapt the parameters accordingly, the code I gave should work.