在C#和C之间编组嵌套结构 – 简单的HelloWorld

我遇到了一个问题,其中来自父结构的数据被正确编组,但子结构中的数据不是。 C中的结构定义和函数:

struct contact_info { char cell[32]; char home[32]; }; struct human { char first[32]; char last[32]; struct contact_info *contact; }; __declspec(dllexport) int __cdecl say_hello(struct human *person); __declspec(dllexport) int __cdecl import_csv(char *csvPath, struct human *person); 

C#P / Invoke代码:

 [StructLayout(LayoutKind.Sequential)] public struct contact_info { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String cell; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String home; } [StructLayout(LayoutKind.Sequential)] public struct human { [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String first; [MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)] public String last; public IntPtr contact; } [DllImport("HelloLibrary.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int say_hello(ref human person); [DllImport("HelloLibrary.dll", CallingConvention = CallingConvention.Cdecl)] public static extern int import_csv([MarshalAs(UnmanagedType.LPStr)]String path, ref human person); 

当我把代码使用时:

 HelloLibrary.human human = new HelloLibrary.human(); human.contact = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(HelloLibrary.contact_info))); HelloLibrary.contact_info contact = (HelloLibrary.contact_info) Marshal.PtrToStructure(human.contact, typeof(HelloLibrary.contact_info)); HelloLibrary.import_csv(args[0], ref human); Console.WriteLine("first:'{0}'", human.first); Console.WriteLine("last:'{0}'", human.last); Console.WriteLine("cell:'{0}'", contact.cell); Console.WriteLine("home:'{0}'", contact.home); 

human.firsthuman.last正确编组(例如"Joe""Schmoe" ),但是contact.cellcontact.home不是。 contact.cell通常是一些垃圾和contact.home什么都没有。

我仍然是编组的新手。 我没有正确的编组吗? 为什么struct contact_info *contact数据没有正确设置?

有关完整来源,请参阅此GitHub要点 。

您在调用import_csv之前将human.contact转换为您的结构,因此当您分配它时,它将包含内存中的任何内容。

如果您在调用import_csv的下方移动创建联系人的行,则应该具有正确的数据。