新聞中心
在使用第三方的非托管API時(shí),我們經(jīng)常會(huì)遇到參數(shù)為指針或指針的指針這種情況,

一般我們會(huì)用IntPtr指向我們需要傳遞的參數(shù)地址;
但是當(dāng)遇到這種一個(gè)導(dǎo)出函數(shù)時(shí),我們?nèi)绾握_的使用IntPtr呢,
extern "C" __declspec(dllexport) int GetClass(Class pClass[50]) ;
由于這種情況也經(jīng)??赡苡龅剑晕抑谱髁?個(gè)示例程序來演示下如何處理這種非托管函數(shù)的調(diào)用!
首先創(chuàng)建一個(gè)C++ 的DLL 設(shè)置一個(gè)如上的導(dǎo)出函數(shù)
- #include
- #include
- typedef struct Student
- {
- char name[20];
- int age;
- double scores[32];
- }Student;
- typedef struct Class
- {
- int number;
- Student students[126];
- }Class;
- extern "C" __declspec(dllexport) int GetClass(Class pClass[50])
- {
- for(int i=0;i<50;i++)
- {
- pClass[i].number=i;
- for(int j=0;j<126;j++)
- {
- memset(pClass[i].students[j].name,0,20);
- sprintf(pClass[i].students[j].name,"name_%d_%d",i,j);
- pClass[i].students[j].age=j%2==0?15:20;
- }
- }
- return 0;
- }
上面DLL 的導(dǎo)出函數(shù)要求傳遞的參數(shù)為它自定義的Class結(jié)構(gòu)體數(shù)組, 那么我們在C#調(diào)用它時(shí)也要自定義對(duì)應(yīng)的結(jié)構(gòu)體了,
我們可以定義為如下:
- [StructLayout(LayoutKind.Sequential)]
- struct Student
- {
- [MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]
- public string name;
- public int age;
- [MarshalAs(UnmanagedType.ByValArray,SizeConst=32)]
- public double[] scores;
- }
- [StructLayout(LayoutKind.Sequential)]
- struct Class
- {
- public int number;
- [MarshalAs(UnmanagedType.ByValArray,SizeConst=126)]
- public Student[] students;
- }
需要注意的是,這2個(gè)結(jié)構(gòu)體中的數(shù)組大小一定要跟C++中的限定一樣大小哦,接下來如何使用這個(gè)API來正確的獲取數(shù)據(jù)呢,大多數(shù)人可能想到像這樣的處理方式
- Class myclass = new Class();
- IntPtr ptr=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Class)));
- GetClass(ptr);
- Marshal.FreeHGlobal(ptr);
沒錯(cuò),這樣的處理是沒問題的,但是我們的API的參數(shù)是Class數(shù)組,這種處理方式只是傳遞一個(gè)Class結(jié)構(gòu)體參數(shù),所以這種方式在這里就不太合適了,!
那大家就想到先Class[] myclass = new Class[MaxClass]; 然后在用Marshal.AllocHGlobal 來獲取myclass 數(shù)據(jù)的指針,
其實(shí)這樣也是錯(cuò)的, 因?yàn)?Class結(jié)構(gòu)中包含了,不能直接封送的Student結(jié)構(gòu),所以無論如何上面的想法是錯(cuò)誤的!
那要怎么辦呢,其實(shí)很簡單,就是先分配一段非托管內(nèi)存,并調(diào)用API后,再將非托管內(nèi)容數(shù)據(jù)讀取到托管結(jié)構(gòu)體數(shù)據(jù)中!
示例C語言封送結(jié)構(gòu)體數(shù)組演示代碼如下
- 1 static void Main(string[] args)
- 2 {
- 3 int size = Marshal.SizeOf(typeof(Class)) * 50;
- 4 byte[] bytes = new byte[size];
- 5 IntPtr pBuff = Marshal.AllocHGlobal(size);
- 6 Class[] pClass = new Class[50];
- 7 GetClass(pBuff);
- 8 for (int i = 0; i < 50; i++)
- 9 {
- 10 IntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);
- 11 pClass[i] = (Class)Marshal.PtrToStructure(pPonitor, typeof(Class));
- 12 }
- 13 Marshal.FreeHGlobal(pBuff);
- 14 Console.ReadLine();
- 15 }
有興趣的不妨自己測試一下C語言封送結(jié)構(gòu)體數(shù)組,看看輸出結(jié)果是否正確!
【編輯推薦】
- 詳解C#中不同類的類型
- 淺談C#中標(biāo)準(zhǔn)Dispose模式的實(shí)現(xiàn)
- C#選擇正確的集合進(jìn)行編碼
- C# 4.0新特性:協(xié)變與逆變中的編程思想
- C#應(yīng)用Attribute特性 代碼統(tǒng)計(jì)分析
網(wǎng)頁題目:C語言封送結(jié)構(gòu)體數(shù)組
當(dāng)前網(wǎng)址:http://www.fisionsoft.com.cn/article/coidsde.html


咨詢
建站咨詢
