在C#控制台应用程序中运行C ++控制台应用程序

在C#控制台应用程序中运行C ++控制台应用程序

问题描述:

我有一个在Visual Studio中运行的C ++控制台应用程序。这将收集数据并将其与所有原始数据一起显示在控制台中。

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.

另一个应用程序(C#)用于收集此信息并将其呈现给UI。

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

是否可以通过将一个C ++放在C#中来合并两者,以便两者都与一项服务同时运行,而C ++应用会将其信息输出到面板还是类似的东西?

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?

谢谢! :)

一个非常简单的例子,我必须像我之前所说的那样做:

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();
        }
    }

当然,它不是完美的,但在执行Powershell时有效脚本,每当有新内容出现时,我都在多行文本框中看到输出更新,更新我的文本框的方法是logMessage()

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()