Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running executable programs in VB 2

Status
Not open for further replies.

davideart

Programmer
Apr 4, 2001
55
IT
Hi, everybody.

I need to run a sequence of programs (DOS batch) form a VB application. The problem is that I need to launch these bat files in fixed sequence, whereas the VB SHELL function doesen't wait for a program to be finished before launching the next one.

So if I write something like:

for i=1 to NumberOfBat
Shell Filename(i).bat
next

the filename(3).bat will be launched before the filename(2).bat is finished, and this gives me problems.

Does anyone how I can check that a program is finished?

Can I use the SHELL function or have I to use a different statement (if so, what statement?)

Thank you in advance to anyone who can give me some advice.

G'day
 
Check out faq222-42. It illustrates a ShellAndWait procedure.
 
The following is an example of a ShellAndWait function that can replace the Shell function. it takes and returns the same parameters (note, though, that the returned TaskID is useless since the task is closed by the time the function returns)

Option Explicit

Public Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Public Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessId As Long
dwThreadId As Long
End Type

Private Const NORMAL_PRIORITY_CLASS = &H20&
Public Const STARTF_USESHOWWINDOW = &H1


Public Declare Sub GetStartupInfo Lib "kernel32" Alias "GetStartupInfoA" (lpStartupInfo As STARTUPINFO)
Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long




Public Function ShellAndWait(ByVal strProgram As String, ByVal Windowmode As Long) As Variant
Dim SUI As STARTUPINFO
Dim PI As PROCESS_INFORMATION
Dim Success As Boolean


SUI.cb = Len(SUI)
SUI.dwFlags = STARTF_USESHOWWINDOW
SUI.wShowWindow = Windowmode
Success = CreateProcess(strProgram, vbNullString, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, vbNullString, SUI, PI)
If Success = True Then
Call WaitForSingleObject(PI.hProcess, -1) ' -1 means wait forever, if necessary

' Belts and braces cleanup
CloseHandle PI.hThread
CloseHandle PI.hProcess
ShellAndWait = CDbl(PI.dwProcessId)
Else
MsgBox "Failed to shell program " + strProgram
ShellAndWait = CDbl(0)
End If
End Function
 
ok, you have been all very helpful!
Thanks a lot, I think I have everything I need now.

Bye
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top