我在C#中有以下结构:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct RECORD
{
public uint m1;
public uint m2;
public uint m3;
}
Run Code Online (Sandbox Code Playgroud)
我还需要将这些结构的数组(固定长度)传递给本机代码,后者将一些数据写入这些结构.该数组在C#中分配并传递给C dll.我将导入的函数声明为:
[DllImport("marshall.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void doIt(RECORD[] dataRecord);
Run Code Online (Sandbox Code Playgroud)
但我没有得到任何数据.我已经尝试过PInvoke Interop助手.我应该在这里使用IntPtr吗?有任何想法吗?
编辑:
以下是调用本机函数的C#代码:
RECORD[] rec = new RECORD[256];
doIt(rec);
// values of rec are all zero here
Run Code Online (Sandbox Code Playgroud)
这是C函数:
int doIt(RECORD* rec)
{
// deref pointer and write some data
}
Run Code Online (Sandbox Code Playgroud)