标签:class java http tar ext com
因为公司一直都是做C++开发的,因客户需要要提供C#版本接口,研究了一下C#,发现其强大简洁, 在跨语言调用方面封装的很彻底,提供了强大的API与之交互.这点比JNA方便多了. Java与C#都只能调用C格式导出动态库,因为C数据类型比较单一,容易映射. 两者都是在本地端提供一套与之映射的C#/java描述接口,通过底层处理这种映射关系达到调用的目的.
一. 结构体的传递
- #define JNAAPI extern "C" __declspec(dllexport) // C方式导出函数
-
- typedef struct
- {
- int osVersion;
- int majorVersion;
- int minorVersion;
- int buildNum;
- int platFormId;
- char szVersion[128];
- }OSINFO;
-
-
- JNAAPI bool GetVersionPtr( OSINFO *info );
-
- JNAAPI bool GetVersionRef(OSINFO &info);
可以通过二种方式来调用:
-
- [StructLayout(LayoutKind.Sequential)]
- public struct OSINFO
- {
- public int osVersion;
- public int majorVersion;
- public int minorVersion;
- public int buildNum;
- public int platFormId;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
- public string szVersion;
- }
1. 方式一(传入结构体引用),在C#中,结构体是以传值方式传递,类才是以传地址方式传递,加关键字ref即可. C端传递了两种不同类型的参数,都可以通过引用来解决.
- [DllImport("jnalib.dll", EntryPoint = "GetVersionPtr")]
- public static extern bool GetVersionPtr(ref OSINFO info);
- public static extern bool GetVersionRef(ref OSINFO info);
2. 方式二(传入IntPtr(平台通用指针))
- IntPtr pv = Marshal.AllocHGlobal(148);
- Marshal.WriteInt32(pv,148);
- if (GetVersionPtr(pv))
- {
- Console.WriteLine("--osVersion:{0}", Marshal.ReadInt32(pv, 0));
- Console.WriteLine("--Major:{0}",Marshal.ReadInt32(pv, 4));
- Console.WriteLine("--BuildNum: " + Marshal.ReadInt32(pv, 12));
- Console.WriteLine("--szVersion: "+Marshal.PtrToStringAnsi((IntPtr)(pv.ToInt32()+20)));
- }
- Marshal.FreeHGlobal(pv);
二.结构体数组的传递
-
- JNAAPI bool GetVersionArray(OSINFO *info,int nLen);
调用代码:
-
-
-
- [DllImport("jnalib.dll", EntryPoint = "GetVersionArray")]
- public static extern bool GetVersionArray(IntPtr p, int nLen);
-
-
- OSINFO[] infos = new OSINFO[2];
- for (int i = 0; i < infos.Length; i++)
- {
- infos[i] = new OSINFO();
- }
-
- IntPtr[] ptArr = new IntPtr[1];
- ptArr[0] = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)) * 2);
- IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)));
- Marshal.Copy(ptArr, 0, pt, 1);
- GetVersionArray(pt, 2);
-
-
- for (int i = 0; i < 2; i++)
- {
- infos[i]=(OSINFO)Marshal.PtrToStructure((IntPtr)(pt.ToInt32()+i*Marshal.SizeOf(typeof(OSINFO))),typeof(OSINFO));
- Console.WriteLine("OsVersion:{0} szVersion:{1}", infos[i].osVersion, infos[i].szVersion);
- }
三. 复杂结构体的传递
1. 输出参数,结构体作为指针传出
- typedef struct
- {
- char name[20];
- int age;
- double scores[30];
- }Student;
-
-
- typedef struct
- {
- int number;
- Student students[50];
- }Class;
-
-
- JNAAPI int GetClass(Class *pClass,int len);
-
- [DllImport("jnalib.dll", EntryPoint = "GetClass")]
- public static extern int GetClass(IntPtr pv,int len);
-
-
-
- [StructLayout(LayoutKind.Sequential)]
- public struct Student
- {
- [MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]
- public string name;
- public int age;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 30)]
- public double[] scores;
- }
-
-
- [StructLayout(LayoutKind.Sequential)]
- public struct Class
- {
- public int number;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
- public Student[] students;
- }
-
-
- int size = Marshal.SizeOf(typeof(Class)) * 50;
- IntPtr pBuff = Marshal.AllocHGlobal(size);
- GetClass(pBuff, 50);
-
- Class[] pClass = new Class[50];
- for (int i = 0; i < 50; i++)
- {
- IntPtr ptr = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);
- pClass[i] = (Class)Marshal.PtrToStructure(ptr, typeof(Class));
- }
- Marshal.FreeHGlobal(pBuff);
2. 输入参数, 给复杂结构体赋值后作为输入参数传入
对于比较大的结构体指针,无法直接应用结构体类型,转化成IntPtr类型, 此时需要将原生类型转化为指针,并给指针赋值
调用方法: Marshal.StructureToPtr(stu, ptr1, true)
C#调用C/C++动态库 封送结构体,结构体数组,布布扣,bubuko.com
C#调用C/C++动态库 封送结构体,结构体数组
标签:class java http tar ext com
原文地址:http://www.cnblogs.com/gc2013/p/3764699.html