2

I am trying to make a C++ app, which returns a result to the C# app, which can be displayed, or maybe printed to the console.

For example, in my C++ file:

int main(int argc, char* argv[]){
    // code
    some_method_to_return_result("result");
}

And in my C# app

System.Diagnostics.Process.Start("exec.exe", "arguments");
some_method_to_get_result();

How would I be able to achieve this?

5 Answers 5

4

The best approach is to organize your C++ app as a dll and call this dll from the .NET process (C#). In this case you will have just one process. Calls from C# to C++ are very effective. Marshalling data in some cases can be tricky. In simple cases it is straightforward. Keep your data formats simple.

Prototype of your C++ function should look in C# like this:

[DllImport("avifil32.dll")]
private static extern void AVIFileInit();

Search for C# dllimport and C# extern for details.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you're slightly confused about what information is available when you spawn a process in C#. You can't just directly call a C++ method you need a bunch of ugly interop code that I don't know about. What you can get from your C++ app is main's return code (either 1 or 0). Something like the following will make it so result holds the return status of your C++ app after it's execution has completed.

Process P = System.Diagnostics.Process.Start("exec.exe", "arguments");
P.WaitForExit();
int result = P.ExitCode;

If you want to get other data from your C++ app you need to do some research on Marshalling which I'm not really knowledgeable on but I think this msdn article might be a good starting point; http://msdn.microsoft.com/en-us/library/eaw10et3(v=vs.110).aspx

Comments

0

In C++ write your result to standard output:

std::cout << "result";

In C# use Process.StandardOutput.ReadLine or similar way to grab the standard output of C++ application.

Comments

0

Maybe not the best solution, but probably the easiest would be to write to some file in your c++ code, and read from it in your c# code:

//c++
int main(int argc, char* argv[]){
  ofstream fileToWrite;
  fileToWrite.open ("file.txt");
  fileToWrite << some_method_to_return_result("result");
  fileToWrite.close();
}

//c#
string data;
    using (StreamReader reader = new StreamReader("file.txt"))
    {
        data = reader.ReadLine();
    }

Comments

0

Something like this?

using (Process p = new Process())
{
    p.StartInfo.FileName = "filename.exe";
    p.StartInfo.Arguments = "params";
    p.StartInfo.UseShellExecute = false; // use CreateProcess
    p.StartInfo.RedirectStandardOutput = true; // get stdout
    p.StartInfo.RedirectStandardError = true;  // get stderr
    p.StartInfo.CreateNoWindow = true;

    p.Start();

    string line;
    while (!p.StandardOutput.EndOfStream)
    {
        line = p.StandardOutput.ReadLine();
        Console.WriteLine(line);
    }

    if (!p.StandardError.EndOfStream)
    {
        Console.WriteLine("Error: " + p.StandardError.ReadToEnd());
    }

}

btw, this code was extracted from a real-world app which I wrote a long time ago.

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.