C#結構定義爲:如何將此結構數組傳遞給非託管代碼?
[StructLayout(LayoutKind.Sequential)]
public struct RecognizeResult
{
/// float
public float similarity;
/// char*
[MarshalAs(UnmanagedType.LPStr)]
public string fileName;
}
c function signature:
void FaceRecognition(RecognizeResult *similarity); //where similarity is a pointer to an array
P /調用簽名:
[DllImport(DllName, EntryPoint = "FaceRecognition")]
public static extern void Recognize(ref RecognizeResult similarity);
這是我如何調用C++函數在託管代碼:
RecognizeResult[] results = new RecognizeResult[100];
Recognize(ref results[0]); //through p/invoke
原來陣列不能傳遞給非託管代碼,只傳遞第一個元素。 我應該如何將數組傳遞給非託管代碼(它甚至可能)?
BTW,我一定到引腳排列調用非託管代碼時,使GC將不會移動陣列?
請問你的P/Invoke函數的聲明看起來像在C#中?特別是,你如何調整論點? – 2009-11-10 07:22:32
@Rudolph,我不知道如何編組數組。 – Benny 2009-11-10 07:28:44