[英]Running a c++ console app inside a C# console app
我有一個在Visual Studio中運行的C ++控制台應用程序。 這將收集數據並將其與所有原始數據一起顯示在控制台中。
另一個應用程序(C#)用於收集此信息並將其呈現給UI。
是否可以通過將C ++一個放入C#中來組合兩者,從而使兩者在一項服務同時運行,而C ++應用將其信息輸出到面板或類似的東西?
謝謝! :)
一個非常快速的示例,我必須像我之前所說的那樣做:
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()
據我了解,您可以從C#應用程序中運行c ++應用程序,在另一個進程中啟動它,然后重定向該進程的標准輸出,以便您能夠以自己想要的方式使用它,例如,可以放置它在面板中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.