?
Solved

Capturing DOS ErrorLevel after Shell

Posted on 2000-03-26
3
Medium Priority
?
1,815 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 800 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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 …
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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