Solved

Capturing DOS ErrorLevel after Shell

Posted on 2000-03-26
3
1,800 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
Comment Utility
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
Comment Utility
Thank You Thank You Thank You

It works like a champ!

Lee
0
 
LVL 14

Expert Comment

by:mcrider
Comment Utility
You're welcome!  Glad I could help!


Cheers!®©
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now