You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.0 KiB

  1. #if NET40 || NET461
  2. using Apewer.Internals.Interop;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. namespace Apewer.Surface
  9. {
  10. /// <summary></summary>
  11. internal class FileMapping
  12. {
  13. /// <summary>创建内存映射。</summary>
  14. /// <param name="path">文件路径。</param>
  15. /// <param name="handle">内存映射句柄。</param>
  16. /// <param name="block">块大小。</param>
  17. /// <returns>映射成功。</returns>
  18. internal bool Create(string path, ref IntPtr handle, ref UInt32 block)
  19. {
  20. var DesiredAccess = Constant.GENERIC_READ | Constant.GENERIC_WRITE;
  21. var ShareMode = FileShare.ReadWrite;
  22. var CreationDesposition = FileMode.OpenOrCreate;
  23. var FlagsAndAttributes = Constant.FILE_ATTRIBUTE_NORMAL | Constant.FILE_FLAG_SEQUENTIAL_SCAN;
  24. if (File.Exists(path))
  25. {
  26. try
  27. {
  28. var fh = Kernel32.CreateFile(path, DesiredAccess, ShareMode, IntPtr.Zero, CreationDesposition, FlagsAndAttributes, IntPtr.Zero);
  29. if (Constant.INVALID_HANDLE_VALUE != (int)fh)
  30. {
  31. // 创建内存句柄。
  32. var mh = Kernel32.CreateFileMapping(fh, IntPtr.Zero, Constant.PAGE_READWRITE, 0, 0, Guid.NewGuid().ToString());
  33. // 获取系统信息。
  34. var si = new SystemInfo();
  35. Kernel32.GetSystemInfo(ref si);
  36. // 得到系统页分配粒度。
  37. uint ag = si.dwAllocationGranularity;
  38. uint flh = 0;
  39. // 获取文件长度。
  40. uint fl = Kernel32.GetFileSize(fh, out flh);
  41. fl |= (((uint)flh) << 32);
  42. // 关闭文件句柄 。
  43. Kernel32.CloseHandle(fh);
  44. uint bl = 1000 * ag;
  45. if (fl < 1000 * ag) bl = fl;
  46. // 返回。
  47. handle = mh;
  48. block = bl;
  49. return true;
  50. }
  51. }
  52. catch { }
  53. }
  54. handle = IntPtr.Zero;
  55. block = 0;
  56. return false;
  57. }
  58. /// <summary>从内存映射中读取数据。</summary>
  59. /// <param name="handle">内存映射句柄。</param>
  60. /// <param name="block">块大小。</param>
  61. /// <param name="offset">读取的位置。</param>
  62. /// <param name="length">读取的长度。</param>
  63. /// <returns>读取的结果。</returns>
  64. internal byte[] Read(IntPtr handle, UInt32 block, Int32 offset, Int32 length)
  65. {
  66. var DesiredAccess = (uint)(Constant.FILE_MAP_COPY | Constant.FILE_MAP_READ | Constant.FILE_MAP_WRITE);
  67. if ((handle != IntPtr.Zero) && (block > 0) && (offset > 0) && (length > 0))
  68. {
  69. try
  70. {
  71. // 映射视图,得到地址 。
  72. var address = Kernel32.MapViewOfFile(handle, DesiredAccess, (uint)(offset >> 32), (uint)(offset & 0xFFFFFFFF), block);
  73. if (address != IntPtr.Zero)
  74. {
  75. // 从非托管的内存中复制内容到托管的内存中。
  76. var start = offset;
  77. var buffer = new byte[block];
  78. Marshal.Copy(address, buffer, start, (int)length);
  79. return buffer;
  80. }
  81. }
  82. catch { }
  83. }
  84. return new byte[0];
  85. }
  86. /// <summary>关闭内存映射。</summary>
  87. /// <param name="handle">内存映射句柄。</param>
  88. internal static void Close(IntPtr handle)
  89. {
  90. if (handle == IntPtr.Zero) return;
  91. try { Kernel32.CloseHandle(handle); } catch { }
  92. }
  93. }
  94. }
  95. #endif