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
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).