wormboy__6
asked on
shelling a file
How can i shell i file, then execute commands after the file has closed that i have shelled?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks
Thanks for the points! Glad I could help!
Cheers!
Cheers!
Global ProgHandle As Long
Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
Declare Function SysSetFocus Lib "user32" Alias "SetFocus" _
(ByVal hwnd As Long) As Long
Function IsActive(hprog) As Long
Dim hProc, RetVal As Long
hProc = OpenProcess(0, False, hprog)
If hProc <> 0 Then GetExitCodeProcess hProc, RetVal
IsActive = (RetVal = 259)
CloseHandle hProc
End Function
--------------------------
When you shell the program, do it like this, (remember ProgHandle is a global definition):
ProgHandle = Shell("notepad.exe", vbNormalFocus)
You can then make the following call to see if the shelled process is still running:
If IsActive(ProgHandle) Then
'THE SHELLED PROGRAM IS ACTIVE
'DO WHATEVER YOUR GOING TO DO
Else
'THE SHELLED PROGRAM IS NOT ACTIVE
'DO WHATEVER YOUR GOING TO DO
End If
The "If Active" can be put in a loop, so that you poll the shelled program to see if it has terminated instead of freezing your app completely until the shell terminates.
You can do it like this:
--------------------------
Do
If IsActive(ProgHandle) Then
DoEvents
Else
MsgBox "Execution completed"
Exit Do
End If
Loop
'-------------------------
Remember, if you do it this way instead of the 1st answer I gave, you program events will still fire... So it would be a good idea to disable the button or menu item you use to get into this code before you start the DO...Loop.
You can then enable it again when the loop is complete. That way you don't get multiple instanciations of the shell.
Cheers!