Shell 调用程序后等待该程序结束后返回继续

本文介绍使用VBScript通过三种不同的方法来实现进程同步,包括利用SHELLEXECUTEINFO结构体、OpenProcess函数结合WaitForSingleObject及GetExitCodeProcess函数配合循环检查进程退出状态码的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法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
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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值