Solved

Capturing DOS ErrorLevel after Shell

Posted on 2000-03-26
3
1,810 Views
Last Modified: 2008-03-03
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
Comment
Question by:trims30
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
mcrider earned 200 total points
ID: 2659000
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
 

Author Comment

by:trims30
ID: 2659561
Thank You Thank You Thank You

It works like a champ!

Lee
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2661211
You're welcome!  Glad I could help!


Cheers!®©
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question