我想了解如何從C#數組傳遞到本機函數的解決方案。我已經有一個委託給函數(Marshal.GetDelegateForFunctionPointer),但現在我試圖傳遞一個多維數組(或更確切的說是一個數組數組)。修復在C#中的數組數組(不安全的代碼)
此代碼示例在輸入有2個子數組時可以工作,但我需要能夠處理任意數量的子數組。你能想到的最簡單的方法是什麼?我不希望數據陣列,因爲這之間的複製將在實時循環中發生的事情(我用的音頻效果通信)
public void process(float[][] input)
{
unsafe
{
// If I know how many sub-arrays I have I can just fix them like this... but I need to handle n-many arrays
fixed (float* inp0 = input[0], inp1 = input[1])
{
// Create the pointer array and put the pointers to input[0] and input[1] into it
float*[] inputArray = new float*[2];
inputArray[0] = inp0;
inputArray[1] = inp1;
fixed(float** inputPtr = inputArray)
{
// C function signature is someFuction(float** input, int numberOfChannels, int length)
functionDelegate(inputPtr, 2, input[0].length);
}
}
}
}
我讀過關於GCHandle類的內容。在閱讀你的回覆之後,我試着爲我的電話實施它,它似乎工作得很好。感謝您快速回答。 – Valdemar