ShellWait()函数

简介

以下代码(模块)提供了执行程序的工具,但可以同步执行。 的

Shell()函数异步执行命令,因此,无论命令是否已完成执行,在调用命令后立即返回控制。 这通常是不合适的或不方便的,因此这是一个使调用代码等待命令终止直到恢复执行的版本。 解决方案
Option Compare Database
Option Explicit 
'Windows API Variable Prefixes
'cb = Count of Bytes (32-bit)
'w  = Word (16-bit)
'dw = Double Word (32-bit)
'lp = Long Pointer (32-bit)
'b  = Boolean (32-bit)
'h  = Handle (32-bit)
'ul = Unsigned Long (32-bit) 
Private Const conUseShowWindow = &H1&
Private Const conNormalPriority = &H20&
Private Const conInfinite = -1& 
Private Type typStartupInfo
    cbLen As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCount As Long
    dwYCount As Long
    dwFillAtt As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdIn As Long
    hStdOut As Long
    hStdErr As Long
End Type 
Private Type typProcInfo
    hProc As Long
    hThread As Long
    dwProcID As Long
    dwThreadID As Long
End Type 
Private Declare Function CreateProcessA Lib "kernel32" ( _
    ByVal lpApplicationName As Long, _
    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 lpCurrentDirectory As Long, _
    lpStartupInfo As typStartupInfo, _
    lpProcessInformation As typProcInfo) As Long
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 
'ShellWait() executes a command synchronously (Shell() works asynchronously).
Public Sub ShellWait(strCommand As String, _
                     Optional intWinStyle As Integer = vbNormalFocus)
    Dim objProcInfo As typProcInfo
    Dim objStart As typStartupInfo 
    'Initialize the typStartupInfo structure:
    With objStart
        .cbLen = Len(objStart)
        .dwFlags = conUseShowWindow
        .wShowWindow = intWinStyle
    End With
    'Start the shelled application:
    Call CreateProcessA(lpApplicationName:=0&, _
                        lpCommandLine:=strCommand, _
                        lpProcessAttributes:=0&, _
                        lpThreadAttributes:=0&, _
                        bInheritHandles:=1&, _
                        dwCreationFlags:=conNormalPriority, _
                        lpEnvironment:=0&, _
                        lpCurrentDirectory:=0&, _
                        lpStartupInfo:=objStart, _
                        lpProcessInformation:=objProcInfo)
    'Wait for the shelled application to finish
    Call WaitForSingleObject(hHandle:=objProcInfo.hProc, _
                             dwMilliseconds:=conInfinite)
    Call CloseHandle(hObject:=objProcInfo.hProc)
End Sub

From: https://bytes.com/topic/access/insights/841878-shellwait-function

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值