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 ?
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With