c#struct定义为:
[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 / Invoke签名:
[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不会移动数组?答案 0 :(得分:1)
试试这个:
[DllImport(DllName, EntryPoint = "FaceRecognition")]
public static extern void Recognize(RecognizeResult[] similarity);
RecognizeResult[] results = new RecognizeResult[100];
// fill array elements
Recognize(results);