Runs an executable synchronously. See VB Shell documentation for more info.
- Syntax
-
ShellModal PathName, WindowStyle
Call ShellModal(PathName, WindowStyle)
Part
|
Description
|
| PathName |
Required text that names the executable and any command line parameters.
|
| WindowStyle |
Optional integer corresponding to the style
of the window in which the program is to be run.
If omitted, the program is started minimized with focus.
See Shell documentation that comes with VB for more WindowSTyle settings.
|
Remarks
Unlike the VB Shell instruction, ShellModal executes the command synchronously.
That means execution does not move to the instruction following ShellModal
until after the command completes.
See Shell documentation that comes with VB for more info.
Example
This example writes DOS instructions into a batch file,
runs it and then displays the results in Notepad.
hFile = FreeFile
Open "Temp_Dos.Bat" For Output As hFile
Print #hFile, "C:"
Print #hFile, "CD \Windows"
Print #hFile, "Dir > Win_Dir.Txt"
Close hFile
Call ShellModal("Temp_Dos.Bat", vbMinimizedFocus)
Call ShellModal("Notepad C:\Windows\Win_Dir.Txt", vbNormalFocus)
Source
#If Win32 Then ' 32-bit VB uses this Declare.
Const STILL_ACTIVE = &H103
Const PROC_QUERY_INFO = &H400
Declare Function SmOpenProcess Lib "kernel32" _
Alias "OpenProcess" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Declare Function SmGetExitCodeProcess Lib "kernel32" _
Alias "GetExitCodeProcess" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
Declare Sub SmSleep Lib "kernel32" _
Alias "Sleep" _
(ByVal dwMilliseconds As Long)
Declare Function SmCloseHandle Lib "kernel32" _
Alias "CloseHandle" _
(ByVal hObject As Long) As Long
#Else ' 16-bit VB uses this Declare.
Declare Function SmGetModuleUsage Lib "Kernel" _
Alias "GetModuleUsage" _
(ByVal hModule As Integer) As Integer
#End If
Sub ShellModal(ByVal PathName As String, _
Optional ByVal WindowStyle As Variant)
#If Win32 Then ' 32-bit VB for 95 & NT
Dim ProcessId As Long
Dim hProcess As Long
Dim ExitCode As Long
ProcessId = Shell(PathName, WindowStyle)
hProcess = SmOpenProcess(PROC_QUERY_INFO, False, ProcessId)
Do
SmGetExitCodeProcess hProcess, ExitCode
'Uncomment DoEvents if you want to
'allow other events in this app.
'Don't werry about other apps.
'They will run normally since 95 & NT
'are preemptive unlike 3.1.
'DoEvents
SmSleep 500 '1000 = 1 second.
Loop While ExitCode = STILL_ACTIVE
SmCloseHandle hProcess
#Else ' 16-bit VB for Windows 3.1.
'I cannot prevent other events from processing
'since this procedure requires a DoEvents for 3.1
'so it's not completely modal.
Dim hInstance As Integer
hInstance = Shell(PathName, WindowStyle)
Do
DoEvents
Loop Until SmGetModuleUsage(hInstance) = 0
#End If
End Sub
|