since Windows updates occurred an api call to ShellExecuteEx(sExecuteInfo) crashes saying "unhandled exception at 0x75F7A529 (shell32.dll) Access violation reading location 0x68686903". I have no clue what is wrong here, can you help me please ?
Definition :
Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
Hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Private Const STILL_ACTIVE As Long = &H103 ' Constant for the lpExitCode parameter of the GetExitCodeProcess API function.
Private Declare PtrSafe Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpExecInfo As SHELLEXECUTEINFO) As Long
Code:
Private Function ShellAndWait(ByVal szProgram As String, Optional ByVal szOptions As String, Optional ByVal iWindowState As Integer = vbHide) As Boolean
Dim lTaskID As Long
Dim lReturn As Long
Dim lExitCode As Long
Dim lResult As Long
Dim bShellAndWait As Boolean
Dim hInstance As Object
Dim lPriority As Long
On Error GoTo ErrorHandler
Dim sExecuteInfo As SHELLEXECUTEINFO
sExecuteInfo.cbSize = Len(sExecuteInfo)
sExecuteInfo.lpVerb = "open"
sExecuteInfo.lpFile = szProgram
sExecuteInfo.lpParameters = szOptions
sExecuteInfo.nShow = &H7 ' Parameter SW_SHOWMINNOACTIVE, (0x7) , displays the window as a minimized window. The active window remains active.
sExecuteInfo.fMask = &H8140 ' Parameter SEE_MASK_NO_CONSOLE (0x00008000), use to inherit the parent's console for the new process instead of having it create a new console
' Parameter SEE_MASK_NOASYNC (0x00000100), wait for the execute operation to complete before returning.
' Parameter SEE_MASK_NOCLOSEPROCESS (0x00000040), puts process id back in sExecuteInfo.hProcess
lPriority = &H100000 'PROCESS_MODE_BACKGROUND_BEGIN
lReturn = ShellExecuteEx(sExecuteInfo)
'Loop while the shelled process is still running.
Do
'lExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
lResult = GetExitCodeProcess(sExecuteInfo.hProcess, lExitCode)
DoEvents
'iCount = iCount + 1
'Application.StatusBar = Str(iCount) + " iteration waited"
Loop While lExitCode = STILL_ACTIVE
bShellAndWait = True
Exit Function
ErrorHandler:
sErrMsg = Err.Description
bShellAndWait = False
End Function
Use way recommended by Microsoft.
MSDN article "Determine When a Shelled Process Ends" recommends approach through different API call and I can confirm it is working reliably. I'm posting code module (adopted from code in the article) here.
The following VBA module uses API call
CreateProcessA()
, waits for application to finish and returns ERRORLEVEL code as result:If you store all the code as for example
ShellExecModule
, then you can call it as