zoukankan      html  css  js  c++  java
  • 使用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

  • 相关阅读:
    VS2013专业版+QT5.6.3+qt-vs-addin-1.2.5环境搭建
    提权获取进程路径并获取进程列表
    解决Qt发布的程序在xp环境下提示“无法定位程序输入点 K32GetModuleFileNameExA 于动态链接库 KERNEL32.dll 上”的错误
    QT5中使用Echarts图表组件
    Qt5.9生成dll详细图文教程
    Qt 编程指南 & 选择合适的Qt5版本
    Qt 之 国际化(中英文切换)
    Qt资料
    第三次作业
    第二次作业
  • 原文地址:https://www.cnblogs.com/lbnnbs/p/4784603.html
Copyright © 2011-2022 走看看