I’m attempting to write a wrapper so that my C# application can use a DLL written in C.
Here is a method definition that I’m trying to wrap:
void methodA(const uint32_t *data); //c header declaration
The issue I’m having is trying to figure out how to give a equivalent pointer from c#. In c# I want it to operate on a:
UInt32 data[] //my c# object i want to be able to pass in
but how do I give an equivalent pointer in my wrapper? I have tried
ref data //my attempt at giving an equivalent pointer to the DLL
but that doesn’t seem to be working. Using debug statements in the DLL I can see that the values it gets that way are not what I’m attempting to pass in.
So my question boils down to have do I properly wrap a c function that is using a pointer to reference an array?
An array is already a reference, so it will get marshalled as a pointer to it. This should work:
If you need to pass data back to managed code, you need to decorate the parameter with the Out attribute:
Usage:
Another solution is to declare the parameter to be of type IntPtr:
To make this work, you need to pin the array in order to get the IntPtr for it, or allocate memory in unmanaged space and copy the array contents to it. I wouldn’t recommend these options, though.
refis required if you want to pass a single value of a value type by reference: