P/Invoke or C++/CLI for wrapping a C library

前端 未结 6 673
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 08:45

Have a moderate size (40-odd function) C API that needs to be called from a C# project. The functions logically break up to form a few classes that will be API presented to the

6条回答
  •  粉色の甜心
    2021-01-31 09:14

    It depends in large part how memory ownership is handled. P/invoke can marshal pointers only when memory management is one of a couple particular ways, usually caller-allocated buffers. If your API returns pointers (via return value or out parameter, doesn't matter) and expects them to be handed back to a destruction function later... p/invoke will either do the automatic marshaling or give you direct access to the pointer value you need to send back later, never both. So C++/CLI becomes a very desirable approach in that case.

提交回复
热议问题