Search code examples
c#c++command-lineargvargc

C# Call an app using the command line


In my C++ App, which is an .exe, I have this:

int main(int argc, char** argv){
--argc;
++argv;
if (argc != 1){
    throw std::exception("Bad command line.");
}

E.t.c

But how would I call that in my C# (WPF) app? I tried System.Diagnostics.Process.Start("pathofapp.exe", "stringiwantedtouse"), but I got Bad Command Line. What should I do?


Solution

  • You should try using the c++ program like this:

    int main ( int argc, char *argv[] )
    {
        if (argc != 1){
            throw std::exception("Bad command line.");
        }
    }
    

    And then use argv[0] to access it's first parameter.

    For example:

    In C++:

    int main ( int argc, char *argv[] )
    {
        if (argc != 1){
            throw std::exception("Bad command line.");
        }else{
            std::cout << "Param 1: " << argv[0] << std::endl;
        }
    }
    

    In C#:

    System.Diagnostics.Process.Start("pathofapp.exe", "this");
    

    Output:

    Param 1: this