c# - 在 c# 应用程序和 c++ exe 文件之间传递参数和返回

标签 c# c++

我想在我的 c# 应用程序中调用一个 c++ exe 文件,它接受一个命令行参数并返回结果,以便我可以在我的 c# 应用程序中使用它,但我不知道该怎么做。

这是我尝试过但失败的简单示例: C++ 代码:returner.exe

#include<iostream>
#include<cstdlib>
using namespace std;
int main(string argc , string argv)
{
    int b= atoi(argv.c_str());
    return b;
}

C# 代码:

 private void button1_Click(object sender, EventArgs e)
        {
            ProcessStartInfo stf = new ProcessStartInfo("returner.exe", "3");
            stf.RedirectStandardOutput = true;
            stf.UseShellExecute = false; 
            stf.CreateNoWindow = true;

            using (Process p = Process.Start(stf))
            {
                p.WaitForExit();
                int a = p.ExitCode;
                label1.Text = a.ToString();
            }
        }

我希望在标签中看到 3 个。但它总是 0 。我该怎么办?

最佳答案

你的main签名不正确,应该是:

int main(int argc, char *argv[])
{
    // you are better to verify that argc == 2, otherwise it's UB.
    int b= atoi(argv[1]);
    return b;
}

关于c# - 在 c# 应用程序和 c++ exe 文件之间传递参数和返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14113384/

相关文章:

c# - 未处理的异常,MySql.Data.MySqlClient.MySqlException

c++ - 在不使用双端队列的情况下弹出队列的后面

c++ - 如何最好地保存 XML 文件

c++ - 属性如何传递给 GLSL 中的顶点着色器?

c++ - 如何将代码更改为交换指针而不是使用 "temp"指针?

c# - 上传到 Azure 文件存储因大文件而失败

C#/XNA 巨大的内存泄漏

c# - 如何使用这些部件在 C# 中可靠地构建 URL?

c++ - Eigen::Ref<Mat<T>> 的模板参数推导

c# - 将 Linq-To-SQL 列表导入 MongoDB