Breez
asked on
How do I get the errorlevel from a dos app?
I want to run pkunzip from a VB5 and can't figure out how to get the errorlevel after it has run. Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
They can be used in VB as well.
This is code for declarations:
Private Const INFINITE = &HFFFF ' Infinite timeout
Private Const SW_SHOW = 5
Private Const STARTF_USESHOWWINDOW = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20
Private 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
Private Type PROCESS_INFORMATION
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 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 = 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.h Process, ExitCode)
If success <> 0 Then
Debug.Print "Exit code: "; ExitCode
Else
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.
This is code for declarations:
Private Const INFINITE = &HFFFF ' Infinite timeout
Private Const SW_SHOW = 5
Private Const STARTF_USESHOWWINDOW = &H1
Private Const NORMAL_PRIORITY_CLASS = &H20
Private 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
Private Type PROCESS_INFORMATION
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 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 = STARTF_USESHOWWINDOW
sinfo.wShowWindow = SW_SHOW
success = CreateProcess(vbNullString
If success = 0 Then
Debug.Print "CreateProcess failed"
Exit Sub
End If
WaitForSingleObject pinfo.hProcess, INFINITE
success = GetExitCodeProcess(pinfo.h
If success <> 0 Then
Debug.Print "Exit code: "; ExitCode
Else
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.
ASKER
thanks