Mix c++ and c# in the same project inside Visual Studio Express 2015

c#, c++, visual-studio, visual-studio-2015

Solution

If you pick option 2. You have to create 3 projects:

- C# project

- managed CLI/C++ (wrapper) project (dll)

- native C++ project (dll or lib)

Add a reference of the wrapper to your C# project and add a reference of the native project to your wrapper. You can, however, add your native C++ code directly to the wrapper and remove the 3rd project. But afaik you can't use the wrapper in other native C++ projects then.

You can't add C++ code directly to C#, so you have to go with one of your mentioned options.

Problem

I have a piece of unmanaged native C++ code (one class) that I want to integrate into a C# .NET assembly. I (think) I know I have two options for doing this: - pack my native C++ code into an unmanaged DLL and use PInvoke (DllImport, etc.) to load and run it from C# code; - use C++/CLI to make a managed wrapper C++ class that in turn references my unmanaged native C++ class, for which I found some examples. Let's suppose I want to go for option 2 and use Visual Studio Express 2015 IDE GUI, without calling directly the compilers from command line. Do I need to make two separate projects for the two C# (project "C#") and C++/CLI-C++/native (project "C++") parts, then reference (add reference) the DLL output file of project "C++" from project "C#" and link it statically? Is this the only way or could C++ source files be integrated directly into project "C#" by specifying a different compiler and different compiler options for each of them?

Original source