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

标签 c# c++

我正在寻找一种在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/

相关文章:

c++ - 如何提取/扩展可变模板参数

c++ - 在 UDP sendto 中将数据作为结构发送

c++ - Emacs EDE - Automake 和子目录的顺序

c++ - 用 C++ 计算雅可比矩阵(符号数学)

c++ - 尝试使用 magick++ 在 c++ 中删除背景并使其透明

c# - 如何使用 SVN 发布版本?

c# - 通过接口(interface)在多个集合中引用同一个对象

c# - VSTS 版本 : roles keep recycling because they can't locate valid DLLs

c# - GZipInputSteam 解压不适用于大小超过 13239 的数据

c# - 传输或序列化程序集