Marshal C ++ Struct作为C#中的引用

时间:2011-12-07 00:50:09

标签: c# c++ struct

我正在尝试将此结构从C ++复制到C#:

        typedef struct
        {
            int id;
            char *name;
        } *ListOfObjects;

我已尝试使用此功能,但在使用此DLL并寻找特定签名的应用中无法正确导入。

  [StructLayout(LayoutKind.Sequential), Serializable]
  public struct ListOfObjects {
       [MarshalAsAttribute(UnmanagedType.ByValArray)]
       public int id;

       [MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 15)]
       public string name;
  }

  [DllExport("ReadListOfObjects", CallingConvention = CallingConvention.Cdecl)]
  static ListOfObjects ReadListOfObjects()
  {
      ListOfObjects lists = new ListOfObjects();
      return lists;
  }

编译DLL然后尝试启动导入这些函数的程序后,它会出现此错误:

  The prodedure entry point ReadListOfObjects could not be located in the dynamic link library thedll.dll.

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

[StructLayout(LayoutKind.Sequential), Serializable]
public struct ListOfObjects
{
    public int id;

    [MarshalAs(UnmanagedType.LPStr)]
    public string name;
}