I am developing an application with C# and have to call an external function from a DLL. This function requires a pointer to an integer array as an argument. The DLL documentation states that the integer array must have >= 4kb space allocation. I know C# steers away from pointers but I'm pretty sure I don't have a choice here, no? How do I allocate a pointer to an integer array and guarantee it is >= 4kb in size, in C#?
I have:
public readonly unsafe int*[] dataBuffer = new int*[1000];
But I am not sure if this is correct.
Method signature is
int DataRec(void* buf);
CodePudding user response:
This is a quite simple case, you just need a pointer to an array, not an array of pointers (as you have declared).
This should work:
byte[] buf = new byte[4096]; // allocate managed buffer
fixed (int* p = &buf[0]) // create a fixed pointer to the first element of the buffer (= to the buffer itself)
{
int result = DataRec(p);
// ...
// use the data
}