日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

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

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
C#共享內(nèi)存操作類實例淺析

C#共享內(nèi)存操作是如何實現(xiàn)的呢?VC++的共享內(nèi)存操作代碼實現(xiàn)起來相對比較容易,但是用C#語言來實現(xiàn),就有一定難度,由于工作需要,把以前VC開發(fā)的共享內(nèi)存代碼要用C#實現(xiàn),別說,還費了不少周折,畢竟C#操作API函數(shù)和地址指針不是那么直接,還好,總算完成了,效果還不錯。

C#共享內(nèi)存操作類實例:

 
 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Runtime.InteropServices;  
  5.  
  6. namespace ShareMemLib  
  7. {  
  8. public class ShareMem  
  9. {  
  10. [DllImport("user32.dll", CharSet = CharSet.Auto)]  
  11. public static extern IntPtr SendMessage(IntPtr hWnd,   
  12. int Msg, int wParam, IntPtr lParam);  
  13.  
  14. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]  
  15. public static extern IntPtr CreateFileMapping(  
  16. int hFile, IntPtr lpAttributes, uint flProtect,   
  17. uint dwMaxSizeHi, uint dwMaxSizeLow, string lpName);  
  18.  
  19. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]  
  20. public static extern IntPtr OpenFileMapping(  
  21. int dwDesiredAccess,  
  22. [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,string lpName);  
  23.  
  24. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]  
  25. public static extern IntPtr MapViewOfFile(IntPtr   
  26. hFileMapping,uint dwDesiredAccess,  
  27.  uint dwFileOffsetHigh, uint dwFileOffsetLow,  
  28. uint dwNumberOfBytesToMap);  
  29.  
  30. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]  
  31. public static extern bool UnmapViewOfFile(IntPtr pvBaseAddress);  
  32.  
  33. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]  
  34. public static extern bool CloseHandle(IntPtr handle);  
  35.  
  36. [DllImport("kernel32", EntryPoint="GetLastError")]  
  37. public static extern int GetLastError ();  
  38.  
  39. const int ERROR_ALREADY_EXISTS = 183;  
  40.  
  41. const int FILE_MAP_COPY = 0x0001;  
  42. const int FILE_MAP_WRITE = 0x0002;  
  43. const int FILE_MAP_READ = 0x0004;  
  44. const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004;  
  45.  
  46. const int PAGE_READONLY = 0x02;  
  47. const int PAGE_READWRITE = 0x04;  
  48. const int PAGE_WRITECOPY = 0x08;  
  49. const int PAGE_EXECUTE = 0x10;  
  50. const int PAGE_EXECUTE_READ = 0x20;  
  51. const int PAGE_EXECUTE_READWRITE = 0x40;  
  52.  
  53. const int SEC_COMMIT = 0x8000000;  
  54. const int SEC_IMAGE = 0x1000000;  
  55. const int SEC_NOCACHE = 0x10000000;  
  56. const int SEC_RESERVE = 0x4000000;  
  57.  
  58. const int INVALID_HANDLE_VALUE = -1;  
  59.  
  60. IntPtr m_hSharedMemoryFile = IntPtr.Zero;  
  61. IntPtr m_pwData = IntPtr.Zero;  
  62. bool m_bAlreadyExist = false;  
  63. bool m_bInit = false;  
  64. long m_MemSize=0;  
  65.  
  66. public ShareMem()  
  67. {  
  68. }  
  69. ~ShareMem()  
  70. {  
  71. Close();  
  72. }  
  73.  
  74. /// ﹤summary﹥  
  75. /// 初始化共享內(nèi)存  
  76. /// ﹤/summary﹥  
  77. /// ﹤param name="strName"﹥共享內(nèi)存名稱﹤/param﹥  
  78. /// ﹤param name="lngSize"﹥共享內(nèi)存大小﹤/param﹥  
  79. /// ﹤returns﹥﹤/returns﹥  
  80. public int Init(string strName, long lngSize)  
  81. {  
  82. if (lngSize ﹤= 0 || lngSize ﹥ 0x00800000) lngSize = 0x00800000;  
  83. m_MemSize = lngSize;  
  84. if (strName.Length ﹥ 0)  
  85. {  
  86. //創(chuàng)建內(nèi)存共享體(INVALID_HANDLE_VALUE)  
  87. m_hSharedMemoryFile = CreateFileMapping(  
  88. INVALID_HANDLE_VALUE, IntPtr.Zero,   
  89. (uint)PAGE_READWRITE, 0, (uint)lngSize, strName);  
  90. if (m_hSharedMemoryFile == IntPtr.Zero)  
  91. {  
  92. m_bAlreadyExist = false;  
  93. m_bInit = false;  
  94. return 2; //創(chuàng)建共享體失敗  
  95. }  
  96. else 
  97. {  
  98. if (GetLastError() == ERROR_ALREADY_EXISTS)  //已經(jīng)創(chuàng)建  
  99. {  
  100. m_bAlreadyExist = true;  
  101. }  
  102. else //新創(chuàng)建  
  103. {  
  104. m_bAlreadyExist = false;  
  105. }  
  106. }  
  107. //---------------------------------------  
  108. //創(chuàng)建內(nèi)存映射  
  109. m_pwData = MapViewOfFile(m_hSharedMemoryFile,   
  110. FILE_MAP_WRITE, 0, 0, (uint)lngSize);  
  111. if (m_pwData == IntPtr.Zero)  
  112. {  
  113. m_bInit = false;  
  114. CloseHandle(m_hSharedMemoryFile);  
  115. return 3; //創(chuàng)建內(nèi)存映射失敗  
  116. }  
  117. else 
  118. {  
  119. m_bInit = true;  
  120. if (m_bAlreadyExist == false)  
  121. {  
  122. //初始化  
  123. }  
  124. }  
  125. //----------------------------------------  
  126. }  
  127. else 
  128. {  
  129. return 1; //參數(shù)錯誤   
  130. }  
  131.  
  132. return 0; //創(chuàng)建成功  
  133. }  
  134. /// ﹤summary﹥  
  135. /// 關(guān)閉共享內(nèi)存  
  136. /// ﹤/summary﹥  
  137. public void Close()  
  138. {  
  139. if (m_bInit)  
  140. {  
  141. UnmapViewOfFile(m_pwData);  
  142. CloseHandle(m_hSharedMemoryFile);  
  143. }  
  144. }  
  145.  
  146. /// ﹤summary﹥  
  147. /// 讀數(shù)據(jù)  
  148. /// ﹤/summary﹥  
  149. /// ﹤param name="bytData"﹥數(shù)據(jù)﹤/param﹥  
  150. /// ﹤param name="lngAddr"﹥起始地址﹤/param﹥  
  151. /// ﹤param name="lngSize"﹥個數(shù)﹤/param﹥  
  152. /// ﹤returns﹥﹤/returns﹥  
  153. public int Read(ref byte[] bytData,  
  154.  int lngAddr, int lngSize)  
  155. {  
  156. if (lngAddr + lngSize ﹥ m_MemSize) return 2;   
  157. //超出數(shù)據(jù)區(qū)  
  158. if (m_bInit)  
  159. {     
  160. Marshal.Copy(m_pwData, bytData, lngAddr, lngSize);  
  161. }  
  162. else 
  163. {  
  164. return 1; //共享內(nèi)存未初始化  
  165. }  
  166. return 0; //讀成功  
  167. }  
  168.  
  169. /// ﹤summary﹥  
  170. /// 寫數(shù)據(jù)  
  171. /// ﹤/summary﹥  
  172. /// ﹤param name="bytData"﹥數(shù)據(jù)﹤/param﹥  
  173. /// ﹤param name="lngAddr"﹥起始地址﹤/param﹥  
  174. /// ﹤param name="lngSize"﹥個數(shù)﹤/param﹥  
  175. /// ﹤returns﹥﹤/returns﹥  
  176. public int Write(byte[] bytData, int lngAddr, int lngSize)  
  177. {  
  178. if (lngAddr + lngSize ﹥ m_MemSize) return 2; //超出數(shù)據(jù)區(qū)  
  179. if (m_bInit)  
  180. {  
  181. Marshal.Copy(bytData, lngAddr, m_pwData, lngSize);  
  182. }  
  183. else 
  184. {  
  185. return 1; //共享內(nèi)存未初始化  
  186. }  
  187. return 0; //寫成功  
  188. }  
  189. }  

C#共享內(nèi)存操作類實例測試例程:

 
 
 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using ShareMemLib;  
  9.  
  10. namespace YFShareMem  
  11. {  
  12. public partial class frmShareMem : Form  
  13. {  
  14. ShareMem MemDB=new ShareMem();  
  15. public frmShareMem()  
  16. {  
  17. InitializeComponent();  
  18. }  
  19.  
  20. private void btnOpen_Click(object sender, EventArgs e)  
  21. {  
  22. if (MemDB.Init("YFMemTest", 10240) != 0)  
  23. {  
  24. //初始化失敗  
  25. MessageBox.Show("初始化失敗");  
  26. }  
  27. else 
  28. {  
  29. btnOpen.Enabled = false;  
  30. chkWrite.Enabled = true;  
  31. tmrTime.Enabled = true;  
  32. }  
  33. }  
  34.  
  35. private void tmrTime_Tick(object sender, EventArgs e)  
  36. {  
  37. byte[] bytData = new byte[16];  
  38. int intRet = MemDB.Read(ref bytData, 0, 16);  
  39. lstData.Items.Clear();   
  40. if (intRet == 0)  
  41. {  
  42. for (int i = 0; i ﹤ 16; i++)  
  43. {  
  44. lstData.Items.Add(bytData[i].ToString());  
  45. }  
  46.  
  47. if (chkWrite.Checked)  
  48. {  
  49. bytData[0]++;  
  50. bytData[1] += 2;  
  51. if (bytData[0] ﹥ 200) bytData[0] = 0;  
  52. if (bytData[1] ﹥ 200) bytData[1] = 0;  
  53. MemDB.Write(bytData, 0, 16);  
  54. }  
  55. }     
  56. }  
  57.  
  58. }  

C#共享內(nèi)存操作類的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C#共享內(nèi)存操作類有所幫助。

【編輯推薦】

  1. C#操作文本文件應(yīng)用實例簡析
  2. C#操作文本文件演練實例淺析
  3. C#操作內(nèi)存之指針淺析
  4. C#操作內(nèi)存讀寫方法淺析
  5. C#操作內(nèi)存實例詳解

新聞標題:C#共享內(nèi)存操作類實例淺析
鏈接分享:http://www.dlmjj.cn/article/cdpcdhi.html