Perdoname, pero lo que estas haciendo es totalmente desprolijo y derrochas un monton de recursos al ejecutar un script.
Hace otra aplicacion, que reciba como parametro el nombre del ".exe" que queres "escuchar". Aca te dejo una funcion que "espera" que terime un EXE arbitrario y luego sigue ejecutando lo que esta abajo...
Pega el codigo en un formulario y en el Form_Load llama a "WaitForExe(Command$)" y luego "Shell Command$" y finalmente "End". Pone que el form no sea visible.
(Tenes que hacerlo en un form porque si lo haces en un modulo usando el Sub Main los antivirus te van a botonear el exe).
Private Const TH32CS_SNAPHEAPLIST = &H1
Private Const TH32CS_SNAPPROCESS = &H2
Private Const TH32CS_SNAPTHREAD = &H4
Private Const TH32CS_SNAPMODULE = &H8
Private Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)
Private Const TH32CS_INHERIT = &H80000000
Private Const MAX_PATH As Integer = 260
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const SYNCHRONIZE As Long = &H100000
Private Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF)
Private Const INFINITE = &HFFFF
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH
End Type
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Private Declare Function Process32First Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "Kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function WaitForSingleObject Lib "kernel32.dll" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Sub CloseHandle Lib "Kernel32" (ByVal hPass As Long)
Private Function GetPIDFromName(ByVal sName As String, _
ByRef lPID As Long) As Boolean
Dim lSnap As Long
Dim pProcess As PROCESSENTRY32
Dim lReturn As Long
Dim sExeName As String
lSnap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0&)
With pProcess
.dwSize = Len(pProcess)
lReturn = Process32First(lSnap, pProcess)
Do While lReturn
sExeName = Left$(.szExeFile, IIf(InStr(1, .szExeFile, Chr$(0)) > 0, InStr(1, .szExeFile, Chr$(0)) - 1, 0))
If StrComp(sExeName, sName, vbTextCompare) = 0 Then
GetPIDFromName = True
lPID = .th32ProcessID
Call CloseHandle(lSnap)
Exit Function
End If
lReturn = Process32Next(lSnap, pProcess)
Loop
End With
Call CloseHandle(lSnap)
End Function
Private Function WaitForExe(ByVal sName As String) As Boolean
Dim lPID As Long
Dim lProcHandle As Long
If Not GetPIDFromName("notepad.exe", lPID) Then
Exit Function
End If
lProcHandle = OpenProcess(SYNCHRONIZE, 0, lPID)
If lProcHandle = 0 Then
Exit Function
End If
Call WaitForSingleObject(lProcHandle, INFINITE)
Call CloseHandle(lProcHandle)
WaitForExe = True
End Function