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

How do I get the errorlevel from a dos app?

Posted on 1997-06-09
Last Modified: 2012-05-04
I want to run pkunzip from a VB5 and can't figure out how to get the errorlevel after it has run. Thanks
Question by:Breez
  • 2

Accepted Solution

y96andha earned 100 total points
ID: 1427024
You can start it with CreateProcess and retrieve the errorlevel with GetExitCodeProcess.

Author Comment

ID: 1427025
umm, CreateProcess and GetExitCodeProccess seem to apply to C++. What would the VisualBasic equivalent be?


Expert Comment

ID: 1427026
They can be used in VB as well.

This is code for declarations:

Private Const INFINITE = &HFFFF                    '  Infinite timeout
Private Const SW_SHOW = 5
        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
        hProcess As Long
        hThread As Long
        dwProcessId As Long
        dwThreadId As Long
End Type
Private 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
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

This is code to execute pkunzip:

    Dim sinfo As STARTUPINFO
    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 = STARTF_USESHOWWINDOW
    sinfo.wShowWindow = SW_SHOW
    success = CreateProcess(vbNullString, "pkunzip c:\adsads.zip", vbNullString, vbNullString, False, NORMAL_PRIORITY_CLASS, vbNullString, vbNullString, sinfo, pinfo)
    If success = 0 Then
        Debug.Print "CreateProcess failed"
        Exit Sub
    End If
    WaitForSingleObject pinfo.hProcess, INFINITE
    success = GetExitCodeProcess(pinfo.hProcess, ExitCode)
    If success <> 0 Then
        Debug.Print "Exit code: "; ExitCode
        Debug.Print "GetExitCodeProcess failed"
    End If
    CloseHandle pinfo.hProcess
    CloseHandle pinfo.hThread

If you want to be able to do other things while pkunzip is running, the code can be easily modified to do that, just call GetExitCodeProcess regularly, and as long as the process is running it will return 259.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
VBA to copy paste columns form one file to other 20 91
Visual Basic Excel Formatting error 4 127
SLMGR Switches Are Not Working On KMS Host 3 110
Sub or Function is not defined 6 43
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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

856 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