• 使用API调用外部程序并监控程序状态


    Public 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
        lpIDList As Long
        lpClass As String
        hkeyClass As Long
        dwHotKey As Long
        hIcon As Long
        hProcess As Long
    End Type

    Public Const SEE_MASK_NOCLOSEPROCESS = &H40
    Public Const SW_SHOWNORMAL = 1

    Public Declare Function ShellExecuteEx Lib "shell32.dll" Alias "ShellExecuteExA" _
    (lpExecInfo As SHELLEXECUTEINFO) As Long
       
    Public Const SE_ERR_FNF = 2
    Public Const SE_ERR_NOASSOC = 31

    Public Declare Function WaitForSingleObject Lib "kernel32.dll" _
    (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
       
    Public Const INFINITE = &HFFFF
    Public Const WAIT_TIMEOUT = &H102

    Sub Upload1()
        Dim sei As SHELLEXECUTEINFO  ' structure used by the function
        Dim retval As Long  ' return value
       
        ' Load the information needed to open C:Bootlog.txt into the structure.
        With sei
            ' Size of the structure
            .cbSize = Len(sei)
            ' Use the optional hProcess element of the structure.
            .fMask = SEE_MASK_NOCLOSEPROCESS
            ' Handle to the window calling this function.
    '        .hwnd = Me.hwnd
            ' The action to perform: open the file.
            .lpVerb = "open"
            ' The file to open.
            .lpFile = strPath + "IT3CW32.EXE"
            ' No additional parameters are needed here.
            .lpParameters = strPath + "CARTONS.DAT +R +E +V"
            ' The default directory -- not really necessary in this case.
            .lpDirectory = ""
            ' Simply display the window.
            .nShow = SW_SHOWNORMAL
            ' The other elements of the structure are either not used
            ' or will be set when the function returns.
        End With
       
        ' Open the file using its associated program.
        retval = ShellExecuteEx(sei)
        If retval = 0 Then
            ' The function failed, so report the error.  Err.LastDllError
            ' could also be used instead, if you wish.
            Select Case sei.hInstApp
            Case SE_ERR_FNF
                MsgBox "Program not found."
            Case SE_NOASSOC
                MsgBox "No associate"
            Case Else
                MsgBox "Unexpected Error"
            End Select
        Else
            ' Wait for the opened process to close before continuing. Instead of waiting once
            ' for a time of INFINITE, this example repeatedly checks to see if the process
            ' is still open.  This allows the DoEvents VB function to be called, preventing
            ' our program from appearing to lock up while it waits.
            Do
                DoEvents
                retval = WaitForSingleObject(sei.hProcess, 0)
            Loop While retval = WAIT_TIMEOUT
            ' MsgBox "Operation Completed"
            bUpload = True
        End If
    End Sub

  • 相关阅读:
    程序员自我【营销】,如何打造个人【品牌】
    程序员应该怎样和领导相处?
    程序员必备能力——晋升之道
    聊一聊 软件系统中的“热力学第二定律”
    程序员如何利用技术管理技巧
    技术人必须掌握能力——深度思考
    程序员逆袭之路——系列文章更新中
    程序员跳槽,该如何选择一家好公司
    C++-运行时类型信息,异常(day11)
    C++-多态,纯虚函数,抽象类,工厂模式,虚析构函数(day10)
  • 原文地址:https://www.cnblogs.com/lbnnbs/p/4784603.html
Copyright © 2020-2023  润新知