Using CreateProcessA crashes Access

G

Guest

I'm using some "shell out and wait for it to complete" code I found on the
'net. Actually I found several versions, all of them basically the same. My
version looks like this...

Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplication As
Variant, ByVal lpCommandLine As Variant, ByVal lpProcessAttributes As Long,
ByVal lpThreadAttributes As Long, ByVal bInheritHandle As Integer, ByVal
dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal
lpCurrentDirectory As Variant, ByRef lpStartupInfo As STARTUPINFO, ByRef
lpProcessInfo As PROCESS_INFORMATION) As Integer

Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long,
ByVal dwMilliseconds As Long) As Long

.... then in the sub that handles it...

Dim si As STARTUPINFO
si.cb = Len(si)
si.dwFlags = STARTF_USESHOWWINDOW
si.wShowWindow = SW_SHOWNORMAL

bRet = CreateProcessA(0, lpApp, 0, 0, 0, NORMAL_PRIORITY_CLASS, 0, 0, si,
pi)

Access crashes on the last line, and restarts.

Anyone out there doing something similar and has it working ok?

Maury
 
L

Lorenzo\(pyx\)

Maury said:
I'm using some "shell out and wait for it to complete" code I found
on the 'net. Actually I found several versions, all of them basically
the same. My version looks like this...
[...]

bRet = CreateProcessA(0, lpApp, 0, 0, 0, NORMAL_PRIORITY_CLASS, 0,
0, si, pi)

Access crashes on the last line, and restarts.

Hello,
Try to set As Long the zero value:
bRet = CreateProcessA(0&, lpApp, 0&, 0&, 0&, NORMAL_PRIORITY_CLASS, 0&,
0&, si, pi)

If you want, try also this simple solution, put the code
inside a function, it's launch the calculator program but
you can change.

'=========
Call Shell("cmd /C calc.exe>c:\WaitingEnd.tmp", vbHide)
On Error GoTo WaitForEnd
Kill ("c:\WaitingEnd.tmp")
Exit Function
WaitForEnd:
DoEvents
Resume
'=========

By
 
R

Ron Hinds

Yes, I have it working. It is a set of functions, beginning with one I call
SynchShell:

Public Function SyncShell(CommandLine As String, Optional Timeout As Long =
0, _
Optional WaitForInputIdle As Boolean = False, Optional Hide As Boolean =
False) As Boolean

Dim hProcess As Long

Dim ret As Long
Dim nMilliseconds As Long

If Timeout > 0 Then
nMilliseconds = Timeout
Else
nMilliseconds = INFINITE
End If

hProcess = StartProcess(CommandLine, Hide)

If WaitForInputIdle Then
'Wait for the shelled application to finish setting up its UI:
ret = InputIdle(hProcess, nMilliseconds)
Else
'Wait for the shelled application to terminate:
ret = WaitForSingleObject(hProcess, nMilliseconds)
End If

CloseHandle hProcess

'Return True if the application finished. Otherwise it timed out or
erred.
SyncShell = (ret = WAIT_OBJECT_0)

End Function

Here is Start Process:

Public Function StartProcess(CommandLine As String, Optional Hide As Boolean
= False) As Long

Const STARTF_USESHOWWINDOW As Long = &H1
Const SW_HIDE As Long = 0

Dim Proc As PROCESS_INFORMATION
Dim Start As STARTUPINFO

'Initialize the STARTUPINFO structure:
Start.cb = Len(Start)
If Hide Then
Start.dwFlags = STARTF_USESHOWWINDOW
Start.wShowWindow = SW_HIDE
End If
'Start the shelled application:
CreateProcessA 0&, CommandLine, 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, Start, Proc

StartProcess = Proc.hProcess

End Function

You also need these Decalres:

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hProcess
As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function InputIdle Lib "user32" Alias "WaitForInputIdle"
(ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long)
As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal
lpApplicationName As Long, 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 Long, lpStartupInfo As STARTUPINFO,
lpProcessInformation As PROCESS_INFORMATION) As Long

Private Const NORMAL_PRIORITY_CLASS As Long = &H20&
Private Const INFINITE As Long = -1&

Private Const STATUS_WAIT_0 As Long = &H0
Private Const WAIT_OBJECT_0 As Long = STATUS_WAIT_0

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 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top