我在 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 互操作助手。我应该在这里使用 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
}