3

I have a c++ console app which runs in visual studio. This collects data and displays it in the console with all the raw data.

Another app (C#) is used to collect this information and present it to the UI.

Is it possible to combine the two by putting the C++ one inside the C# one so that both run at the same time as one service with the C++ app outputting its info to a panel or anything similar?

Thanks! :)

1
  • Is it managed C++? You could use it directly. If it is not managed, you can use the Process class to start a process (the console app). Commented Feb 9, 2011 at 15:48

3 Answers 3

4

A very quick example I have to do something like I said earlier is this:

private void executeCommand(string programFilePath, string commandLineArgs, string workingDirectory)
    {
        Process myProcess = new Process();

        myProcess.StartInfo.WorkingDirectory = workingDirectory;
        myProcess.StartInfo.FileName = programFilePath;
        myProcess.StartInfo.Arguments = commandLineArgs;
        myProcess.StartInfo.UseShellExecute = false;
        myProcess.StartInfo.CreateNoWindow = true;
        myProcess.StartInfo.RedirectStandardOutput = true;
        myProcess.StartInfo.RedirectStandardError = true;
        myProcess.Start();

        StreamReader sOut = myProcess.StandardOutput;
        StreamReader sErr = myProcess.StandardError;

        try
        {
            string str;

            // reading errors and output async...

            while ((str = sOut.ReadLine()) != null && !sOut.EndOfStream)
            {   
              logMessage(str + Environment.NewLine, true);
              Application.DoEvents();
              sOut.BaseStream.Flush();
            }

            while ((str = sErr.ReadLine()) != null && !sErr.EndOfStream)
            {
              logError(str + Environment.NewLine, true);
              Application.DoEvents();
              sErr.BaseStream.Flush();
            }

            myProcess.WaitForExit();
        }
        finally
        {
            sOut.Close();
            sErr.Close();
        }
    }

surely it's not perfect but it worked when executing a powershell script, I was seeing the output in a multiline textbox updating whenever something new came out, the method which updated my textbox was the logMessage()

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

2 Comments

thanks for the the submission. I gave you an up-vote! What using directives are required for the Process and StreamReader functions from this code?
if i want to have 2 commandLineArgs do I just list myProcess.StartInfo.Arguments = commandLineArgs 2x or allow for 2 lines under it? What is StartInfo in this instance? thanks !
0

As I understand it, you could run the c++ application from your C# application, you start it in another process and you redirect the standard output of that process so you are able to consume it in the way you want, like for example you can put it in a panel.

Comments

0

Sounds like you need to read from the stdout of the console application from the UI application. This article shows how this type of thing is done in C (on Windows). I'm sure with a bit of research you'll find how to do it in C# (as it's an operating system feature, not a language feature).

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.