在 Visual Studio 2010 中从 .NET 项目启动C++项目

本文关键字:项目 启动 C++ NET 中从 Visual Studio 2010 | 更新日期: 2023-09-27 17:56:01

我有以下问题:

我有两个项目,Project Game 包含一个使用 SDL 库以C++编码的游戏。项目启动器是一个 C# .NET 项目,它提供了一个界面,可以在启动项目游戏之前从中选择选项。

我的问题是A) 如何从项目启动器中启动项目游戏?B) 如何将参数从项目启动器传递到项目游戏?

我还没有真正找到一个明确的解决方案,只是在这里和那里窃窃私语。对于参数,很明显,只需用参数调用.exe并C++阅读它们,但我想知道是否有一种更干净的方法来执行此操作,该方法内置于 .NET 中。任何帮助将不胜感激。如果我找到解决方案,我会在这里发布。

在 Visual Studio 2010 中从 .NET 项目启动C++项目

我目前没有IDE,所以我不确定,但我记得这样的事情应该可以解决问题。

ProcessStartInfo proc = new ProcessStartInfo();
//Add the arguments
proc.Arguments = args; 
//Set the path to execute
proc.FileName = gamePath;
proc.WindowStyle = ProcessWindowStyle.Maximized;
Process.Start(proc);

编辑:我的错,我没有看到您正在寻找不使用将参数传递给游戏进程的方法。我留下回复只是为了参考其他人!:)

.NET 框架包含一个名为 Process 的类,它包含在"诊断"命名空间中。您应该使用System.Diagnostics包含命名空间,然后启动应用程序,如下所示:

using System.Diagnostics;
// Prepare the process to run
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = "readme.txt"; 
// Enter the executable to run, including the complete path
start.FileName = "notepad";
// Do you want to show a console window?
start.WindowStyle = ProcessWindowStyle.Hidden;
start.CreateNoWindow = true;
//Is it maximized?
start.WindowStyle = ProcessWindowStyle.Maximized;
// Run the external process & wait for it to finish
using (Process proc = Process.Start(start))
{
     proc.WaitForExit();
     // Retrieve the app's exit code
     exitCode = proc.ExitCode;
}