gpt4 book ai didi

c# - 在C#程序中编译并执行C++代码

转载 作者:行者123 更新时间:2023-12-02 10:38:20 24 4
gpt4 key购买 nike

我正在寻找一种在C#程序中编译和执行C++代码的解决方案。有什么办法吗?
我正在使用Visual Studio 2019 Professional。
下面的代码是创建C#编译器。

Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider();

我想创建C++代码并编译并执行它。我在我的代码中尝试:
CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("Cpp");

但错误是:

System.Configuration.ConfigurationErrorsException: 'The CodeDom provider type "Microsoft.VisualC.CppCodeProvider, CppCodeProvider, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" could not be located.'


CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("Cpp");
string Output = "Out.exe";
Button ButtonObject = (Button)sender;

textBox2.Text = "";
System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
//Make sure we generate an EXE, not a DLL
parameters.GenerateExecutable = true;
parameters.OutputAssembly = Output;
CompilerResults results =
codeProvider.CompileAssemblyFromSource(parameters, textBox1.Text);

if (results.Errors.Count > 0)
{
textBox2.ForeColor = Color.Red;
foreach (CompilerError CompErr in results.Errors)
{
textBox2.Text = textBox2.Text +
"Line number " + CompErr.Line +
", Error Number: " + CompErr.ErrorNumber +
", '" + CompErr.ErrorText + ";" +
Environment.NewLine + Environment.NewLine;
}
}
else
{
//Successful Compile
textBox2.ForeColor = Color.Blue;
textBox2.Text = "Success!";
//If we clicked run then launch our EXE
if (ButtonObject.Text == "Run") Process.Start(Output);
}

最佳答案

您可能需要创建一个C++项目,然后将其编译为DLL并在您的C#项目中使用它。

关于c# - 在C#程序中编译并执行C++代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58669184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com