我有一个问题,我无法在网上找到答案。 我想从我的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