如何使用pinvoke来编组另一个传递给C DLL的结构

时间:2012-08-14 14:01:38

标签: c# c dll struct pinvoke

我试图弄清楚如何将一个简单的结构嵌入到另一个从c#传递给C dll的结构中。你如何编组嵌入式结构?精简到必需品。

//The C code
typedef struct {
       int a;
       int b;
} A;
typedef struct {
      int c;
      A myStruct;
} B;

//The c# code:
using System.Runtime.InteropServices;

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class A{
        int a;
        int b;
    }

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class B{
          int c;
          public IntPtr A;
     }

1 个答案:

答案 0 :(得分:0)

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class B{
      int c;
      public A a;
    }

只有在B定义为:

时才需要IntPtr
typedef struct {
      int c;
      A* myStruct;
} B;
相关问题