gpt4 book ai didi

c++ - 在 VB 和 C++ 之间控制程序流

转载 作者:太空宇宙 更新时间:2023-11-04 11:51:04 24 4
gpt4 key购买 nike

我有一个从 VB/Excel 运行的程序,并在其中执行一个 C++ 程序。我有两个(我认为相关的)问题:

  1. 我从 C++ 程序执行时捕获返回值,但我得到的数字不是零(而是一个 4 位整数值,我收到的示例值是 8156、5844、6100 , 5528).我确定程序正常退出时代码为 0,但我认为 VB 在 C++ 程序完成执行之前获得了它的值——这可以解释为什么我没有获得零值,以及我如何获得最终的、正确的从我的 C++ 程序返回值?

  2. [可能作为 #1 的解决方案] 如何让 VB 程序“暂停”,直到 C++ 模型完成执行?模型完成后,我需要做一些额外的 VB 工作(基于 C++ 模型运行的输出配置)

这是我的模型调用方式的 VB 代码。我正在通过 Windows shell 运行一个完整编译的 C++ 程序。

'---------------------------------------------------------
' SECTION III - RUN THE MODEL AS C++ EXECUTABLE
'---------------------------------------------------------
Dim ModelDirectoryPath As String
Dim ModelExecutableName As String
Dim ModelFullString As String
Dim ret As Long

ModelDirectoryPath = Range("ModelFilePath").value
ModelExecutableName = Range("ModelFileName").value
ModelFullString = ModelDirectoryPath & ModelExecutableName

' Call the model to run
Application.StatusBar = "Running C Model..."
ModelFullString = ModelFullString & " " & ScenarioCounter & " " & NumDeals _
& " " & ModelRunTimeStamp
ret = Shell(ModelFullString)

' Add error checking based on return value
' This is where I want to do some checks on the return value and then start more VB code

最佳答案

1) 您正在捕获程序的任务 ID(这是 Shell() 返回的内容)而不是打开程序的任何返回 - 这就是为什么它是一个 4 位数字

2) Shell() 异步运行所有程序。

要同步运行程序或运行它并等待返回,要么:

基本上,做类似的事情:

Set o = CreateObject("WScript.Shell")
valueReturnedFromYourProgram = o.Run( _
strCommand:="notepad", _
intWindowStyle:=1,
bWaitOnReturn:=true)
Debug.Print valueReturnedFromYourProgram

关于c++ - 在 VB 和 C++ 之间控制程序流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18213357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com