**** Merged from MCS ****
[mono-project.git] / mcs / class / System.Windows.Forms / System.Windows.Forms / win32Handles.cs
blob26c7148ab059b8e42a854201626416ce931384be
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Copyright (C) 5/11/2002 Carlos Harvey Perez
25 * Permission is hereby granted, free of charge, to any person obtaining
26 * a copy of this software and associated documentation files (the
27 * "Software"), to deal in the Software without restriction, including
28 * without limitation the rights to use, copy, modify, merge, publish,
29 * distribute, sublicense, and/or sell copies of the Software, and to
30 * permit persons to whom the Software is furnished to do so, subject
31 * to the following conditions:
33 * The above copyright notice and this permission notice shall be
34 * included in all copies or substantial portions of the Software.
36 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
38 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39 * NONINFRINGEMENT.
40 * IN NO EVENT SHALL CARLOS HARVEY PEREZ BE LIABLE FOR ANY CLAIM,
41 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
42 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
43 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 * Except as contained in this notice, the name of Carlos Harvey Perez
46 * shall not be used in advertising or otherwise to promote the sale,
47 * use or other dealings in this Software without prior written
48 * authorization from Carlos Harvey Perez.
51 using System;
53 //namespace UtilityLibrary.Win32
54 namespace System.Windows.Forms{
55 #if HAVE_SHELL_SUPPORT
56 internal class ShellHandle : IDisposable
58 #region Class Variables
59 IntPtr handle = IntPtr.Zero;
60 #endregion
62 #region Constructors
63 // Can only be used as base classes
64 internal ShellHandle(IntPtr handle)
66 this.handle = handle;
69 ~ShellHandle()
71 Dispose(false);
73 #endregion
75 #region Properties
76 internal IntPtr Handle
78 get { return handle; }
81 #endregion
83 #region Virtuals
84 internal virtual void Dispose(bool disposing)
86 // This class encapsulate a PIDL handle that
87 // it is allocated my the Shell Memory Manager
88 // it needs to be deallocated by the Shell Memory Manager
89 // interface too
91 // To avoid threads simultaneously releasing this resource
92 lock (this)
95 if ( handle != IntPtr.Zero )
97 // If we have a valid handle
98 // Release pointer that was allocated by the COM memory allocator
99 Win32.SHFreeMalloc(handle);
100 handle = IntPtr.Zero;
104 #endregion
106 #region Methods
107 // Implements the IDisposable Interface
108 public void Dispose()
110 // Let the Garbage Collector know that it does
111 // not need to call finalize for this class
112 GC.SuppressFinalize(this);
114 // Do the disposing
115 Dispose(true);
117 #endregion
120 #endif
122 internal class COMInterface : IDisposable
124 #region Class Variables
125 internal IUnknown iUnknown = null;
126 #endregion
128 #region Constructors
129 // Can only be used as base classes
130 internal COMInterface(IUnknown iUnknown)
132 this.iUnknown = iUnknown;
135 ~COMInterface()
137 Dispose(false);
139 #endregion
141 #region Properties
142 #endregion
144 #region Virtuals
145 protected virtual void Dispose(bool disposing)
147 // Release the reference to this interface
148 lock(this)
150 if ( iUnknown != null )
152 iUnknown.Release();
153 iUnknown = null;
157 #endregion
159 #region Methods
160 // Implements the IDisposable Interface
161 public void Dispose()
163 // Let the Garbage Collector know that it does
164 // not need to call finalize for this class
165 GC.SuppressFinalize(this);
167 // Do the disposing
168 Dispose(true);
170 #endregion
174 internal class GdiHandle : IDisposable
176 #region Class Variables
177 IntPtr handle = IntPtr.Zero;
178 #endregion
180 #region Constructors
181 // Can only be used as base classes
182 protected GdiHandle(IntPtr handle)
184 this.handle = handle;
187 ~GdiHandle()
189 Dispose(false);
191 #endregion
193 #region Properties
194 public IntPtr Handle
196 get { return handle; }
199 #endregion
201 #region Virtuals
202 protected virtual void Dispose(bool disposing)
204 // To avoid threads simultaneously releasing this resource
205 lock (this)
208 if ( handle != IntPtr.Zero )
210 // If we have a valid handle
211 // Destroy the handle
212 Win32.DeleteObject(handle);
213 handle = IntPtr.Zero;
217 #endregion
219 #region Methods
220 // Implements the IDisposable Interface
221 public void Dispose()
223 // Let the Garbage Collector know that it does
224 // not need to call finalize for this class
225 GC.SuppressFinalize(this);
227 // Do the disposing
228 Dispose(true);
230 #endregion