Question
September 13, 2007 at 04:00 PM
asihuay

VBA code to run command line and retrieve exit code

by asihuay . Updated 19 years, 1 month ago

Hi.
The code below is for run an command line proggramm from vba but the exit code is not the correct.
The intention of the code is to execute command line in minimized windows.
here is the code:
‘modulo version 2007 09 13 4:45PM
‘FuncionRunCommandLineAndReturnExitCode
‘
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
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 Long
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 WaitForSingleObject Lib “kernel32” (ByValhHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib “kernel32” (ByVal lpApplicationName As String, _
ByVal lpCommandLine As String, _
ByVal lpProcessAttributes As Long, _
ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, _
ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, _
ByVal lpCurrentDirectory As String, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib “kernel32” (ByVal hObject As Long) As Long

Private Declare Function GetExitCodeProcess Lib “kernel32″ (ByVal hProcess As Long, lpExitCode As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&
Private Const SW_MINIMIZE = 6
Private Const STARTF_USESHOWWINDOW = &H1
Private Const SW_SHOWMINIMIZED = 2

Private Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO

‘ Initialize the STARTUPINFO structure:
start.cb = Len(start)

‘set startup info to window minimized.
start.dwFlags = STARTF_USESHOWWINDOW
start.wShowWindow = 6 ‘”SW_MINIMIZE”

‘ Start the shelled application:
ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)

‘ Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = ret&
End Function

Public Function FuncionRunCommandLineAndReturnExitCode(ByVal LineaDeComando As String) As Long
‘this function return exit code after run command line programm
‘ esta funcion devuelve el exit code despues de terminar la ejecucion de un command line

‘sincronus execution
‘ la ejecucion es sincronica osea el codigo se detiene de ejecutar hasta que termina el command line

‘ LineaDeComando is string variable with the full command line
‘ LineaDeComando es variable de texto que contiene toda la linea de comando para ejecutar el command line
Dim retval As Long
retval = ExecCmd(LineaDeComando)
FuncionRunCommandLineAndReturnExitCode = retval
End Function
Private Sub Demostration()
‘C:\cmdutils\7za.exe a -tzip %destino%\%datename%.zip arch\* -r -w%destino%
Dim LineaDeComando As String
ZipExe = “C:\Program Files\7-ZIP\7z.exe”
ChDrive “c”

ChDir “c:\”
‘MsgBox CurDir
Origen = “CMDUTILS\*”
Destino = “c:\”
ZipPath = Destino & “zzzzzzzJ.zip”

LineaDeComando = ZipExe & ” a -tzip ” & ZipPath & ” ” & Origen & ” -r -w” & Destino
MsgBox FuncionRunCommandLineAndReturnExitCode(LineaDeComando)
End Sub

This discussion is locked

All Comments