Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

42 wiersze
1.4 KiB

  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace RenderHookAPI.Hook
  4. {
  5. /// <summary>
  6. /// Provides a safe handle around a block of unmanaged memory.
  7. /// </summary>
  8. public class SafeHGlobal : SafeHandle
  9. {
  10. /// <summary>
  11. /// When overridden in a derived class, gets a value indicating whether the handle value is invalid.
  12. /// </summary>
  13. /// <returns>true if the handle value is invalid; otherwise, false.</returns>
  14. public override bool IsInvalid
  15. {
  16. get { return handle == IntPtr.Zero; }
  17. }
  18. /// <summary>
  19. /// Initializes a new instance of the <see cref="SafeHGlobal"/> class.
  20. /// </summary>
  21. /// <param name="sizeInBytes">The size of the block of memory to allocate, in bytes.</param>
  22. public SafeHGlobal(int sizeInBytes)
  23. : base(Marshal.AllocHGlobal(sizeInBytes), true)
  24. {
  25. }
  26. /// <summary>
  27. /// When overridden in a derived class, executes the code required to free the handle.
  28. /// </summary>
  29. /// <returns>
  30. /// true if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a releaseHandleFailed MDA Managed Debugging Assistant.
  31. /// </returns>
  32. protected override bool ReleaseHandle()
  33. {
  34. Marshal.FreeHGlobal(handle);
  35. return true;
  36. }
  37. }
  38. }