.NET Interop IntPtr vs. ref

前端 未结 5 883
独厮守ぢ
独厮守ぢ 2020-12-28 09:22

Probably a noob question but interop isn\'t one of my strong points yet.

Aside from limiting the number of overloads is there any reason I should declare my DllImpo

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-28 09:35

    I've had some fun cases where a parameter is something like ref Guid parent and the corresponding documentation says:

    "Pointer to a GUID specifying the parent. Pass a null pointer to use [insert some system-defined item]."

    If null (or IntPtr.Zero for IntPtr parameters) really is an invalid parameter, then you're fine using a ref parameter - maybe even better off since it's extra clear exactly what you need to pass.

    If null is a valid parameter, you can pass ClassType instead of ref StructType. Objects of a reference type (class) are passed as a pointer, and they allow null.

提交回复
热议问题