Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I'm trying to integrate C++ and C# projects in order to get C++ console output to C#. Problem is the output of the C++ exe is displayed in the C# console only after the C++ exe is terminated. I want to get them soon after they have been written to console.

Currently I'm having my code in C# module as follows to call C++ module.

C#
private void Form1_Load(object sender, EventArgs ex)
       {
           Process process = new Process();

           process.StartInfo.UseShellExecute = false;
           process.StartInfo.RedirectStandardOutput = true;
           process.StartInfo.RedirectStandardError = true;
           process.EnableRaisingEvents = true;
           process.StartInfo.FileName = "C:\\Users\\acer\\docs\\testapp.exe";

           process.Start();
           process.BeginOutputReadLine();
           process.OutputDataReceived += new DataReceivedEventHandler(Process_OutputDataReceived);

           //not using as wants to run C# module at the same time with C++ module
           //process.WaitForExit();
       }

       void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
       {

           if(e.Data != null){
           string s = e.Data.ToString();
           Console.WriteLine(s);

           }
       }


Can anyone let me know what's I'm missing here?

Thank you.
Posted
Comments
Sergey Alexandrovich Kryukov 15-Feb-12 13:02pm    
What's the problem?
--SA
Sergey Alexandrovich Kryukov 15-Feb-12 13:05pm    
Why do you want to use it "at the same time"? What's the timing? Does the child process wait for input? Then you need to re-direct input as well...
--SA
gimaMad 15-Feb-12 20:42pm    
Acutally my UI in C# and inputs are coming from C++ module. Therefore, in order to handle the UI I need to read C++ console outputs to C# module during run time as in they have to work together.
Sergey Alexandrovich Kryukov 15-Feb-12 13:11pm    
As you use Console.WriteLine..? Is your C# application using console? Otherwise the output goes nowhere? Did you run it under debugger? If you want to run two applications in parallel, do you understand that you will mess up the output of two applications in one console. How are you going to use the output of the child application?
--SA
gimaMad 15-Feb-12 20:44pm    
No there's is no such problem. There's noting much to print with C# module; just one line. And as i mentioned above I'm getting C++ console output to C# module only after C++ exit.

Please see my comment to your question. There are a number of questions. This is certainly possible, but…

This is the worst kind of integration I could imagine. This is still acceptable if you don't have a source code of your C++ project, and if the C++ application is very simple console application so you could read all the output at once and wait for the child process to terminate. By the way, this is better solution for a child process: run it all synchronously and wait for termination (how else would you know it is terminated?) but it a separate thread of the parent process.

However, this game is awkward. If you need real integration, do it all in one process (but you can use different threads). Modify your C++ project into DLL. Then you have two options:



—SA
 
Share this answer
 
in your c++ console application use
C++
cout.flush()
instead of cout<<
 
Share this answer
 
Have your c# code read from comand line and perhaps use pipes to chain the output as inpout to c#. U will then have one more c# app exectuning both programs with pipe.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-12 13:22pm    
OP already piped the two using redirection. Please understand: I have to vote 1. You advice is based on misunderstanding of what is already done and totally redundant; it would create exactly the same problem but with extra effort. The OP's problem is different.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900