我想在我的 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/