Shell 调用程序后等待该程序结束后返回继续 | 雨律在线

方法1:

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function
CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function
ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" (lpInfo As Any) As Long

Private
Type SHELLEXECUTEINFO
cbSize
As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
' Optional members
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon_OR_Monitor As Long
hProcess As Long
End
Type

Private Sub Form_Load()
Dim si As SHELLEXECUTEINFO
si.cbSize = Len(si)
si.lpVerb =
"open"
si.lpFile = "notepad.exe"
si.lpParameters = ""
si.lpDirectory = ""
si.nShow = 5 'SW_SHOW
si.fMask = &H40 'SEE_MASK_NOCLOSEPROCESS
ShellExecuteEx si
If si.hProcess <> 0 Then
WaitForSingleObject si.hProcess, &HFFFFFFFF ' 无限等待, 直到程式结束
CloseHandle si.hProcess
MsgBox
"程序运行完毕!"
End If
End Sub


方法2:

Public Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function
WaitForSingleObject Lib "kernel32" Alias "WaitForSingleObject" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function
CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long
Dim
lngPId As Long
Dim
lngPHandle As Long
lngPId = Shell("Notepad", vbNormalFocus)
lngPHandle = OpenProcess(SYNCHRONIZE,
0, lngpId)
If lngPHandle <> 0 Then
Call
WaitForSingleObject(lngPHandle, INFINITE) ' 无限等待, 直到程式结束
Call CloseHandle(lngPHandle)
End If


方法3:

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function
GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function nt>CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Const
PROCESS_QUERY_INFORMATION = &H400

Const STILL_ALIVE = &H103

Private Sub Command1_Click()
Dim pid As Long
pid = Shell("c:\a.bat", vbNormalFocus)
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION,
0, pid)
Do
Call
GetExitCodeProcess(hProcess, ExitCode)
DoEvents
Loop While ExitCode = STILL_ALIVE
Call CloseHandle(hProcess)
MsgBox (
"运行结束")
End Sub


 
目前有0条回应
Comment
Trackback
你目前的身份是游客,请输入昵称和电邮!