4

I have written a C++ program (which executes from the command line), that works fine. Now I need to use it for my C# application. That is to say, I would like the output from my C++ program to be used in my C# application whenever it is called.

Is it possible? If so, how?

Any links or help would be appreciated.

4 Answers 4

9

You can use System.Diagnostics.Process to fire up your C++ program and redirect its output to a stream for use in your C# application. The information in this question details the specifics:

string command = "arg1 arg2 arg3"; // command line args
string exec = "filename.exe";      // executable name
string retMessage = String.Empty;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();

startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;

startInfo.UseShellExecute = false;
startInfo.Arguments = command;
startInfo.FileName = exec;

p.StartInfo = startInfo;
p.Start();

using (StreamReader output = p.StandardOutput)
{
    retMessage = output.ReadToEnd();
}

p.WaitForExit();

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

Comments

1

Make your C++ code DLL, and use pinvoke to call the C++ functions from C# code.

Read this article: Calling Win32 DLLs in C# with P/Invoke

Another way to do this is to use Process class from .Net. Using Process, you don't need to make your C++ code DLL; you can start your C++ EXE as a process from C# code.

Comments

1

You could have your C++ program write it's output to a file, and have your C# program read from the file.

If your application is very performance sensitive, then this is not the best way.

Here is the C# code to run the C++ program:

        try
        {
            Process p = StartProcess(ExecutableFileName);
            p.Start();
            p.WaitForExit();
        }
        catch
        {
            Log("The program failed to execute.");
        }

Now you are left to write to the file from your C++ program, and read from it in your C# program.

This will show you how to write to a file from your C++ program: http://www.cplusplus.com/doc/tutorial/files/

This will show you how to read from a file in your C# program: http://msdn.microsoft.com/en-us/library/ezwyzy7b.aspx

Comments

0

Since it seems the OP didn't leave any further comments, I am left wondering, would | not have sufficed? I guess it comes down to the object that is the "it" in "whenever it is called". If "it" refers to the C++ program, then Andy Mikula's answer is best. If "it" refers to the C# program, then I would suggest:

C:\>myCpluplus.exe | myCsharp.exe

and simply read from Console.In within myCsharp.exe.

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.