[英]Pass structure (or class) from C++ dll to C# (Unity 3D)
我正在編寫包裝程序以從傳感器獲取一些數據。 雖然我對傳遞int,float和數組沒有任何問題,但是我很難掌握如何傳遞結構。
代碼摘要
C ++方面
結構如下:
struct HandInfo
{
int id;
float x, y, z;
};
在某個時候,一個靜態的,全局可見的HandInfo leftHand填充了可以通過以下包裝函數檢索的值:
extern EXPORT_API HandInfo MyWrapper_getLeftHand()
{
return handtracker.getLeftHand();
}
其中handtracker只是包裝類的一個實例。
C#面
一旦聲明了extern函數
[DllImport("MyWrapper")]
private static extern HandInfo MyWrapper_getLeftHand();
在C#端,理想情況下,我將具有相同的struct類型
public struct HandInfo
{
public int id;
public float x, y, z;
}
並分配一個變量
HandInfo hi = MyWrapper_getLeftHand(); // NAIVELY WRONG CODE
可以理解,這是行不通的。
如何實現這一目標?
我將不勝感激。 謝謝大家的時間。
它應該正常工作(至少在這里,在我擁有的一個小型C ++ + C#項目中,我能夠使其與該struct
,無論在x86和x64中)
[DllImport("MyWrapper.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern HandInfo MyWrapper_getLeftHand();
唯一的事情是,必須聲明CallingConvention = CallingConvention.Cdecl
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.