Unable to open Shell process handle
In Excel I have a button that when I click it shell's out to a .exe file someone else made. I used the API function ShellExecute because I wanted to specify parameters.
I want to wait until the .exe file is done running until I continue on with my code. So I have been trying to use OpenProcess and GetExitCodeProcess API functions. The problem is whenever I try to get the .exe file's process handle it comes back as 0. I am not sure what the problem is. Any ideas would be greatly appreciated.
Thanks!
Re: Unable to open Shell process handle
You may also use the WaitForSingleObject API to do this, but post your code and lets see what may be going on.
Re: Unable to open Shell process handle
Here is my code...
VB Code:
Function RunRe_Synch() As Boolean
Dim lTaskID As Long
Dim lProcess As Long
Dim lExitCode As Long
Dim lResult As Long
Dim File, Operation, Parameters, Directory As String
File = "C:\ReSynch\WorksetVersion\executable\Re_Synch.exe"
Operation = "Open"
Parameters = "C:\ReSynch\WorksetVersion\executable\"
Directory = "C:\ReSynch\WorksetVersion\executable\"
'Run the Shell Function
lTaskID = ShellExecute(Application.hwnd, Operation, File, Parameters, Directory, 1)
'Check for errors.
If lTaskID = 0 Then MsgBox ("Shell function error.")
'Get the process handle from the task ID returned by Shell.
lProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0&, lTaskID)
'Check for errors.
If lProcess = 0 Then MsgBox ("Unable to open Shell process handle.")
'Loop while the shelled process is still running.
Do
'ExitCode will be set to STILL_ACTIVE as long as the shelled process is running.
lResult = GetExitCodeProcess(lProcess, lExitCode)
DoEvents
Loop While lExitCode = STILL_ACTIVE
RunRe_Synch = True
End Function
The error catches arn't done... I just have message boxes popping up now. The problem I have is that my lProcess equals 0 everytime I run my program.
Re: Unable to open Shell process handle
The lTaskID being returned from ShellExecute is not really the process id.
Quote:
Return Values
If the function succeeds, the return value is the instance handle of the application that was run.
If the function fails, the return value is an error value that is less than or equal to 32.