我在C#中有以下结构:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct RECORD
{
public uint m1;
public uint m2;
public uint m3;
}
我还需要将这些结构的数组(固定长度)传递给本机代码,后者会将一些数据写入这些结构。该数组在C#中分配并传递给C dll。我将导入的函数声明为:
[DllImport("marshall.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void doIt(RECORD[] dataRecord);
但我没有得到任何数据。我已经尝试过PInvoke Interop助手。我应该在这里使用IntPtr吗?有什么想法吗?
修改
以下是调用本机函数的C#代码:
RECORD[] rec = new RECORD[256];
doIt(rec);
// values of rec are all zero here
这是C函数:
int doIt(RECORD* rec)
{
// deref pointer and write some data
}
答案 0 :(得分:14)
我远离P / Invoke专家,但我想知道是否将它作为输入/输出参数可能有所帮助:
DllImport("marshall.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void doIt([In, Out] RECORD[] dataRecord);
我不希望期望这是必要的,就像LayoutKind.Sequential
一样,我希望你的结构已经是一个blittable类型,并且数组也是blittable