Is there generally a noticeable performance hit when calling PInvoke on Win32 / COM methods?

前端 未结 2 352
清酒与你
清酒与你 2021-02-01 03:42

I\'m wondering whether anyone has a decent explanation or overview on the negative aspects of using DLLImport / PInvoke on Win32 methods from managed .Net code?

I plan t

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 04:41

    Here are some issues I see with PInvoke:

    • Parameter marshaling can be expensive, depending on the data type (blittable / non-blittable)
    • Correct parameter marshaling is often not very intuitive
    • PInvoke does not provide any compile time security. One can easily misspell DLL or function names easily without the compiler complaining.

    If you plan to use lots of unmanaged functions I would create a mixed DLL (http://msdn.microsoft.com/en-us/library/x0w2664k.aspx) rather than declaring a bunch of PInvokes.

提交回复
热议问题