日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C語言封送結(jié)構(gòu)體數(shù)組

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

一般我們會用IntPtr指向我們需要傳遞的參數(shù)地址;

但是當遇到這種一個導(dǎo)出函數(shù)時,我們?nèi)绾握_的使用IntPtr呢,

extern "C" __declspec(dllexport) int GetClass(Class pClass[50]) ;

由于這種情況也經(jīng)??赡苡龅剑晕抑谱髁?個示例程序來演示下如何處理這種非托管函數(shù)的調(diào)用!

首先創(chuàng)建一個C++ 的DLL  設(shè)置一個如上的導(dǎo)出函數(shù)

 
 
 
  1. #include   
  2. #include   
  3.  
  4. typedef struct Student  
  5. {  
  6.     char name[20];  
  7.     int age;  
  8.     double scores[32];  
  9. }Student;  
  10.  
  11. typedef struct Class  
  12. {  
  13.     int number;  
  14.     Student students[126];  
  15. }Class;  
  16.  
  17. extern "C" __declspec(dllexport) int GetClass(Class pClass[50])  
  18. {  
  19.     for(int i=0;i<50;i++)  
  20.     {  
  21.         pClass[i].number=i;  
  22.        for(int j=0;j<126;j++)  
  23.         {  
  24.             memset(pClass[i].students[j].name,0,20);  
  25.             sprintf(pClass[i].students[j].name,"name_%d_%d",i,j);  
  26.            pClass[i].students[j].age=j%2==0?15:20;  
  27.         }  
  28.     }  
  29.     return 0;  
  30. }  

上面DLL 的導(dǎo)出函數(shù)要求傳遞的參數(shù)為它自定義的Class結(jié)構(gòu)體數(shù)組, 那么我們在C#調(diào)用它時也要自定義對應(yīng)的結(jié)構(gòu)體了,

我們可以定義為如下:

 
 
 
  1. [StructLayout(LayoutKind.Sequential)]  
  2.        struct Student  
  3.       {  
  4.            [MarshalAs(UnmanagedType.ByValTStr,SizeConst=20)]  
  5.            public string name;  
  6.            public int age;  
  7.            [MarshalAs(UnmanagedType.ByValArray,SizeConst=32)]  
  8.            public double[] scores;  
  9.        }  
  10.        [StructLayout(LayoutKind.Sequential)]  
  11.        struct Class  
  12.        {  
  13.           public int number;  
  14.            [MarshalAs(UnmanagedType.ByValArray,SizeConst=126)]  
  15.           public Student[] students;  
  16.  
  17.        }  

需要注意的是,這2個結(jié)構(gòu)體中的數(shù)組大小一定要跟C++中的限定一樣大小哦,接下來如何使用這個API來正確的獲取數(shù)據(jù)呢,大多數(shù)人可能想到像這樣的處理方式

 
 
 
  1. Class myclass = new Class();  
  2.             IntPtr ptr=Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Class)));  
  3.             GetClass(ptr);  
  4.             Marshal.FreeHGlobal(ptr);  

沒錯,這樣的處理是沒問題的,但是我們的API的參數(shù)是Class數(shù)組,這種處理方式只是傳遞一個Class結(jié)構(gòu)體參數(shù),所以這種方式在這里就不太合適了,!

 那大家就想到先Class[] myclass = new Class[MaxClass]; 然后在用Marshal.AllocHGlobal 來獲取myclass 數(shù)據(jù)的指針,

其實這樣也是錯的, 因為 Class結(jié)構(gòu)中包含了,不能直接封送的Student結(jié)構(gòu),所以無論如何上面的想法是錯誤的!

那要怎么辦呢,其實很簡單,就是先分配一段非托管內(nèi)存,并調(diào)用API后,再將非托管內(nèi)容數(shù)據(jù)讀取到托管結(jié)構(gòu)體數(shù)據(jù)中!

示例C語言封送結(jié)構(gòu)體數(shù)組演示代碼如下

 
 
 
  1.  1  static void Main(string[] args)  
  2.  2         {  
  3.  3             int size = Marshal.SizeOf(typeof(Class)) * 50;  
  4.  4             byte[] bytes = new byte[size];  
  5.  5             IntPtr pBuff = Marshal.AllocHGlobal(size);  
  6.  6             Class[] pClass = new Class[50];  
  7.  7             GetClass(pBuff);  
  8.  8             for (int i = 0; i < 50; i++)  
  9.  9             {  
  10. 10                 IntPtr pPonitor = new IntPtr(pBuff.ToInt64() + Marshal.SizeOf(typeof(Class)) * i);  
  11. 11                 pClass[i] = (Class)Marshal.PtrToStructure(pPonitor, typeof(Class));  
  12. 12             }  
  13. 13             Marshal.FreeHGlobal(pBuff);  
  14. 14             Console.ReadLine();  
  15. 15         }  

有興趣的不妨自己測試一下C語言封送結(jié)構(gòu)體數(shù)組,看看輸出結(jié)果是否正確!

【編輯推薦】

  1. 詳解C#中不同類的類型
  2. 淺談C#中標準Dispose模式的實現(xiàn)
  3. C#選擇正確的集合進行編碼
  4. C# 4.0新特性:協(xié)變與逆變中的編程思想
  5. C#應(yīng)用Attribute特性 代碼統(tǒng)計分析

名稱欄目:C語言封送結(jié)構(gòu)體數(shù)組
標題網(wǎng)址:http://www.5511xx.com/article/coidsde.html