現(xiàn)在公司要做一個(gè)使用C#程序調(diào)用C++的一個(gè)DLL庫,。所以在網(wǎng)上找了一些資料,。
一,、結(jié)構(gòu)體傳遞
- #define JNAAPI extern "C" __declspec(dllexport) // C方式導(dǎo)出函數(shù)
-
- typedef struct
- {
- int osVersion;
- int majorVersion;
- int minorVersion;
- int buildNum;
- int platFormId;
- char szVersion[128];
- }OSINFO;
-
- // 1. 獲取版本信息(傳遞結(jié)構(gòu)體指針)
- JNAAPI bool GetVersionPtr( OSINFO *info );
- // 2.獲取版本信息(傳遞結(jié)構(gòu)體引用)
- JNAAPI bool GetVersionRef(OSINFO &info);
可以通過二種方式進(jìn)行調(diào)用:
- // OSINFO定義
- [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. 方式一(傳入結(jié)構(gòu)體引用),在C#中,結(jié)構(gòu)體是以傳值方式傳遞,類才是以傳地址方式傳遞,加關(guān)鍵字ref即可.
C端傳遞了兩種不同類型的參數(shù),都可以通過引用來解決.
- [DllImport("jnalib.dll", EntryPoint = "GetVersionPtr")]
- public static extern bool GetVersionPtr(ref OSINFO info);
- public static extern bool GetVersionRef(ref OSINFO info);
2. 方式二(傳入IntPtr(平臺(tái)通用指針))
- IntPtr pv = Marshal.AllocHGlobal(148); //結(jié)構(gòu)體在使用時(shí)一定要分配空間(4*sizeof(int)+128)
- Marshal.WriteInt32(pv,148); //向內(nèi)存塊里寫入數(shù)值
- if (GetVersionPtr(pv)) //直接以非托管內(nèi)存塊地址為參數(shù)
- {
- Console.WriteLine("--osVersion:{0}", Marshal.ReadInt32(pv, 0));
- Console.WriteLine("--Major:{0}",Marshal.ReadInt32(pv, 4)); //移動(dòng)4個(gè)字節(jié)
- Console.WriteLine("--BuildNum: " + Marshal.ReadInt32(pv, 12));
- Console.WriteLine("--szVersion: "+Marshal.PtrToStringAnsi((IntPtr)(pv.ToInt32()+20)));
- }
- Marshal.FreeHGlobal(pv); //處理完記得釋放內(nèi)存
二.結(jié)構(gòu)體數(shù)組的傳遞
- // 傳遞結(jié)構(gòu)體指針
- JNAAPI bool GetVersionArray(OSINFO *info,int nLen);
調(diào)用:
- /**
- * C#接口,對(duì)于包含數(shù)組類型,只能傳遞IntPtr
- */
- [DllImport("jnalib.dll", EntryPoint = "GetVersionArray")]
- public static extern bool GetVersionArray(IntPtr p, int nLen);
-
- // 源目標(biāo)參數(shù)
- 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); //分配包含兩個(gè)元素的數(shù)組
- IntPtr pt = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(OSINFO)));
- Marshal.Copy(ptArr, 0, pt, 1); //拷貝指針數(shù)組
- GetVersionArray(pt, 2); //調(diào)用
-
- //還原成結(jié)構(gòu)體數(shù)組
- 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);
- }
三. 復(fù)雜結(jié)構(gòu)體的傳遞
1. 輸出參數(shù),結(jié)構(gòu)體作為指針傳出
- typedef struct
- {
- char name[20];
- int age;
- double scores[30];
- }Student;
-
- // Class中包含結(jié)構(gòu)體數(shù)組類型
- typedef struct
- {
- int number;
- Student students[50];
- }Class;
-
- // 傳入復(fù)雜結(jié)構(gòu)體測(cè)試
- JNAAPI int GetClass(Class *pClass,int len);
- // 接口定義
- [DllImport("jnalib.dll", EntryPoint = "GetClass")]
- public static extern int GetClass(IntPtr pv,int len);
-
- // 結(jié)構(gòu)體定義
- // Student
- [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;
- }
-
- // Class
- [StructLayout(LayoutKind.Sequential)]
- public struct Class
- {
- public int number;
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)] // 指定數(shù)組尺寸
- public Student[] students; // 結(jié)構(gòu)體數(shù)組定義
- }
-
- // 調(diào)用復(fù)雜結(jié)構(gòu)體測(cè)試
- int size = Marshal.SizeOf(typeof(Class)) * 50;
- IntPtr pBuff = Marshal.AllocHGlobal(size); // 直接分配50個(gè)元素的空間,比Marshal.copy方便多了
- 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); // 釋放內(nèi)存
2. 輸入?yún)?shù), 給復(fù)雜結(jié)構(gòu)體賦值后作為輸入?yún)?shù)傳入
對(duì)于比較大的結(jié)構(gòu)體指針,無法直接應(yīng)用結(jié)構(gòu)體類型,轉(zhuǎn)化成IntPtr類型, 此時(shí)需要將原生類型轉(zhuǎn)化為指針,并給指針賦值
調(diào)用方法: Marshal.StructureToPtr(stu, ptr1, true)
end...
轉(zhuǎn)自:http://tcspecial./blog/1675309
|