如何在C#中的结构数组马歇尔? [英] How to marshall array of structs in C#?
问题描述
我有以下结构在C#中:
[StructLayoutAttribute(LayoutKind.Sequential)]
公共结构RECORD
{
公共UINT M1;
公共UINT平方米;
公共UINT立方米;
}
我也需要通过这些结构的阵列(固定长度)到本地code,其中的一些数据写入到这些结构。该数组在C#中分配,并转给C DLL。我宣布导入函数为:
函数[DllImport(marshall.dll,CallingConvention = CallingConvention.Cdecl)
私人静态外部无效的doIt(RECORD [] dataRecord);
但我没有得到任何数据备份。我已经尝试过的PInvoke互操作助理。我应该在这里使用的IntPtr?任何想法?
编辑:
下面是C#code调用本机的功能:
录像[] =录制新纪录[256];
doIt方法(REC);
REC的//值都是这里的零
下面是C函数:
INT的doIt(RECORD * REC)
{
// DEREF指针和写一些数据
}
我是远从P / Invoke的专家,但我不知道是否使得它的输入/输出参数可能会有所帮助:
的DllImport(marshall.dll,CallingConvention = CallingConvention.Cdecl)
私人静态外部无效的doIt([输入,输出]录像[] dataRecord);
我也不会的有望的这是必要的,虽然,与 LayoutKind.Sequential
我倒是希望你的结构已经是一个blittable类型,并且该阵列将是blittable太
I've the following structure in C#:
[StructLayoutAttribute(LayoutKind.Sequential)]
public struct RECORD
{
public uint m1;
public uint m2;
public uint m3;
}
I need too pass an array (fixed length) of these structs over to native Code, which writes some data to these structures. The array is allocated in C# and passed over to the C dll. I declared the imported function as:
[DllImport("marshall.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void doIt(RECORD[] dataRecord);
but I don't get any data back. I already tried the PInvoke Interop Assistant. Should I use IntPtr here? Any ideas?
Edit:
Here is the C# code to call the native function:
RECORD[] rec = new RECORD[256];
doIt(rec);
// values of rec are all zero here
Here is the C function:
int doIt(RECORD* rec)
{
// deref pointer and write some data
}
I'm far from a P/Invoke expert, but I wonder if making it an in/out parameter might help:
DllImport("marshall.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void doIt([In, Out] RECORD[] dataRecord);
I wouldn't have expected that to be necessary though, as with LayoutKind.Sequential
I'd hope that your struct is already a blittable type, and that the array would be blittable too.
这篇关于如何在C#中的结构数组马歇尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!