The function: MyCppDLL.WriteDeinterlacedAudio() is in a C++ wrapper
DLL. I don't think a Collection or multi-dimensional array can be cast
either, can it?
It can, but need to take some pains.
Because short[][]
is an array of arrays and since unsafe
code supports at most 1 "conversion" from array-pointer at a time in C#, the best you can do it to convert it to array of pointers first before converting it to pointer of pointers
in short, you need to do: array of arrays -> array of pointers -> pointer of pointers
Edit: I decided to put this note after comment by Mr. drf, please refer to his explanation and code. The idea above is to convert array of arrays to array of pointers before converting to pointer to pointers can still be used though. But how to do it safely, please refer to his more complete answer.
Something like this:
public int WriteAudio(short[][] audio, uint num_channels, uint channel_len) {
int ret = 0;
unsafe {
fixed (short* d = &audio[0][0]) //pointer to the address of first element in audio
{
short*[] arrOfShortPtr = new short*[audio.Length]; //here is the key!! really, this is now array of pointers!
for (int i = 0; i < arrOfShortPtr.Length; ++i) {
fixed (short* ptr = &audio[i][0]) {
arrOfShortPtr[i] = ptr; //like magic, casting from array of arrays to array of pointers
}
}
fixed (short** ptrOfPtr = &arrOfShortPtr[0]) { //now it is fine... second casting from array of pointers to pointer of pointers
//do what you want
}
}
}
return ret;
}