Solved

Capturing DOS ErrorLevel after Shell

Posted on 2000-03-26
3
1,812 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

695 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