Basically, you will need to write C++ code that is declared extern "C" and that has a pure C API (not using classes, for example) that wraps the C++ library. Then you use the pure C wrapper library that you've created.
Rust can link to/call C functions via its FFI, but not C++ functions.
C and C++ are two closely related programming languages. Therefore, it may not come as a surprise to you that you can actually mix C and C++ code in a single program. However, this doesn't come automatically when you write your code the normal way.
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.
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.
I'm not familiar with the library you mentioned, but in general there are a couple ways to do so:
Yes, it is called P/Invoke.
Here's a great resource site for using it with the Win32 API:
http://www.pinvoke.net/
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With