Om man startar ett program med shell functionen så får man programmets Task id i retur om allt går som det ska. Add a command button to a form along with the following code: Kontrollera om program är igång
Går det att utnyttja denna id för att senare i koden kontrollera om programmet forfarande är igång eller om det har avslutats?Sv: Kontrollera om program är igång
--------------------------------------------------------------------------------
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
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 Const PROCESS_QUERY_INFORMATION = &H400
Private Const STATUS_PENDING = &H103&
Private Sub Command1_Click()
RunShell "c:\windows\notepad.exe"
End Sub
Private Sub RunShell(cmdline As String)
Dim hProcess As Long
Dim ProcessId As Long
Dim exitCode As Long
ProcessId = Shell(cmdline, 1)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, ProcessId)
Do
Call GetExitCodeProcess(hProcess, exitCode)
DoEvents
Loop While exitCode = STATUS_PENDING
Call CloseHandle(hProcess)
MsgBox "The shelled process " & cmdline & " has ended."
End Sub