Fabius Wiesner
Fabius Wiesner

Reputation: 926

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

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:

  1. pack my native C++ code into an unmanaged DLL and use PInvoke (DllImport, etc.) to load and run it from C# code;
  2. 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?

Upvotes: 3

Views: 7372

Answers (2)

Timo
Timo

Reputation: 9835

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

  1. C# project
  2. managed CLI/C++ (wrapper) project (dll)
  3. 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.

Upvotes: 3

Simone Cifani
Simone Cifani

Reputation: 784

If you go with option 2 you need a separate C++/CLI project that you must reference from c# project.
I suppose that "link it statically" means to add the assembly reference, which isn't the same thing.

Upvotes: 2

Related Questions