【问题标题】:How to use pinvoke to marshall structure inside another passed to C DLL如何使用 pinvoke 在另一个传递给 C DLL 的内部编组结构
【发布时间】:2012-08-10 20:14:00
【问题描述】:

我试图弄清楚如何将一个简单的结构嵌入到另一个从 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;
     }

【问题讨论】:

    标签: c# c dll struct pinvoke


    【解决方案1】:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class B{
          int c;
          public A a;
        }
    

    仅当 B 定义为时才需要 IntPtr:

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

    【讨论】:

      猜你喜欢
      • 2018-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多