Gili
Gili

Reputation: 90180

Marshalling reference-types from C++ to C#

I want to invoke the following C++ function (exported by a DLL) from C#:

void createVm( 
  const jace::VmLoader& loader, 
  const jace::OptionList& options, 
  bool ignoreUnrecognized = true );

I've found documentation for marshaling primitives from C++ to C# but I'm not sure how to handle reference-types or non-pritmive types such as VmLoader or OptionList (both of which are classes). I'm trying to wrap a C++ API with a C# layer, delegating to the underlying C++ code for the actual method implementation.

Any ideas?

Upvotes: 1

Views: 853

Answers (2)

nichow
nichow

Reputation: 56

Assuming the c++ DLL correctly exports the types being passed by reference, you could right a light weight managed C++ wrapper that calls the dll. With managed c++ you can call native C/C++ libs and dlls directly while still exporting a managed interface from the resulting assemblies. Other .Net languages can call the managed interface just like they would any other assembly. It's a bit of extra overhead, but's is can be the quickest way to get it done.

Upvotes: 0

JaredPar
JaredPar

Reputation: 755477

AFAIK, PInvoking into a function with C++ constructs is not a supported operation. You could probably get it to work but I think you'll find problems.

What is supported is writing a simple C wrapper function which calls into your C++ function. PInvoke into the wrapper function instead and that will do the trick.

Upvotes: 4

Related Questions