SafeHandles.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //------------------------------------------------------------------------------
  2. // <copyright file="IPersistStreamInit.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using Microsoft.Win32.SafeHandles;
  7. using System;
  8. namespace Experimental.System.Messaging.Interop
  9. {
  10. internal class MessageQueueHandle : SafeHandleZeroOrMinusOneIsInvalid
  11. {
  12. public static readonly MessageQueueHandle InvalidHandle = new InvalidMessageQueueHandle();
  13. MessageQueueHandle() : base(true) { }
  14. protected override bool ReleaseHandle()
  15. {
  16. SafeNativeMethods.MQCloseQueue(this.handle);
  17. return true;
  18. }
  19. public override bool IsInvalid
  20. {
  21. get { return base.IsInvalid || IsClosed; }
  22. }
  23. // A subclass needed to express InvalidHandle. The reason is that CLR notices that
  24. // ReleaseHandle requires a call to MQRT.DLL, and throws in the ctor if MQRT.DLL is not available,
  25. // even though CTOR ITSELF DOES NOT REQUIRE MQRT.DLL.
  26. // We address this by defining a NOOP ReleaseHandle
  27. sealed class InvalidMessageQueueHandle : MessageQueueHandle
  28. {
  29. protected override bool ReleaseHandle()
  30. {
  31. return true;
  32. }
  33. }
  34. }
  35. internal class CursorHandle : SafeHandleZeroOrMinusOneIsInvalid
  36. {
  37. public static readonly CursorHandle NullHandle = new InvalidCursorHandle();
  38. protected CursorHandle() : base(true) { }
  39. protected override bool ReleaseHandle()
  40. {
  41. SafeNativeMethods.MQCloseCursor(this.handle);
  42. return true;
  43. }
  44. public override bool IsInvalid
  45. {
  46. get { return base.IsInvalid || IsClosed; }
  47. }
  48. // A subclass needed to express InvalidHandle. The reason is that CLR notices that
  49. // ReleaseHandle requires a call to MQRT.DLL, and throws in the ctor if MQRT.DLL is not available,
  50. // even though CTOR ITSELF DOES NOT REQUIRE MQRT.DLL.
  51. // We address this by defining a NOOP ReleaseHandle
  52. sealed class InvalidCursorHandle : CursorHandle
  53. {
  54. protected override bool ReleaseHandle()
  55. {
  56. return true;
  57. }
  58. }
  59. }
  60. internal class LocatorHandle : SafeHandleZeroOrMinusOneIsInvalid
  61. {
  62. public static readonly LocatorHandle InvalidHandle = new InvalidLocatorHandle();
  63. protected LocatorHandle() : base(true) { }
  64. protected override bool ReleaseHandle()
  65. {
  66. SafeNativeMethods.MQLocateEnd(this.handle);
  67. return true;
  68. }
  69. public override bool IsInvalid
  70. {
  71. get { return base.IsInvalid || IsClosed; }
  72. }
  73. // A subclass needed to express InvalidHandle. The reason is that CLR notices that
  74. // ReleaseHandle requires a call to MQRT.DLL, and throws in the ctor if MQRT.DLL is not available,
  75. // even though CTOR ITSELF DOES NOT REQUIRE MQRT.DLL.
  76. // We address this by defining a NOOP ReleaseHandle
  77. sealed class InvalidLocatorHandle : LocatorHandle
  78. {
  79. protected override bool ReleaseHandle()
  80. {
  81. return true;
  82. }
  83. }
  84. }
  85. internal sealed class SecurityContextHandle : SafeHandleZeroOrMinusOneIsInvalid
  86. {
  87. internal SecurityContextHandle(IntPtr existingHandle)
  88. : base(true)
  89. {
  90. SetHandle(existingHandle);
  91. }
  92. protected override bool ReleaseHandle()
  93. {
  94. SafeNativeMethods.MQFreeSecurityContext(this.handle);
  95. return true;
  96. }
  97. public override bool IsInvalid
  98. {
  99. get { return base.IsInvalid || IsClosed; }
  100. }
  101. }
  102. }