This is my method which is called when I click on a button in my C# GUI program. It launches a very simple C++ console program that does nothing but prints out a line every second in a never ending loop.
private static Process process;
private void LaunchCommandLineApp()
{
process = new Process();
process.StartInfo.FileName = "SimpleTest.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.EnableRaisingEvents = true;
process.StartInfo.CreateNoWindow = false;
process.OutputDataReceived += process_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();
}
This is the method to handle any output data received:
private void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Console.WriteLine(e.Data.ToString());
}
I do not see any output in my C# debug output…
But if i change the printf to std::cout, it will show the redirected message.
I am thinking if there’s any way to show those statements using printf ?
FYI: my c++ code [EDITED working version]:
#include <stdio.h>
#include <Windows.h>
#include <iostream>
int main()
{
int i = 0;
for(;;)
{
Sleep(1000);
i++;
// this version of printf with fflush will work
printf("The current value of i is %d\n", i);
fflush(stdout);
// this version of cout will also work
//std::cout << "the current value of i is " << i << std::endl;
}
printf("Program exit\n");
}
Thanks guys for all your inputs!
I think I would change all my printf to std::cout and std::endl for my C++ console program.