Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

302 rader
10 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using Microsoft.Win32.SafeHandles;
  22. using System;
  23. using System.Runtime.InteropServices;
  24. namespace Alphaleonis.Win32
  25. {
  26. /// <summary>Base class for classes representing a block of unmanaged memory.</summary>
  27. internal abstract class SafeNativeMemoryBufferHandle : SafeHandleZeroOrMinusOneIsInvalid
  28. {
  29. #region Private Fields
  30. private int m_capacity;
  31. #endregion
  32. #region Constructors
  33. protected SafeNativeMemoryBufferHandle(bool ownsHandle)
  34. : base(ownsHandle)
  35. {
  36. }
  37. /// <summary>Initializes a new instance of the <see cref="SafeNativeMemoryBufferHandle"/> specifying the allocated capacity of the memory block.</summary>
  38. /// <param name="capacity">The capacity.</param>
  39. protected SafeNativeMemoryBufferHandle(int capacity)
  40. : this(true)
  41. {
  42. m_capacity = capacity;
  43. }
  44. protected SafeNativeMemoryBufferHandle(IntPtr memory, int capacity)
  45. : this(capacity)
  46. {
  47. SetHandle(memory);
  48. }
  49. #endregion
  50. #region Properties
  51. /// <summary>Gets the capacity. Only valid if this instance was created using a constructor that specifies the size,
  52. /// it is not correct if this handle was returned by a native method using p/invoke.
  53. /// </summary>
  54. public int Capacity
  55. {
  56. get { return m_capacity; }
  57. }
  58. #endregion
  59. #region Public Methods
  60. /// <summary>Copies data from a one-dimensional, managed 8-bit unsigned integer array to the unmanaged memory pointer referenced by this instance.</summary>
  61. /// <param name="source">The one-dimensional array to copy from. </param>
  62. /// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
  63. /// <param name="length">The number of array elements to copy.</param>
  64. public void CopyFrom(byte[] source, int startIndex, int length)
  65. {
  66. Marshal.Copy(source, startIndex, handle, length);
  67. }
  68. public void CopyFrom(char[] source, int startIndex, int length)
  69. {
  70. Marshal.Copy(source, startIndex, handle, length);
  71. }
  72. public void CopyFrom(char[] source, int startIndex, int length, int offset)
  73. {
  74. Marshal.Copy(source, startIndex, new IntPtr(handle.ToInt64() + offset), length);
  75. }
  76. /// <summary>Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.</summary>
  77. /// <param name="destination">The array to copy to.</param>
  78. /// <param name="destinationOffset">The zero-based index in the destination array where copying should start.</param>
  79. /// <param name="length">The number of array elements to copy.</param>
  80. public void CopyTo(byte[] destination, int destinationOffset, int length)
  81. {
  82. if (destination == null)
  83. throw new ArgumentNullException("destination");
  84. if (destinationOffset < 0)
  85. throw new ArgumentOutOfRangeException("destinationOffset", Resources.Negative_Destination_Offset);
  86. if (length < 0)
  87. throw new ArgumentOutOfRangeException("length", Resources.Negative_Length);
  88. if (destinationOffset + length > destination.Length)
  89. throw new ArgumentException(Resources.Destination_Buffer_Not_Large_Enough);
  90. if (length > Capacity)
  91. throw new ArgumentOutOfRangeException("length", Resources.Source_Offset_And_Length_Outside_Bounds);
  92. Marshal.Copy(handle, destination, destinationOffset, length);
  93. }
  94. /// <summary>Copies data from this unmanaged memory pointer to a managed 8-bit unsigned integer array.</summary>
  95. /// <param name="sourceOffset">The offset in the buffer to start copying from.</param>
  96. /// <param name="destination">The array to copy to.</param>
  97. /// <param name="destinationOffset">The zero-based index in the destination array where copying should start.</param>
  98. /// <param name="length">The number of array elements to copy.</param>
  99. public void CopyTo(int sourceOffset, byte[] destination, int destinationOffset, int length)
  100. {
  101. if (destination == null)
  102. throw new ArgumentNullException("destination");
  103. if (destinationOffset < 0)
  104. throw new ArgumentOutOfRangeException("destinationOffset", Resources.Negative_Destination_Offset);
  105. if (length < 0)
  106. throw new ArgumentOutOfRangeException("length", Resources.Negative_Length);
  107. if (destinationOffset + length > destination.Length)
  108. throw new ArgumentException(Resources.Destination_Buffer_Not_Large_Enough);
  109. if (length > Capacity)
  110. throw new ArgumentOutOfRangeException("length", Resources.Source_Offset_And_Length_Outside_Bounds);
  111. Marshal.Copy(new IntPtr(handle.ToInt64() + sourceOffset), destination, destinationOffset, length);
  112. }
  113. public byte[] ToByteArray(int startIndex, int length)
  114. {
  115. if (IsInvalid)
  116. return null;
  117. byte[] arr = new byte[length];
  118. Marshal.Copy(handle, arr, startIndex, length);
  119. return arr;
  120. }
  121. #region Write
  122. public void WriteInt16(int offset, short value)
  123. {
  124. Marshal.WriteInt16(handle, offset, value);
  125. }
  126. public void WriteInt16(int offset, char value)
  127. {
  128. Marshal.WriteInt16(handle, offset, value);
  129. }
  130. public void WriteInt16(char value)
  131. {
  132. Marshal.WriteInt16(handle, value);
  133. }
  134. public void WriteInt16(short value)
  135. {
  136. Marshal.WriteInt16(handle, value);
  137. }
  138. public void WriteInt32(int offset, short value)
  139. {
  140. Marshal.WriteInt32(handle, offset, value);
  141. }
  142. public void WriteInt32(int value)
  143. {
  144. Marshal.WriteInt32(handle, value);
  145. }
  146. public void WriteInt64(int offset, long value)
  147. {
  148. Marshal.WriteInt64(handle, offset, value);
  149. }
  150. public void WriteInt64(long value)
  151. {
  152. Marshal.WriteInt64(handle, value);
  153. }
  154. public void WriteByte(int offset, byte value)
  155. {
  156. Marshal.WriteByte(handle, offset, value);
  157. }
  158. public void WriteByte(byte value)
  159. {
  160. Marshal.WriteByte(handle, value);
  161. }
  162. public void WriteIntPtr(int offset, IntPtr value)
  163. {
  164. Marshal.WriteIntPtr(handle, offset, value);
  165. }
  166. public void WriteIntPtr(IntPtr value)
  167. {
  168. Marshal.WriteIntPtr(handle, value);
  169. }
  170. #endregion // Write
  171. #region Read
  172. public byte ReadByte()
  173. {
  174. return Marshal.ReadByte(handle);
  175. }
  176. public byte ReadByte(int offset)
  177. {
  178. return Marshal.ReadByte(handle, offset);
  179. }
  180. public short ReadInt16()
  181. {
  182. return Marshal.ReadInt16(handle);
  183. }
  184. public short ReadInt16(int offset)
  185. {
  186. return Marshal.ReadInt16(handle, offset);
  187. }
  188. public int ReadInt32()
  189. {
  190. return Marshal.ReadInt32(handle);
  191. }
  192. public int ReadInt32(int offset)
  193. {
  194. return Marshal.ReadInt32(handle, offset);
  195. }
  196. public long ReadInt64()
  197. {
  198. return Marshal.ReadInt64(handle);
  199. }
  200. public long ReadInt64(int offset)
  201. {
  202. return Marshal.ReadInt64(handle, offset);
  203. }
  204. public IntPtr ReadIntPtr()
  205. {
  206. return Marshal.ReadIntPtr(handle);
  207. }
  208. public IntPtr ReadIntPtr(int offset)
  209. {
  210. return Marshal.ReadIntPtr(handle, offset);
  211. }
  212. #endregion // Read
  213. /// <summary>Marshals data from a managed object to an unmanaged block of memory.</summary>
  214. public void StructureToPtr(object structure, bool deleteOld)
  215. {
  216. Marshal.StructureToPtr(structure, handle, deleteOld);
  217. }
  218. /// <summary>Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.</summary>
  219. /// <returns>A managed object containing the data pointed to by the ptr parameter.</returns>
  220. public T PtrToStructure<T>(int offset)
  221. {
  222. return (T) Marshal.PtrToStructure(new IntPtr(handle.ToInt64() + offset), typeof (T));
  223. }
  224. /// <summary>Allocates a managed System.String and copies a specified number of characters from an unmanaged Unicode string into it.</summary>
  225. /// <returns>A managed string that holds a copy of the unmanaged string if the value of the ptr parameter is not null; otherwise, this method returns null.</returns>
  226. public string PtrToStringUni(int offset, int length)
  227. {
  228. return Marshal.PtrToStringUni(new IntPtr(handle.ToInt64() + offset), length);
  229. }
  230. /// <summary>Allocates a managed System.String and copies all characters up to the first null character from an unmanaged Unicode string into it.</summary>
  231. /// <returns>A managed string that holds a copy of the unmanaged string if the value of the ptr parameter is not null; otherwise, this method returns null.</returns>
  232. public string PtrToStringUni()
  233. {
  234. return Marshal.PtrToStringUni(handle);
  235. }
  236. #endregion // Public Methods
  237. }
  238. }