Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 

105 Zeilen
3.7 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 System;
  22. using System.Runtime.InteropServices;
  23. using Microsoft.Win32.SafeHandles;
  24. namespace Alphaleonis.Win32.Security
  25. {
  26. /// <summary>An IntPtr wrapper which can be used as the result of a Marshal.AllocHGlobal operation.
  27. /// <para>Calls Marshal.FreeHGlobal when disposed or finalized.</para>
  28. /// </summary>
  29. internal sealed class SafeLocalMemoryBufferHandle : SafeHandleZeroOrMinusOneIsInvalid
  30. {
  31. #region Constructors
  32. /// <summary>Creates new instance with zero IntPtr.</summary>
  33. public SafeLocalMemoryBufferHandle() : base(true)
  34. {
  35. }
  36. #endregion // Constructors
  37. #region Methods
  38. #region CopyFrom
  39. /// <summary>Copies data from a one-dimensional, managed 8-bit unsigned integer array to the unmanaged memory pointer referenced by this instance.</summary>
  40. /// <param name="source">The one-dimensional array to copy from.</param>
  41. /// <param name="startIndex">The zero-based index into the array where Copy should start.</param>
  42. /// <param name="length">The number of array elements to copy.</param>
  43. public void CopyFrom(byte[] source, int startIndex, int length)
  44. {
  45. Marshal.Copy(source, startIndex, handle, length);
  46. }
  47. #endregion // CopyFrom
  48. #region CopyTo
  49. public void CopyTo(byte[] destination, int destinationOffset, int length)
  50. {
  51. if (destination == null)
  52. throw new ArgumentNullException("destination");
  53. if (destinationOffset < 0)
  54. throw new ArgumentOutOfRangeException("destinationOffset", Resources.Negative_Destination_Offset);
  55. if (length < 0)
  56. throw new ArgumentOutOfRangeException("length", Resources.Negative_Length);
  57. if (destinationOffset + length > destination.Length)
  58. throw new ArgumentException(Resources.Destination_Buffer_Not_Large_Enough);
  59. Marshal.Copy(handle, destination, destinationOffset, length);
  60. }
  61. #endregion // CopyTo
  62. #region ToByteArray
  63. public byte[] ToByteArray(int startIndex, int length)
  64. {
  65. if (IsInvalid)
  66. return null;
  67. byte[] arr = new byte[length];
  68. Marshal.Copy(handle, arr, startIndex, length);
  69. return arr;
  70. }
  71. #endregion // ToByteArray
  72. #region ReleaseHandle
  73. /// <summary>Called when object is disposed or finalized.</summary>
  74. protected override bool ReleaseHandle()
  75. {
  76. return handle == IntPtr.Zero || NativeMethods.LocalFree(handle) == IntPtr.Zero;
  77. }
  78. #endregion // ReleaseHandle
  79. #endregion // Methods
  80. }
  81. }