4

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 ?

6
  • 1
    Perhaps a C++-made DLL would better suit your purposes. Commented Jan 1, 2013 at 19:16
  • 2
    Is your C++ code even valid? That main signature is not standard. Commented Jan 1, 2013 at 19:19
  • Have you, maybe, considered debugging at all? What happens if your C++ main explicitly returns '3', ie. ignores the 'main' parameters, (which I'm not at all happy with - see Mat comment). Commented Jan 1, 2013 at 19:23
  • I'm not an expert in c++ so if you know how to write c++ code tell me . yes if my c++ code explicitly returns 3 i would see 3 in label . Commented Jan 1, 2013 at 19:28
  • int main(int argc, const char** argv) -> this is a correct prototype for the main function. From that you should perhaps be able to do something Commented Jan 1, 2013 at 19:30

1 Answer 1

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.