C# P/Invoke: Marshalling structures containing function pointers

前端 未结 3 1358
遥遥无期
遥遥无期 2020-12-08 08:15

Sorry for the verbose introduction that follows. I need insight from someone knowing P/Invoke internals better than I do.

Here is how I\'m marshalling structures cont

3条回答
  •  误落风尘
    2020-12-08 09:10

    For point 1:

    Marshal.GetDelegateForFunctionPointer() is more simple if your structure contains a lot of function pointers and you use only a few. A (major) drawback is that you have to compute the offset to the function pointer by hand (note that pointer size differs on 32/64 bits platform). A structure is more easy to use but marshals more data.

    For point 2:

    I don't think a less verbose approach is possible. You may only define delegates for the function you want to use, and use a dummy delegate for the function pointers you don't want to use. This way, the marshaling will perform ok, but you end with a structure containing non-callable delegates.

提交回复
热议问题