95

Is it possible to call C++ code, possibly compiled as a code library file (.dll), from within a .NET language such as C#?

Specifically, C++ code such as the RakNet networking library.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
  • Unmanged c++ to mangaged c#: https://www.c-sharpcorner.com/uploadfile/tanmayit08/unmanaged-cpp-dll-call-from-managed-C-Sharp-application/ – Tyler S. Loeper Feb 26 '19 at 13:33

7 Answers7

98

One easy way to call into C++ is to create a wrapper assembly in C++/CLI. In C++/CLI you can call into unmanaged code as if you were writing native code, but you can call into C++/CLI code from C# as if it were written in C#. The language was basically designed with interop into existing libraries as its "killer app".

For example - compile this with the /clr switch

#include "NativeType.h"

public ref class ManagedType
{
     NativeType*   NativePtr; 

public:
     ManagedType() : NativePtr(new NativeType()) {}
     ~ManagedType() { delete NativePtr; }

     void ManagedMethod()
      { NativePtr->NativeMethod(); } 
}; 

Then in C#, add a reference to your ManagedType assembly, and use it like so:

ManagedType mt = new ManagedType();
mt.ManagedMethod();

Check out this blog post for a more explained example.

Robert Balent
  • 1,442
  • 11
  • 21
Eclipse
  • 43,933
  • 19
  • 112
  • 169
10

P/Invoke is a nice technology, and it works fairly well, except for issues in loading the target DLL file. We've found that the best way to do things is to create a static library of native functions and link that into a Managed C++ (or C++/CLI) project that depends upon it.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
plinth
  • 47,047
  • 11
  • 77
  • 120
9

I'm not familiar with the library you mentioned, but in general there are a couple ways to do so:

  • P/Invoke to exported library functions
  • Adding a reference to the COM type library (in case you're dealing with COM objects).
Cris Luengo
  • 49,445
  • 7
  • 57
  • 113
mmx
  • 402,675
  • 87
  • 836
  • 780
5

Yes, it is called P/Invoke.

Here's a great resource site for using it with the Win32 API:

http://www.pinvoke.net/

Cris Luengo
  • 49,445
  • 7
  • 57
  • 113
TWA
  • 12,474
  • 12
  • 58
  • 92
2

Sure is. This article is a good example of something you can do to get started on this.

We do this from C# on our Windows Mobile devices using P/Invoke.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Mat Nadrofsky
  • 8,189
  • 8
  • 48
  • 73
2

The technology used to do this is called P/Invoke; you can search for articles on the subject. Note that it is for calling C from C#, not C++ so much. So you'll need to wrap your C++ code in a C wrapper that your DLL exports.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Frank Schwieterman
  • 23,788
  • 15
  • 90
  • 127
-2

Have you considered Apache Thrift?

http://thrift.apache.org/

It seems like a very very neat solution.

Soumendra
  • 1,168
  • 1
  • 15
  • 28
  • [Apache Thrift](https://en.wikipedia.org/wiki/Apache_Thrift) - *"Thrift is an interface definition language and binary communication protocol used for defining and creating services for numerous programming languages."* – Peter Mortensen May 25 '22 at 13:04
  • How does that answer the question? It is essentially a link-only answer. Can you elaborate in your answer (in your own words)? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/4280372/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen May 25 '22 at 13:04
  • OK, the OP has probably left the building (*"Last seen more than 1 year ago"*) – Peter Mortensen May 25 '22 at 13:05