Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1829
  • Last Modified:

Capturing DOS ErrorLevel after Shell

I have VB program that shells out to a DOS app synchronously via my ShellWait routine to perform some DOS program functions.  If an error occurs in the dos program it returns a DOS ErrorLevel.

Is there any way to capture this status in my VB Program?

I am using a ShellAndWait function to create a Process, Wait for it to finish and then close the Process.

Lee
0
trims30
Asked:
trims30
  • 2
1 Solution
 
mcriderCommented:
Here you go... Add the following code to a module, then you can do this:

    Dim x As Long
    x = ExecCmd("Your command goes here")
   
x will contain the exit code...

Cheers!®©


THE CODE:

    Const INFINITE = &HFFFF
    Const SW_SHOW = 5
    Const SHOW_OPENWINDOW = &H1
    Const NORMAL_PRIORITY_CLASS = &H20
    Type STARTUPINFO
            cb As Long
            lpReserved As Long
            lpDesktop As Long
            lpTitle As Long
            dwX As Long
            dwY As Long
            dwXSize As Long
            dwYSize As Long
            dwXCountChars As Long
            dwYCountChars As Long
            dwFillAttribute As Long
            dwFlags As Long
            wShowWindow As Integer
            cbReserved2 As Integer
            lpReserved2 As Byte
            hStdInput As Long
            hStdOutput As Long
            hStdError As Long
    End Type
    Type PROCESS_INFORMATION
            hProcess As Long
            hThread As Long
            dwProcessId As Long
            dwThreadId As Long
    End Type
    Declare Sub SleepAPI Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
    Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As String, ByVal lpThreadAttributes As String, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As String, ByVal lpCurrentDirectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
    Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
    Function ExecCmd(CmdString As String) As Long
        Dim sinfo As STARTUPINFO
        Dim pinfo As PROCESS_INFORMATION
        Dim success As Integer
        Dim ExitCode As Long
        sinfo.cb = Len(sinfo)
        sinfo.lpTitle = 0
        sinfo.lpReserved = 0
        sinfo.lpDesktop = 0
        sinfo.cbReserved2 = 0
        sinfo.lpReserved2 = 0
        sinfo.dwFlags = SHOW_OPENWINDOW
        sinfo.wShowWindow = SW_SHOW
        success = CreateProcess(vbNullString, CmdString, vbNullString, vbNullString, False, NORMAL_PRIORITY_CLASS, vbNullString, vbNullString, sinfo, pinfo)
        If success = 0 Then
            ExecCmd = -1
            Exit Function
        End If
        Do
            success = GetExitCodeProcess(pinfo.hProcess, ExitCode)
            If success = 0 Then
                ExecCmd = -2
                Exit Function
            End If
            DoEvents
            SleepAPI 55
        Loop Until ExitCode <> 259
        ExecCmd = ExitCode
        CloseHandle pinfo.hProcess
        CloseHandle pinfo.hThread
    End Function
0
 
trims30Author Commented:
Thank You Thank You Thank You

It works like a champ!

Lee
0
 
mcriderCommented:
You're welcome!  Glad I could help!


Cheers!®©
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now