Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing structure containing an array of structures between C and C# (DLL and P invoke)
    primarykey
    data
    text
    <p>I have C dll with some complicated struct and I ma really a newbie in C#:</p> <pre><code>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; </code></pre> <p>The issue is that I am not able to interface my C# application with this structure!!</p> <p>In the DLL there is a function GetData(complex_struct* myStruct) and I shoud call it from C#, so I created:</p> <pre><code> [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; } ; </code></pre> <p>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!!</p> <p>Can some one help me please, thanks</p> <hr> <p>Hi and thanks for your reply, </p> <p>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):</p> <p>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; } ;</p> <pre><code> [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; } } } </code></pre> <p>I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!! </p> <p>I tried to call GetData by ref or by out and both of them don't work...</p> <hr> <p>Hi and thanks for your reply, </p> <p>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):</p> <p>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; } ;</p> <pre><code> [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; } } } </code></pre> <p>I have done a lot of printf i GetData and all of them are well executed, it seems like the 'return' instruction crashes!! </p> <p>I tried to call GetData by ref or by out and both of them don't work...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload