VBA Shell 并等待退出代码 [英] VBA Shell and Wait with Exit Code

查看:59
本文介绍了VBA Shell 并等待退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在打包一个办公应用程序 (VBA),它调用 C# 控制台应用程序来执行应用程序(大型模拟程序)的一些繁重工作.我希望能够让 VBA 应用程序等待控制台应用程序完成并从控制台应用程序中检索退出代码.我已经能够做到前者,但还没有能够从应用程序中检索退出代码.有什么办法可以使用像

I am wrapping up an office application (VBA) that makes a call to a C# console application to perform some of the heavy lifting for the application (large simulation program). I would like to be able to have the VBA application wait for the console application to complete as well as retreive the exit code from the console application. I have been able to do the former, but have yet to be able to retrieve the exit code from the application. Is there any way that I can use something like

Diagnostics.Process.Start(filePath)

我在 VB 中看到过这个,但不确定 VBA.否则,还有其他建议吗?

I have seen this in VB but not sure about VBA. Otherwise, any other suggestions?

推荐答案

查看 WaitForSingleObjectGetExitCodeProcess 函数.

Have a look at WaitForSingleObject and GetExitCodeProcess functions.

示例用法:

Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long

Public Const INFINITE = &HFFFF
Public Const PROCESS_ALL_ACCESS = &H1F0FFF

Sub RunApplication(ByVal Cmd as String)

    lTaskID = Shell(Cmd, vbNormalFocus)
    ' Get process handle
    lPID = OpenProcess(PROCESS_ALL_ACCESS, True, lTaskID)
    If lPID Then
        ' Wait for process to finish
        Call WaitForSingleObject(lPID, INFINITE)
        ' Get Exit Process
        If GetExitCodeProcess(lPID, lExitCode) Then
            ' Received value
            MsgBox "Successfully returned " & lExitCode, vbInformation
        Else
            MsgBox "Failed: " & DLLErrorText(Err.LastDllError), vbCritical
        End If
    Else
        MsgBox "Failed: " & DLLErrorText(Err.LastDllError), vbCritical
    End If
    lTaskID = CloseHandle(lPID)
End Sub

Public Function DLLErrorText(ByVal lLastDLLError As Long) As String
    Dim sBuff As String * 256
    Dim lCount As Long
    Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100, FORMAT_MESSAGE_ARGUMENT_ARRAY = &H2000
    Const FORMAT_MESSAGE_FROM_HMODULE = &H800, FORMAT_MESSAGE_FROM_STRING = &H400
    Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000, FORMAT_MESSAGE_IGNORE_INSERTS = &H200
    Const FORMAT_MESSAGE_MAX_WIDTH_MASK = &HFF

    lCount = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM Or FORMAT_MESSAGE_IGNORE_INSERTS, 0, lLastDLLError, 0&, sBuff, Len(sBuff), ByVal 0)
    If lCount Then
        DLLErrorText = Left$(sBuff, lCount - 2) ' Remove line feeds
    End If

End Function

这篇关于VBA Shell 并等待退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆