我有一個問題,我無法在網上找到答案。 我想從我的C#代碼中調用一個C++函數。 C++函數聲明爲:如何將包含void *的結構從C++傳遞到c#?
int read(InfoStruct *pInfo, int size, BOOL flag)
結構如下
typedef struct
{
int ID;
char Name[20];
double value;
void *Pointer;
int time;
}InfoStruct;
在我的C#代碼,我寫道:
public unsafe struct InfoStruct
{
public Int32 ID;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public string Name;
public Double value;
public void *Pointer;
public Int32 time;
};
[DllImport("mydll.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe int read(out MeasurementInfoStruct[] pInfo, int size, bool flag);
我試圖運行的代碼,但它崩潰所以我想我對這個結構特別是void *犯了一個錯誤,但是我無法弄清楚需要改變什麼。也可能是這個函數返回一個結構數組,也許我沒有把它調用正確。 你能幫我解決嗎? 非常感謝。
您是否嘗試過的IntPtr? –
你必須編組每個C#中的類變量,並嘗試在C#中使用引用而不是intptr。如果可能的話,會盡力爲您解決問題併發布解決方案。 – 2013-06-19 10:23:24
我嘗試IntPtr沒有成功。 – diditexas