Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing arguments and returns between c# application and c++ exe file

Tags:

c++

c#

I want to call an c++ exe file into my c# application that takes a command line argument and returns the result so that i can use it in my c# application but i don't know how to do it .

here's the simple sample that i tried and failed : c++ code : returner.exe

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

c# code :

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

i expect to see 3 in the lable . but it's always 0 . what should i do ?

like image 755
arman radan Avatar asked Sep 02 '25 16:09

arman radan


1 Answers

The signature of your main is incorrect, it should be:

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;
}
like image 133
Yakov Galka Avatar answered Sep 05 '25 07:09

Yakov Galka