Performance differences between P/Invoke and C++ Wrappers

Will Eddins picture Will Eddins · Sep 16, 2009 · Viewed 9.1k times · Source

In the process of learning P/Invoke, I asked this previous question:

How to P/Invoke when pointers are involved

However, I don't quite understand the implications of using P/Invoke in C# over creating a wrapper in Managed C++. Creating the same DLL using P/Invoke in C# definately resulted in a cleaner interface since I could use DLLImport on an embedded resource, but would a Managed C++ wrapper for a native DLL, where I do the marshaling myself, have better performance?

Answer

Dror Helper picture Dror Helper · Sep 16, 2009

C++ wrapper should be faster, have a look at this MSDN page:

C++ Interop uses the fastest possible method of data marshaling, whereas P/Invoke uses the most robust method. This means that C++ Interop (in a fashion typical for C++) provides optimal performance by default, and the programmer is responsible for addressing cases where this behavior is not safe or appropriate.

So basically the main reason is that P/Invoke does pinning, blitting, error checking, while C++ interop just pushes the parameters on the stack and calls the function.

Another point to remember is that C++ can call a several APIs in a single call while P/Invoke EVERY parameter passed by address gets pinned and unpinned on EVERY call, copied and copied back, etc.