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.

SafeNativeMemoryBufferHandle.cs 10 KiB

7 vuotta sitten
7 vuotta sitten
7 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 readonly int m_capacity;
  31. #endregion
  32. #region Constructors
  33. protected SafeNativeMemoryBufferHandle(bool ownsHandle) : base(ownsHandle)
  34. {
  35. }
  36. /// <summary>Initializes a new instance of the <see cref="SafeNativeMemoryBufferHandle"/> specifying the allocated capacity of the memory block.</summary>
  37. /// <param name="capacity">The capacity.</param>
  38. protected SafeNativeMemoryBufferHandle(int capacity)
  39. : this(true)
  40. {
  41. m_capacity = capacity;
  42. }
  43. protected SafeNativeMemoryBufferHandle(IntPtr memory, int capacity)
  44. : this(capacity)
  45. {
  46. SetHandle(memory);
  47. }
  48. #endregion
  49. #region Properties
  50. /// <summary>Gets the capacity. Only valid if this instance was created using a constructor that specifies the size,
  51. /// it is not correct if this handle was returned by a native method using p/invoke.
  52. /// </summary>
  53. public int Capacity
  54. {
  55. get { return m_capacity; }
  56. }
  57. #endregion
  58. #region Public Methods
  59. /// <summary>Copies data from a one-dimensional, managed 8-bit unsigned integer array to the unmanaged memory pointer referenced by this instance.</summary>
  60. /// <param name="source">The one-dimensional array to copy from. </param>
  61. /// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
  62. /// <param name="length">The number of array elements to copy.</param>
  63. public void CopyFrom(byte[] source, int startIndex, int length)
  64. {
  65. Marshal.Copy(source, startIndex, handle, length);
  66. }
  67. public void CopyFrom(char[] source, int startIndex, int length)
  68. {
  69. Marshal.Copy(source, startIndex, handle, length);
  70. }
  71. public void CopyFrom(char[] source, int startIndex, int length, int offset)
  72. {
  73. Marshal.Copy(source, startIndex, new IntPtr(handle.ToInt64() + offset), length);
  74. }
  75. /// <summary>Copies data from an unmanaged memory pointer to a managed 8-bit unsigned integer array.</summary>
  76. /// <param name="destination">The array to copy to.</param>
  77. /// <param name="destinationOffset">The zero-based index in the destination array where copying should start.</param>
  78. /// <param name="length">The number of array elements to copy.</param>
  79. public void CopyTo(byte[] destination, int destinationOffset, int length)
  80. {
  81. if (destination == null)
  82. throw new ArgumentNullException("destination");
  83. if (destinationOffset < 0)
  84. throw new ArgumentOutOfRangeException("destinationOffset", Resources.Negative_Destination_Offset);
  85. if (length < 0)
  86. throw new ArgumentOutOfRangeException("length", Resources.Negative_Length);
  87. if (destinationOffset + length > destination.Length)
  88. throw new ArgumentException(Resources.Destination_Buffer_Not_Large_Enough);
  89. if (length > Capacity)
  90. throw new ArgumentOutOfRangeException("length", Resources.Source_Offset_And_Length_Outside_Bounds);
  91. Marshal.Copy(handle, destination, destinationOffset, length);
  92. }
  93. /// <summary>Copies data from this unmanaged memory pointer to a managed 8-bit unsigned integer array.</summary>
  94. /// <param name="sourceOffset">The offset in the buffer to start copying from.</param>
  95. /// <param name="destination">The array to copy to.</param>
  96. /// <param name="destinationOffset">The zero-based index in the destination array where copying should start.</param>
  97. /// <param name="length">The number of array elements to copy.</param>
  98. public void CopyTo(int sourceOffset, byte[] destination, int destinationOffset, int length)
  99. {
  100. if (destination == null)
  101. throw new ArgumentNullException("destination");
  102. if (destinationOffset < 0)
  103. throw new ArgumentOutOfRangeException("destinationOffset", Resources.Negative_Destination_Offset);
  104. if (length < 0)
  105. throw new ArgumentOutOfRangeException("length", Resources.Negative_Length);
  106. if (destinationOffset + length > destination.Length)
  107. throw new ArgumentException(Resources.Destination_Buffer_Not_Large_Enough);
  108. if (length > Capacity)
  109. throw new ArgumentOutOfRangeException("length", Resources.Source_Offset_And_Length_Outside_Bounds);
  110. Marshal.Copy(new IntPtr(handle.ToInt64() + sourceOffset), destination, destinationOffset, length);
  111. }
  112. public byte[] ToByteArray(int startIndex, int length)
  113. {
  114. if (IsInvalid)
  115. return null;
  116. byte[] arr = new byte[length];
  117. Marshal.Copy(handle, arr, startIndex, length);
  118. return arr;
  119. }
  120. #region Write
  121. public void WriteInt16(int offset, short value)
  122. {
  123. Marshal.WriteInt16(handle, offset, value);
  124. }
  125. public void WriteInt16(int offset, char value)
  126. {
  127. Marshal.WriteInt16(handle, offset, value);
  128. }
  129. public void WriteInt16(char value)
  130. {
  131. Marshal.WriteInt16(handle, value);
  132. }
  133. public void WriteInt16(short value)
  134. {
  135. Marshal.WriteInt16(handle, value);
  136. }
  137. public void WriteInt32(int offset, short value)
  138. {
  139. Marshal.WriteInt32(handle, offset, value);
  140. }
  141. public void WriteInt32(int value)
  142. {
  143. Marshal.WriteInt32(handle, value);
  144. }
  145. public void WriteInt64(int offset, long value)
  146. {
  147. Marshal.WriteInt64(handle, offset, value);
  148. }
  149. public void WriteInt64(long value)
  150. {
  151. Marshal.WriteInt64(handle, value);
  152. }
  153. public void WriteByte(int offset, byte value)
  154. {
  155. Marshal.WriteByte(handle, offset, value);
  156. }
  157. public void WriteByte(byte value)
  158. {
  159. Marshal.WriteByte(handle, value);
  160. }
  161. public void WriteIntPtr(int offset, IntPtr value)
  162. {
  163. Marshal.WriteIntPtr(handle, offset, value);
  164. }
  165. public void WriteIntPtr(IntPtr value)
  166. {
  167. Marshal.WriteIntPtr(handle, value);
  168. }
  169. #endregion // Write
  170. #region Read
  171. public byte ReadByte()
  172. {
  173. return Marshal.ReadByte(handle);
  174. }
  175. public byte ReadByte(int offset)
  176. {
  177. return Marshal.ReadByte(handle, offset);
  178. }
  179. public short ReadInt16()
  180. {
  181. return Marshal.ReadInt16(handle);
  182. }
  183. public short ReadInt16(int offset)
  184. {
  185. return Marshal.ReadInt16(handle, offset);
  186. }
  187. public int ReadInt32()
  188. {
  189. return Marshal.ReadInt32(handle);
  190. }
  191. public int ReadInt32(int offset)
  192. {
  193. return Marshal.ReadInt32(handle, offset);
  194. }
  195. public long ReadInt64()
  196. {
  197. return Marshal.ReadInt64(handle);
  198. }
  199. public long ReadInt64(int offset)
  200. {
  201. return Marshal.ReadInt64(handle, offset);
  202. }
  203. public IntPtr ReadIntPtr()
  204. {
  205. return Marshal.ReadIntPtr(handle);
  206. }
  207. public IntPtr ReadIntPtr(int offset)
  208. {
  209. return Marshal.ReadIntPtr(handle, offset);
  210. }
  211. #endregion // Read
  212. /// <summary>Marshals data from a managed object to an unmanaged block of memory.</summary>
  213. public void StructureToPtr(object structure, bool deleteOld)
  214. {
  215. Marshal.StructureToPtr(structure, handle, deleteOld);
  216. }
  217. /// <summary>Marshals data from an unmanaged block of memory to a newly allocated managed object of the specified type.</summary>
  218. /// <returns>A managed object containing the data pointed to by the ptr parameter.</returns>
  219. public T PtrToStructure<T>(int offset)
  220. {
  221. return (T) Marshal.PtrToStructure(new IntPtr(handle.ToInt64() + offset), typeof (T));
  222. }
  223. /// <summary>Allocates a managed System.String and copies a specified number of characters from an unmanaged Unicode string into it.</summary>
  224. /// <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>
  225. public string PtrToStringUni(int offset, int length)
  226. {
  227. return Marshal.PtrToStringUni(new IntPtr(handle.ToInt64() + offset), length);
  228. }
  229. /// <summary>Allocates a managed System.String and copies all characters up to the first null character from an unmanaged Unicode string into it.</summary>
  230. /// <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>
  231. public string PtrToStringUni()
  232. {
  233. return Marshal.PtrToStringUni(handle);
  234. }
  235. #endregion // Public Methods
  236. }
  237. }