在c#application和c ++ exe文件之间传递参数和返回

时间:2013-01-01 19:14:28

标签: c# c++

我想将c ++ exe文件调用到我的c#应用程序中,该应用程序接受命令行参数并返回结果,以便我可以在我的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#c​​ode:

 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。我该怎么办?

1 个答案:

答案 0 :(得分:3)

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;
}