Re: Execute batch and wait
I am also having this problem and could not get solution anywhere. I am not discouraging you but requesting you to please pass the solution if you get.
Re: Execute batch and wait
This supposedly works, but I haven't gotten it to work for me. I think I may be putting the code in the wrong place. Let me know how it works out. I tried putting it into a module and on the form, but neither helps so I'm not sure what else I need. Let me know how it works out.
private Const PROCESS_ALL_ACCESS = &H1F0FFF
private Declare Function GetWindowText Lib "user32" _
Alias "GetWindowTextA" (byval hWnd as Long, byval lpString as _
string, byval cch as Long) as Long
private Declare Function GetWindowTextLength Lib "user32" _
Alias "GetWindowTextLengthA" (byval hWnd as Long) as Long
private Declare Function GetNextWindow Lib "user32" _
Alias "GetWindow" (byval hWnd as Long, byval wFlag as Long) _
as Long
private Declare Function OpenProcess Lib "kernel32" (byval dwDesiredAccess as Long, _
byval bInheritHandle as Long, byval dwProcessId as Long) as Long
private Declare Function GetWindowThreadProcessId Lib "user32" _
(byval hWnd as Long, lpdwProcessId as Long) as Long
private Declare Function GetExitCodeProcess Lib "kernel32" _
(byval hProcess as Long, lpExitCode as Long) as Long
private Declare Function TerminateProcess Lib "kernel32" _
(byval hProcess as Long, byval uExitCode as Long) as Long
Dim hWnd as Long
Dim hInst as Long
Dim hProcess as Long
Dim lProcessID
Dim bAns as Boolean
Dim lExitCode as Long
Dim lRet as Long
public Function EndAllInstances(byval WindowCaption as string) _
as Boolean
'*********************************************
'PURPOSE: ENDS ALL RUNNING INSTANCES OF A PROCESS
'THAT CONTAINS ANY PART OF THE WINDOW CAPTION
'input: ANY PART OF THE WINDOW CAPTION
'RETURNS: true IF SUCCESSFUL (AT LEASE ONE PROCESS WAS STOPPED,
'false OTHERWISE)
'EXAMPLE EndProcess "Notepad"
'NOTES:
'1. THIS is DESIGNED to TERMINATE THE PROCESS IMMEDIATELY,
' THE APP WILL NOT RUN THROUGH IT'S NORMAL SHUTDOWN PROCEDURES
' E.G., THERE WILL BE NO DIALOG BOXES LIKE "ARE YOU SURE
' YOU WANT to QUIT"
'2. BE CAREFUL WHEN USING:
' E.G., IF YOU CALL ENDPROCESS("A"), ANY PROCESS WITH A
' WINDOW THAT HAS THE LETTER "A" IN ITS CAPTION WILL BE
' TERMINATED
'3. as WRITTEN, ALL THIS CODE MUST BE PLACED WITHIN
' A FORM MODULE
'***********************************************
on error GoTo ErrorHandler
If Trim(WindowCaption) = "MIMI - TESTMAST" then Exit Function
Do
hWnd = FindWin(WindowCaption)
If hWnd = 0 then Exit Do
hInst = GetWindowThreadProcessId(hWnd, lProcessID)
'get handle to process
hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0&, lProcessID)
If hProcess <> 0 then
'get exit code
GetExitCodeProcess hProcess, lExitCode
If lExitCode <> 0 then
'bye-bye
lRet = TerminateProcess(hProcess, lExitCode)
If bAns = false then bAns = lRet > 0
End If
End If
Loop
EndAllInstances = bAns
ErrorHandler:
End Function
private Function FindWin(WinTitle as string) as Long
Dim lhWnd as Long, sAns as string
Dim sTitle as string
lhWnd = me.hWnd
sTitle = LCase(WinTitle)
Do
DoEvents
If lhWnd = 0 then Exit Do
sAns = LCase$(GetCaption(lhWnd))
If InStr(sAns, sTitle) then
FindWin = lhWnd
Exit Do
else
FindWin = 0
End If
lhWnd = GetNextWindow(lhWnd, 2)
Loop
End Function
private Function GetCaption(lhWnd as Long) as string
Dim sAns as string, lLen as Long
lLen& = GetWindowTextLength(lhWnd)
sAns = string(lLen, 0)
Call GetWindowText(lhWnd, sAns, lLen + 1)
GetCaption = sAns
End Function
Re: Execute batch and wait
My fault. I was thinking of something else. That code I just posted is a way to close an application. Which I am still unable to get working. The code I psoted initially is calling something that needs to be declared in a module. Again, I am not sure how and will welcome any suggestions.