Hi there,
I need to call a .exe in the DOS-Command Window and display the return codes. I have the following code for this:
Private Const WIN32_API_INFINITE = -1&
Private Const WIN32_API_SYNCHRONIZE = &H100000
Private Const WIN32_API_PROCESS_QUERY_INFORMATION = &H400
Private Const WIN32_API_WAIT_OBJECT_0 = 0
Private Const WIN32_API_WAIT_TIMEOUT = &H102
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hHandle As Long, ByRef nExitCode As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Public Function SyncShellCmd(CmdStr As String, ApplicationFocus As Long) As Long
'Run the application by the CmdStr, and exit when the program terminated, Application focus specific
'the focus of the application. e.g vbNormalFocus
Dim lPid As Long 'The task ID of the started program
Dim lHnd As Long ' handle to object to wait for
Dim lRet As Long 'Return value of WaitForSingleObject()
Dim nCode As Long
SyncShellCmd = -1
If CmdStr = "" Then Exit Function
lPid = Shell(CmdStr, ApplicationFocus)
If lPid <> 0 Then
'Get a handle to the shelled process.
lHnd = OpenProcess(WIN32_API_SYNCHRONIZE Or WIN32_API_PROCESS_QUERY_INFORMATION, 0, lPid)
'If successful, wait for the application to end and close the handle.
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, WIN32_API_INFINITE) 'no time out
If GetExitCodeProcess(lHnd, nCode) <> 0 Then SyncShellCmd = nCode
CloseHandle (lHnd)
End If
End If
End Function
Private Sub Command1_Click()
Dim nRes As Long
nRes = SyncShellCmd("c:\errLevel.exe", vbHide)
Debug.Print nRes
End Sub
The mehtod OpenProcess always returns 0. If I try to call notepad.exe or calc.exe, it's working fine.
Does anyone have an idea? I would appreciate any hint.
I need to call a .exe in the DOS-Command Window and display the return codes. I have the following code for this:
Private Const WIN32_API_INFINITE = -1&
Private Const WIN32_API_SYNCHRONIZE = &H100000
Private Const WIN32_API_PROCESS_QUERY_INFORMATION = &H400
Private Const WIN32_API_WAIT_OBJECT_0 = 0
Private Const WIN32_API_WAIT_TIMEOUT = &H102
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hHandle As Long, ByRef nExitCode As Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Public Function SyncShellCmd(CmdStr As String, ApplicationFocus As Long) As Long
'Run the application by the CmdStr, and exit when the program terminated, Application focus specific
'the focus of the application. e.g vbNormalFocus
Dim lPid As Long 'The task ID of the started program
Dim lHnd As Long ' handle to object to wait for
Dim lRet As Long 'Return value of WaitForSingleObject()
Dim nCode As Long
SyncShellCmd = -1
If CmdStr = "" Then Exit Function
lPid = Shell(CmdStr, ApplicationFocus)
If lPid <> 0 Then
'Get a handle to the shelled process.
lHnd = OpenProcess(WIN32_API_SYNCHRONIZE Or WIN32_API_PROCESS_QUERY_INFORMATION, 0, lPid)
'If successful, wait for the application to end and close the handle.
If lHnd <> 0 Then
lRet = WaitForSingleObject(lHnd, WIN32_API_INFINITE) 'no time out
If GetExitCodeProcess(lHnd, nCode) <> 0 Then SyncShellCmd = nCode
CloseHandle (lHnd)
End If
End If
End Function
Private Sub Command1_Click()
Dim nRes As Long
nRes = SyncShellCmd("c:\errLevel.exe", vbHide)
Debug.Print nRes
End Sub
The mehtod OpenProcess always returns 0. If I try to call notepad.exe or calc.exe, it's working fine.
Does anyone have an idea? I would appreciate any hint.