Click to See Complete Forum and Search --> : Execute batch and wait


Reese
April 28th, 2000, 09:12 AM
I am trying to execute a batch. I believe I can do this. The only thing is that the batch initiates operations within in a file that will run independently of the batch. I have the code that will execute the batch and pause the rest of the program until the batch is complete but that doesn't help me since it doesn't know when the operations inside the file are complete. The type of file is an Aspentech MIMI case file. The bat file the I am executing within my program will execute the $batch macro inside of the MIMI case file. Does anyone know how to tell when the operation inside of MIMI is complete?
Here is my code thus far which I believe is correct ssince I found it here.

public sub RunBat()
shell "\\BatPath", vbNormalFocus
End Sub

Declare Function WaitForSingleObject Lib "kernel32" (byval hHandle as Long, byval dwMilliseconds as Long) as Long
Declare Function OpenProcess Lib "kernel32" (byval dwDesiredAccess as Long, byval bInheritHandle as Long, byval dwProcessId as Long) as Long

public Const INFINITE = -1

public Sub ShellWait(byval FileName as string, optional WindowStyle as Variant)

Dim idProc as Long
Dim hProc as Long

' start program and save PID
idProc = VBA.Shell(FileName, WindowStyle)
' get process handle
hProc = OpenProcess(PROCESS_ALL_ACCESS, false, idProc)
'wait till program has finished
If hProc <> hNull then
Call WaitForSingleObject(hProc, INFINITE)
Call CloseHandle(hProc)
End If

End Sub

Yamini
April 29th, 2000, 07:12 AM
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.

Reese
May 1st, 2000, 08:13 AM
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

Reese
May 1st, 2000, 09:59 AM
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.