C#调用C/C++动态库 封送结构体,结构体数组

一、结构体传递

 
  1. #define JNAAPI extern "C" __declspec(dllexport) // C方式导出函数

  2.  
  3. typedef struct

  4. {

  5. int osVersion;

  6. int majorVersion;

  7. int minorVersion;

  8. int buildNum;

  9. int platFormId;

  10. char szVersion[128];

  11. }OSINFO;

  12.  
  13. // 1. 获取版本信息(传递结构体指针)

  14. JNAAPI bool GetVersionPtr( OSINFO *info );

  15. // 2.获取版本信息(传递结构体引用)

  16. JNAAPI bool GetVersionRef(OSINFO &info);


可以通过二种方式进行调用:

 
  1. // OSINFO定义

  2. [StructLayout(LayoutKind.Sequential)]

  3. public struct OSINFO

  4. {

  5. public int osVersion;

  6. public int majorVersion;

  7. public int minorVersion;

  8. public int buildNum;

  9. public int platFormId;

  10. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]

  11. public string szVersion;

  12. }


1. 方式一(传入结构体引用),在C#中,结构体是以传值方式传递,类才是以传地址方式传递,加关键字ref即可. C端传递了两种不同类型的参数,都可以通过引用来解决.

 
  1. [DllImport("jnalib.dll", EntryPoint = "GetVersionPtr")]

  2. public static extern bool GetVersionPtr(ref OSINFO info);

  3. public static extern bool GetVersionRef(ref OSINFO info);


2. 方式二(传入IntPtr(平台通用指针))

 
  1. IntPtr pv = Marshal.AllocHGlobal(148); //结构体在使用时一定要分配空间(4*sizeof(int)+128)

  2. Marshal.WriteInt32(pv,148); //向内存块里写入数值

  3. if (GetVersionPtr(pv)) //直接以非托管内存块地址为参数

  4. {

  5. Console.WriteLine("--osVersion:{0}", Marshal.ReadInt32(pv, 0));

  6. Console.WriteLine("--Major:{0}",Marshal.ReadInt32(pv, 4)); //移动4个字节

  7. Console.WriteLine("--BuildNum: " + Marshal.ReadInt32(pv, 12));

  8. Console.WriteLine("--szVersion: "+Marshal.PtrToStringAnsi((IntPtr)(pv.ToInt32()+20)));

  9. }

  10. Marshal.FreeHGlobal(pv); //处理完记得释放内存


 二.结构体数组的传递

 
  1. // 传递结构体指针

  2. JNAAPI bool GetVersionArray(OSINFO *info,int nLen);


调用:

 
  1. /**

  2. * C#接口,对于包含数组类型,只能传递IntPtr

  3. */

  4. [DllImport("jnalib.dll", EntryPoint = "GetVersionArray")]

  5. public static extern bool GetVersionArray(IntPtr p, int nLen);

  6.  
  7. // 源目标参数

  8. OSINFO[] infos = new OSINFO[2];

  9. for (int i = 0; i < infos.Length; i++)

  10. {

  11. infos[i] = new OSINFO();

  12. }

  13.  
  14. IntPtr[] ptArr = new IntPtr[1];

  15. ptArr[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)) * 2); //分配包含两个元素的数组

  16. IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)));

  17. Marshal.Copy(ptArr, 0, pt, 1); //拷贝指针数组

  18. GetVersionArray(pt, 2); //调用

  19.  
  20. //还原成结构体数组

  21. for (int i = 0; i < 2; i++)

  22. {

  23. infos[i]=(OSINFO)Marshal.PtrToStructure((IntPtr)(pt.ToInt32()+i*Marshal.SizeOf(typeof(OSINFO))),typeof(OSINFO));

  24. Console.WriteLine("OsVersion:{0} szVersion:{1}", infos[i].osVersion, infos[i].szVersion);

  25. }


三. 复杂结构体的传递

 1. 输出参数,结构体作为指针传出

 
  1. typedef struct

  2. {

  3. char name[20];

  4. int age;

  5. double scores[30];

  6. }Student;

  7.  
  8. // Class中包含结构体数组类型

  9. typedef struct

  10. {

  11. int number;

  12. Student students[50];

  13. }Class;

  14.  
  15. // 传入复杂结构体测试

  16. JNAAPI int GetClass(Class *pClass,int len);

 
  1. // 接口定义

  2. [DllImport("jnalib.dll", EntryPoint = "GetClass")]

  3. public static extern int GetClass(IntPtr pv,int len);

  4.  
  5. // 结构体定义

  6. // Student

  7. [StructLayout(LayoutKind.Sequential)]

  8. public struct Student

  9. {

  10. [MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]

  11. public string name;

  12. public int age;

  13. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]

  14. public double[] scores;

  15. }

  16.  
  17. // Class

  18. [StructLayout(LayoutKind.Sequential)]

  19. public struct Class

  20. {

  21. public int number;

  22. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)] // 指定数组尺寸

  23. public Student[] students; // 结构体数组定义

  24. }

  25.  
  26. // 调用复杂结构体测试

  27. int size = Marshal.SizeOf(typeof(Class)) * 50;

  28. IntPtr pBuff = Marshal.AllocHGlobal(size); // 直接分配50个元素的空间,比Marshal.copy方便多了

  29. GetClass(pBuff, 50);

  30.  
  31. Class[] pClass = new Class[50];

  32. for (int i = 0; i < 50; i++)

  33. {

  34. IntPtr ptr = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);

  35. pClass[i] = (Class)Marshal.PtrToStructure(ptr, typeof(Class));

  36. }

  37. Marshal.FreeHGlobal(pBuff); // 释放内存

2. 输入参数, 给复杂结构体赋值后作为输入参数传入

   对于比较大的结构体指针,无法直接应用结构体类型,转化成IntPtr类型, 此时需要将原生类型转化为指针,并给指针赋值

   调用方法: Marshal.StructureToPtr(stu, ptr1, true) 

http://tcspecial.iteye.com/blog/1675309

猜你喜欢

转载自blog.csdn.net/qq_34106574/article/details/81388651