PDA

Click to See Complete Forum and Search --> : ShellExecute and Wait???


Joey_k29
Jan 8th, 2002, 08:56 AM
At one point in my program, it is necessary to shell a document(of which I do not know the default program to launch it). To avoid this problem, I used the ShellExecute API command. The problem is now, for some reason the processid that is being returned is not successfully working with my code.


processid = ShellExecute(Me.hwnd, vbNullString, pathtofile, commandline, "C:\", vbNormalFocus)

processhandle = OpenProcess(SYNCHRONIZE, 0, processid)



This worked fine before with Shell. It stil returns a non-zero processid, but it no longer establishes a non-zero process handle. How do I fix this? Thank you very much for the help.
Joe

Hack
Jan 8th, 2002, 11:18 AM
Give this a shot...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 Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Const PROCESS_QUERY_INFORMATION = &H400


Private Function ShellAndWait(PathName As String, Optional WS As VbAppWinStyle = vbMinimizedFocus) As Double
'
Dim lhProcess As Long
Dim lExitcode As Long
Dim dProcessID As Double
'
On Error GoTo errShellAndWait

dProcessID = Shell(PathName, WS)
lhProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, dProcessID)
Do
Call Sleep(50): DoEvents
Call GetExitCodeProcess(lhProcess, lExitcode)
Loop While (lExitcode = STILL_ACTIVE)
CloseHandle (lhProcess)
ShellAndWait = dProcessID

Exit Function

errShellAndWait:
If lhProcess <> 0 Then
CloseHandle (lhProcess)
End If
ShellAndWait = dProcessID
End Function

Joey_k29
Jan 8th, 2002, 12:04 PM
It does not work with shell execute. I am not even shell executing a document(which could potentially cause a problem); I am executing an exe. Thanks for the suggestion.

Joe