How to create a DLL in Visual Studio 2008 for use in a C # application?

I have a C ++ class with which I would like to access from a C # application. I need to access the constructor and one member function. The application is currently accepting data in a form stl::vector, but can I do some conversion if this is unlikely to work?

I found several articles on the Internet that describe how to call the C ++ DLL and some others that describe how to create .dll projects for other purposes. I'm struggling to find a guide for creating them in Visual Studio 2008 for use in a C # application (although it seems that there are several for VS 6.0, but most of the options that they set do not seem to appear in the 2008 version).

If someone has a step-by-step guide or a fairly simple example from which to go, I would be very grateful.

+3
source share
5 answers

The easiest way to interact between C ++ and C # is to use managed C ++ or C ++ / CLI, as it is called. In VisualStudio, create a new C ++ project of the type "CLR Class Library". There is a new syntax for the parts you want to make available for C #, but you can use regular C ++ as usual.

In this example, I use std::vector<int>to show that you can use standard types - however, in a real application, I prefer to use .NET types where possible (in this case a System::Collections::Generic::List<int>).

#pragma unmanaged
#include <vector>
#pragma managed

public ref class CppClass
{
public:
   CppClass() : vectorOfInts_(new std::vector<int>)
   {}

   // This is a finalizer, run when GC collects the managed object
   !CppClass()
   { delete vectorOfInts_; }

   void Add(int n)
   { vectorOfInts_->push_back(n); }

private:
    std::vector<int>* vectorOfInts_;
};

EDIT: , .

+2

DLL . P/invoke. p/invoke :

[DllImport("Library_Name.dll", EntryPoint = "function")]
public static extern void function();

, , (++/CLI) DLL ( ). , .NET.

EDIT: , . ++ DLL .NET, : Visual ++/CLR/Class Library. DLL #.

.:)

+3

++ exe, dll? ++?
++, DLL.Net. stl- # ++, , .Net.
stl , ++ .

+1

, . , , DLL , . , , DLL ​​ .

0

++ Dll #.

A “simple” route is to put it in a COM wrapper.

-1
source

Source: https://habr.com/ru/post/1708558/