Handles.cs
Upload User: nnpulika
Upload Date: 2013-02-15
Package Size: 597k
Code Size: 3k
Category:

StatusBar

Development Platform:

C#

  1. using System;
  2. namespace UtilityLibrary.Win32
  3. {
  4. public class ShellHandle : IDisposable
  5. {
  6. #region Class Variables
  7. protected IntPtr handle = IntPtr.Zero;
  8. #endregion
  9.         
  10. #region Constructors 
  11. // Can only be used as base classes
  12. protected ShellHandle(IntPtr handle)
  13. {
  14. this.handle = handle;
  15. }
  16.         
  17. ~ShellHandle()
  18. {
  19. Dispose(false);
  20. }
  21. #endregion
  22. #region Properties
  23. public IntPtr Handle
  24. {
  25. get { return handle; }
  26. }
  27. #endregion
  28. #region Virtuals
  29. protected virtual void Dispose(bool disposing)
  30. {
  31. // This class encapsulate a PIDL handle that
  32. // it is allocated my the Shell Memory Manager
  33. // it needs to be deallocated by the Shell Memory Manager 
  34. // interface too
  35. // To avoid threads simultaneously releasing this resource
  36. lock (this)
  37. {
  38. if ( handle != IntPtr.Zero )
  39. {
  40. // If we have a valid handle
  41. // Release pointer that was allocated by the COM memory allocator
  42. WindowsAPI.SHFreeMalloc(handle);
  43. handle = IntPtr.Zero;
  44. }
  45. }
  46. }
  47. #endregion
  48. #region Methods
  49. // Implements the IDisposable Interface
  50. public void Dispose()
  51. {
  52. // Let the Garbage Collector know that it does
  53. // not need to call finalize for this class
  54. GC.SuppressFinalize(this);
  55. // Do the disposing
  56. Dispose(true);
  57. }
  58. #endregion
  59. }
  60. public class COMInterface : IDisposable
  61. {
  62. #region Class Variables
  63. protected IUnknown iUnknown = null;
  64. #endregion
  65.         
  66. #region Constructors 
  67. // Can only be used as base classes
  68. protected COMInterface(IUnknown iUnknown)
  69. {
  70. this.iUnknown = iUnknown;
  71. }
  72.         
  73. ~COMInterface()
  74. {
  75. Dispose(false);
  76. }
  77. #endregion
  78. #region Properties
  79. #endregion
  80. #region Virtuals
  81. protected virtual void Dispose(bool disposing)
  82. {
  83. // Release the reference to this interface
  84. lock(this)
  85. {
  86. if ( iUnknown != null )
  87. {
  88. iUnknown.Release();
  89. iUnknown = null;
  90. }
  91. }
  92. }
  93. #endregion
  94. #region Methods
  95. // Implements the IDisposable Interface
  96. public void Dispose()
  97. {
  98. // Let the Garbage Collector know that it does
  99. // not need to call finalize for this class
  100. GC.SuppressFinalize(this);
  101. // Do the disposing
  102. Dispose(true);
  103. }
  104. #endregion
  105. }
  106. public class GdiHandle : IDisposable
  107. {
  108. #region Class Variables
  109. protected IntPtr handle = IntPtr.Zero;
  110. #endregion
  111.         
  112. #region Constructors 
  113. // Can only be used as base classes
  114. protected GdiHandle(IntPtr handle)
  115. {
  116. this.handle = handle;
  117. }
  118.         
  119. ~GdiHandle()
  120. {
  121. Dispose(false);
  122. }
  123. #endregion
  124. #region Properties
  125. public IntPtr Handle
  126. {
  127. get { return handle; }
  128. }
  129. #endregion
  130. #region Virtuals
  131. protected virtual void Dispose(bool disposing)
  132. {
  133. // To avoid threads simultaneously releasing this resource
  134. lock (this)
  135. {
  136. if ( handle != IntPtr.Zero )
  137. {
  138. // If we have a valid handle
  139. // Destroy the handle
  140. WindowsAPI.DeleteObject(handle);
  141. handle = IntPtr.Zero;
  142. }
  143. }
  144. }
  145. #endregion
  146. #region Methods
  147. // Implements the IDisposable Interface
  148. public void Dispose()
  149. {
  150. // Let the Garbage Collector know that it does
  151. // not need to call finalize for this class
  152. GC.SuppressFinalize(this);
  153. // Do the disposing
  154. Dispose(true);
  155. }
  156. #endregion
  157. }
  158. }