C to C# PInvoke with structs with pointers

后端 未结 1 1232
死守一世寂寞
死守一世寂寞 2021-01-19 12:33

I\'m trying to create a C# interface which receives a callback from an external C DLL. The callback\'s parameters contain pointers to C structs, which themselves have a poin

相关标签:
1条回答
  • 2021-01-19 13:00

    You will not get help from the marshaller, this normally causes a major memory management problem. But can work in the specific case of a callback since it is the calling C program that manages the data.

    You have to convert the data yourself. Declare the pointers as IntPtr and use Marshal.PtrToStructure() to retrieve the data.

    The anotherbuffertype.data member looks like an array, use Marshal.Copy() to copy its content into your own byte[] array. If you don't mind the unsafe keyword then you can keep it declared as byte* and access the elements with data[index], avoids the cost of the copy. It is not very unsafe, pretty easy to keep index constrained to [0..size).

    0 讨论(0)
提交回复
热议问题