C#操作共享内存

 
  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using System.Threading.Tasks;

  6. using System.Runtime.InteropServices;

  7. namespace ConsoleApplication2

  8. {

  9.  
  10. class Program

  11. {

  12. [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Auto)]

  13. public static extern IntPtr CreateFile(string lpFileName,

  14. int dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes,

  15. int dwCreationDisposition, int dwFlagsAndAttributes,

  16. IntPtr hTemplateFile);

  17.  
  18. [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Auto)]

  19. public static extern IntPtr CreateFileMapping(IntPtr hFile,

  20. IntPtr lpAttributes, int flProtect,

  21. int dwMaximumSizeLow, int dwMaximumSizeHigh, string lpName);

  22. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

  23. private static extern IntPtr OpenFileMapping(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName);

  24. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

  25. private static extern IntPtr MapViewOfFile(IntPtr hFileMapping, uint dwDesiredAccess, uint dwFileOffsetHigh, uint dwFileOffsetLow, uint dwNumberOfBytesToMap);

  26. [DllImport("kernel32", EntryPoint = "GetLastError")]

  27. public static extern int GetLastError();

  28. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

  29. public static extern IntPtr CreateFileMapping(int hFile, IntPtr lpAttributes, uint flProtect, uint dwMaxSizeHi, uint dwMaxSizeLow, string lpName);

  30. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

  31. public static extern bool UnmapViewOfFile(IntPtr pvBaseAddress);

  32. [DllImport("Kernel32.dll", CharSet = CharSet.Auto)]

  33. public static extern bool CloseHandle(IntPtr handle);

  34. //原文链接:https://blog.csdn.net/gao271003105/article/details/81014094

  35. public const int FILE_MAP_READ = 0x0004;

  36.  
  37.  
  38. public static IntPtr fileMapping = IntPtr.Zero;

  39. public static IntPtr mapView = IntPtr.Zero;

  40. const int FILE_MAP_COPY = 0x0001;

  41. const int FILE_MAP_WRITE = 0x0002;

  42. // const int FILE_MAP_READ = 0x0004;

  43. const int FILE_MAP_ALL_ACCESS = 0x0002 | 0x0004;

  44. const int PAGE_READWRITE = 0x04;

  45. const int INVALID_HANDLE_VALUE = -1;

  46. const int ERROR_ALREADY_EXISTS = 183;

  47. private bool bValid;

  48. static bool m_bAlreadyExist = false;

  49. static bool m_bInit = false;

  50. static long m_MemSize = 0;

  51. public void ShareMemoryHelper(string name, uint length)

  52. {

  53. bValid = GetShareMemoryMap(name, length);

  54. }

  55. public void ShareMemoryHelper()

  56. {

  57. }

  58.  
  59. // ~ShareMemoryHelper()

  60. // {

  61. // Close();

  62. // }

  63. public bool GetShareMemoryMap(string name, uint length)

  64. {

  65. m_MemSize = length;

  66. fileMapping = OpenFileMapping(PAGE_READWRITE, false, name);

  67. if (fileMapping == IntPtr.Zero)

  68. {

  69. return false;

  70. }

  71. mapView = MapViewOfFile(fileMapping, (uint)FILE_MAP_READ, 0, 0, length);

  72. if (mapView == IntPtr.Zero)

  73. {

  74. int a = GetLastError();

  75. return false;

  76. }

  77. m_bInit = true;

  78. return true;

  79. }

  80.  
  81. public static int CreateShareMemoryMap(string strName, long lngSize)

  82. {

  83. if (lngSize <= 0 || lngSize > 0x00800000) lngSize = 0x00800000;

  84. m_MemSize = lngSize;

  85. if (strName.Length > 0)

  86. {

  87. fileMapping = CreateFileMapping(INVALID_HANDLE_VALUE, IntPtr.Zero, (uint)

  88.  
  89. PAGE_READWRITE, 0, (uint)lngSize, strName);

  90. if (fileMapping == IntPtr.Zero)

  91. {

  92. return 2;

  93. }

  94.  
  95. if (GetLastError() == ERROR_ALREADY_EXISTS)

  96. {

  97. m_bAlreadyExist = true;

  98. }

  99. else

  100. {

  101. m_bAlreadyExist = false;

  102. }

  103. mapView = MapViewOfFile(fileMapping, FILE_MAP_WRITE, 0, 0, (uint)lngSize);

  104. if (mapView == IntPtr.Zero)

  105. {

  106. m_bInit = false;

  107. CloseHandle(fileMapping);

  108. return 3;

  109. }

  110. else

  111. {

  112. m_bInit = true;

  113. if (m_bAlreadyExist == false)

  114. {

  115. }

  116. }

  117. }

  118. else

  119. {

  120. return 1;

  121. }

  122. return 0;

  123. }

  124.  
  125. public static int Write(byte[] bytData, int lngAddr, int lngSize)

  126. {

  127. if (lngAddr + lngSize > m_MemSize) return 2;

  128. if (m_bInit)

  129. {

  130. Marshal.Copy(bytData, lngAddr, mapView, lngSize);

  131. }

  132. else

  133. {

  134. return 1;

  135. }

  136. return 0;

  137. }

  138.  
  139. public static int Read(ref byte[] bytData, int lngAddr, int lngSize)

  140. {

  141. if (lngAddr + lngSize > m_MemSize) return 2;

  142. if (m_bInit)

  143. {

  144. Marshal.Copy(mapView, bytData, lngAddr, lngSize);

  145. }

  146. else

  147. {

  148. return 1;

  149. }

  150. return 0;

  151. }

  152.  
  153. public static void Close()

  154. {

  155. if (m_bInit)

  156. {

  157. UnmapViewOfFile(mapView);

  158. CloseHandle(fileMapping);

  159. }

  160. }

  161. static void Main(string[] args)

  162. {

  163.  
  164. CreateShareMemoryMap("sss", 200);

  165. byte[] ss = { 0,0,0,0,0 };

  166. //Write(ss, 0, 2);

  167. int k= Read(ref ss, 0, 5);

  168. int st = ss[1];

  169. Console.ReadLine();

  170. Close();

  171.  
  172. }

  173. }

  174. }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值