# c++ c# question



## bagsfr (May 4, 2011)

Hello all,
i wrote a c++ program and c# program.
i want to run both of them in a same project or i want to join them.
how can i convert c++ code to c# code to use in c# and how?

Plase help me its important for me thank you


----------



## arpanmukherjee1 (May 4, 2011)

use CodeDom. if you dont know what it is, i strongly recommend to read examples from *MSDN *or *CodeProject* before you use the code below


```
using System.CodeDom.Compiler;
```


```
///C# function to compile C++ code
   public string ExecuteCode(string codeText, sting outputFileName) //pass entire C++ code as parameter and the output location
   {
        string result = ""; //final result that is displayed
        CodeDomProvider codeProvider = CodeDomProvider.CreateProvider("Cpp"); //creating a C++ compiler
        CompilerParameters parms = new CompilerParameters();
        parms.GenerateExecutable = true;
        parms.OutputAssembly = outputFileName; //set output filename. must include check that path exists
        CompilerResults res = codeProvider.CompileAssemblyFromSource(parms, codeText);
        if (res.Errors.Count > 0)
            foreach (CompilerError err in res.Errors)
                result += "Line No.: " + err.Line + " Error No.: " + err.ErrorNumber + " - " + err.ErrorText + "\n";
        else
            result = "Executable sucessfully created with file name : " + outputFileName;

        return result;
   }
```


----------

