hzrari
hzrari

Reputation: 1933

Passing structure containing an array of structures between C and C# (DLL and P invoke)

I have C dll with some complicated struct and I ma really a newbie in 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;

The issue is that I am not able to interface my C# application with this structure!!

In the DLL there is a function GetData(complex_struct* myStruct) and I shoud call it from C#, so I created:

    [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;
    } ;

but the problem is that when I pass complex_struct as argument of GetData, all the fields are filled back form me, but not my two array of simple_struct (I mean f and h)!! Their values are null!!

Can some one help me please, thanks


Hi and thanks for your reply,

I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):

This is my C sharp code: namespace dll_test_import_c_sharp { class Program { [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;
        }

    }
}

I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!!

I tried to call GetData by ref or by out and both of them don't work...


Hi and thanks for your reply,

I have done like what you said, but I still have another issue when I call GetData, the process crashes without any message (a kind of Exception):

This is my C sharp code: namespace dll_test_import_c_sharp { class Program { [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;
        }

    }
}

I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!!

I tried to call GetData by ref or by out and both of them don't work...

Upvotes: 4

Views: 3111

Answers (1)

JaredPar
JaredPar

Reputation: 754763

You need to change the array definition on the struct to specify that it's a by value / inline array

[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;
} ;

Upvotes: 7

Related Questions