传递包含C和C#之间结构数组的结构(DLL和P调用)

时间:2012-02-23 17:51:02

标签: c# c arrays dll structure

我有一个带有一些复杂结构的C dll,我真的是C#中的新手:

typedef struct {
    int a;
    int b;
} simple_struct;

typedef struct {
    int d;
    int e;
    simple_struct f[20];
    short g;
    simple_struct h[20];
    short i;
} complex_struct;

问题是我无法使用这种结构连接我的C#应用​​程序!!

在DLL中有一个函数Ge​​tData(complex_struct * myStruct),我应该从C#中调用它,所以我创建了:

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe struct simple_struct {
        public int a;
        public int b;
    } ;

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    unsafe struct complex_struct {
        public int d;
        public int e;
        public simple_struct[] f;
        public short g;
        public simple_struct[] h;
        public short i;
    } ;

但问题是当我将complex_struct作为GetData的参数传递时,所有字段都从我这里填充,但不是我的两个simple_struct数组(我的意思是f和h)!他们的价值观是空的!!

有人可以帮助我,谢谢


嗨,谢谢你的回复,

我做的和你说的一样,但是当我调用GetData时,我还有另一个问题,进程崩溃而没有任何消息(一种异常):

这是我的C代码:     namespace dll_test_import_c_sharp     {         课程         {             [StructLayout(LayoutKind.Sequential,Pack = 1)]             struct simple_struct {                 public int a;                 public int b;             };

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct complex_struct {
            public int d;
            public int e;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] f;
            public short g;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] h;
            public short i;
        } ;




        [DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
        static extern int GetData(ref complex_struct a);



        static void Main(string[] args)
        {
            complex_struct a = new complex_struct();
            GetData(ref a);
            return;
        }

    }
}

我已经做了很多printf我的GetData并且所有这些都很好地执行了,好像'return'指令崩溃了!

我尝试通过ref或out调用GetData,但两者都不起作用......


嗨,谢谢你的回复,

我做的和你说的一样,但是当我调用GetData时,我还有另一个问题,进程崩溃而没有任何消息(一种异常):

这是我的C代码:     namespace dll_test_import_c_sharp     {         课程         {             [StructLayout(LayoutKind.Sequential,Pack = 1)]             struct simple_struct {                 public int a;                 public int b;             };

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        struct complex_struct {
            public int d;
            public int e;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] f;
            public short g;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
            public simple_struct[] h;
            public short i;
        } ;




        [DllImport("test_dll.dll", CharSet = CharSet.Unicode)]
        static extern int GetData(ref complex_struct a);



        static void Main(string[] args)
        {
            complex_struct a = new complex_struct();
            GetData(ref a);
            return;
        }

    }
}

我已经做了很多printf我的GetData并且所有这些都很好地执行了,好像'return'指令崩溃了!

我尝试通过ref或out调用GetData,但两者都不起作用......

1 个答案:

答案 0 :(得分:6)

您需要更改struct上的数组定义,以指定它是按值/内联数组

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe struct complex_struct {
    public int d;
    public int e;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public simple_struct[] f;
    public short g;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
    public simple_struct[] h;
    public short i;
} ;