Question

I know that C# allows interoperability with native code using PInvoke (An Overview of Managed/Unmanaged Code Interoperability)

We are planning to develop new code, and are considering 2 options:

  1. Native solution + managed (C#) wrapper around interop code that calls the native code.
  2. Fully managed solution

I would like to know whether there are any documented limitations to this interop ? (e.g - certain types that cannot be marshalled back and forth between managed/native, etc)

These limitations can affect our decision to use (or not use) option #1.

Was it helpful?

Solution

P.O.D. (plain-old-data) structs are generally fairly easy to marshal.

But if you want to marshal complex C++ classes that contain things like vectors, then you'll run into trouble.

If you can write C/C++ code to transform complex classes into simpler types for calling via p/invoke then that's ok - but otherwise, run away from complex C++ classes.

However, there is another possibility.

You can use the so-called "It just works" technology to wrap C++ code using a CLI C++ class. You can mix unmanaged and CLI code in C++ - even in the same file - which can really help.

See here for more details:

http://msdn.microsoft.com/en-us/library/ms173185.aspx

http://www.windowsdevcenter.com/pub/a/dotnet/2003/03/03/mcppp2.html (this is rather old)

http://www.codeproject.com/Articles/651516/Exposing-native-to-managed-Cplusplus-CLI-vs-P-Invo

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top