码迷,mamicode.com
首页 > 编程语言 > 详细

结构体指针作函数参数(C# 调用C++ 的DLL)

时间:2018-12-28 18:27:25      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:ref   .dll   try   函数   poi   stat   byte   sha   nbsp   

1、C++结构体定义:

  #pragma pack(1)
  struct Person
  {
      #define Count_favoriteNumbers 6
  
      int id;
  
      float favoriteNumbers[Count_favoriteNumbers];
  
  };
  #pragma pack()        // #pragma pack(1) end

 

  C++ 导出函数:

  #define DllExport extern "C" __declspec(dllexport)

  DllExport void __stdcall InitPersonInfo_DLL(Person* p_Person)
  {
      p_Person->id = 6;
  
      for (int i = 1; i <= Count_favoriteNumbers; i++)
      {
          p_Person->favoriteNumbers[i - 1] = 1.11 * i;
      }    
  }

 

2、C# 结构体定义:

  [StructLayout(LayoutKind.Sequential, Pack = 1)]
      public class Person
      {
          public const int Count_favoriteNumbers = 6;

          public int id;

          [MarshalAs(UnmanagedType.ByValArray, SizeConst = Count_favoriteNumbers, ArraySubType = UnmanagedType.U4)]
          public float[] favoriteNumbers = new float[Count_favoriteNumbers];  
      }

 

  C# 调用:

  public partial class MainWindow : Window
  {

    [DllImport("MFCLibrary_ExportFunction.dll", EntryPoint = "InitPersonInfo_DLL")]
          static extern unsafe void InitPersonInfo_DLL(byte* p_Person);
          public unsafe void InitPersonInfo(ref Person person)
          {
              byte[] structBytes = StructToBytes(person);
              fixed (byte* p_Person = &structBytes[0])
              {
                  InitPersonInfo_DLL(p_Person);

                  person = (Person)BytesToStruct(structBytes, person.GetType());
              }

 

    public MainWindow()
          {
              InitializeComponent();

        Person zhangSan = new Person();
              InitPersonInfo(ref zhangSan);
          }

 

    #region // 结构体与 byte[] 互转

    // Struct 转换为 byte[]
          public static byte[] StructToBytes(object structure)
          {
              int size = Marshal.SizeOf(structure);
              IntPtr buffer = Marshal.AllocHGlobal(size);

              try
              {
                  Marshal.StructureToPtr(structure, buffer, false);
                  byte[] bytes = new byte[size];
                  Marshal.Copy(buffer, bytes, 0, size);

                  return bytes;
              }
              finally
              {
                  Marshal.FreeHGlobal(buffer);
              }
          }

          // byte[] 转换为 Struct
          public static object BytesToStruct(byte[] bytes, Type strcutType)
          {
              int size = Marshal.SizeOf(strcutType);
              IntPtr buffer = Marshal.AllocHGlobal(size);

              try
              {
                  Marshal.Copy(bytes, 0, buffer, size);

                  return Marshal.PtrToStructure(buffer, strcutType);
              }
              finally
              {
                  Marshal.FreeHGlobal(buffer);
              }
          }
          #endregion

  }

结构体指针作函数参数(C# 调用C++ 的DLL)

标签:ref   .dll   try   函数   poi   stat   byte   sha   nbsp   

原文地址:https://www.cnblogs.com/dhqy/p/10192062.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!