2012-08-14 54 views
2

我想弄清楚如何将一个简单的结构嵌入到另一个传递给C#的C DLL的结构中。你如何编制嵌入式结构?剥离为必需品。如何使用pinvoke将marshall内部的结构传递给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; 
    } 

回答

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

需要IntPtr的只有B定义为:

typedef struct { 
     int c; 
     A* myStruct; 
} B;