Leron
Leron

Reputation: 9876

Compiling existing C++ project to DLL

I'm working on an inherited project that includes a C++ project which is successfully compiled to .exe. Now I want to try and integrate this project to a .NET form so I want to have it as a DLL (from what I've read it seems easier to do that if I use dll instead of exe) so I want to compile my C++ project to .DLL. I tried by right clicking on the project and making adjustment on the properties, but even though I choose .dll option when I rebuild the project I still get exe file.

Upvotes: 1

Views: 3123

Answers (4)

D Stanley
D Stanley

Reputation: 152624

Here are the three "cleanest" options:

  1. Create a C++/CLI (also called "Mangaged C++) wrapper around your C++ code to expose the functions as class methods. Then you can call these methods from C# just as you would a C# class.

  2. Create a COM wrapper around your C++ functions. Very similar to creating a CLI wrapper, but can be used in non-.NET environments as well (VBA, for example)

  3. Call your C++ DLL using P/Invoke. This requires the least C++ coding but can also tricky to get the interop correct, especially if you're marshaling structures or other complex data types.

Upvotes: 2

harper
harper

Reputation: 13690

You need at least to change

  • General/Configuration Type
  • Linker/General/Output File

But your DLL should implement DllMain to ensure that the runtime initialization is done properly.

Another approach might be creating a new DLL project and adding all code from the EXE. This makes the project wizard script running the appropriate lines.

Upvotes: 1

TheMathemagician
TheMathemagician

Reputation: 958

In Visual Studio it's easiest to take the path of least resistance. Just create a new empty DLL project, add your source files to it, change the C++ details (under Properties) and voila! Trying to change the project settings by hand always seems to leave a lot of redundant chaff in there.

Under Code Generation you probably want Multi-threaded (Debug) DLL

Upvotes: 0

Sandeep Godbole
Sandeep Godbole

Reputation: 1

Use COM Interop to integrate C++ classes with .NET framework.

Upvotes: 0

Related Questions