This repository has been archived by the owner on Mar 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 639
/
ComObject.cs
322 lines (293 loc) · 13.5 KB
/
ComObject.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Runtime.InteropServices;
using SharpDX.Diagnostics;
namespace SharpDX
{
/// <summary>
/// Root IUnknown class to interop with COM object
/// </summary>
public class ComObject : CppObject, IUnknown
{
/// <summary>
/// Initializes a new instance of the <see cref="ComObject"/> class.
/// </summary>
/// <param name="pointer">Pointer to Cpp Object</param>
public ComObject(IntPtr pointer) : base(pointer)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="ComObject"/> class from a IUnknown object.
/// </summary>
/// <param name="iunknowObject">Reference to a IUnknown object</param>
public ComObject(object iunknowObject)
{
NativePointer = Marshal.GetIUnknownForObject(iunknowObject);
}
/// <summary>
/// Initializes a new instance of the <see cref="ComObject"/> class.
/// </summary>
protected ComObject()
{
}
/// <summary>
/// Query this instance for a particular COM GUID/interface support.
/// </summary>
/// <param name = "guid">GUID query interface</param>
/// <param name = "outPtr">output object associated with this GUID, IntPtr.Zero in interface is not supported</param>
/// <exception cref="SharpDXException">If this object doesn't support the interface</exception>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public virtual void QueryInterface(Guid guid, out IntPtr outPtr)
{
var result = ((IUnknown) this).QueryInterface(ref guid, out outPtr);
result.CheckError();
}
/// <summary>
/// Query instance for a particular COM GUID/interface support.
/// </summary>
/// <param name = "guid">GUID query interface</param>
/// <exception cref="SharpDXException">If this object doesn't support the interface</exception>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public virtual IntPtr QueryInterfaceOrNull(Guid guid)
{
var pointer = IntPtr.Zero;
((IUnknown)this).QueryInterface(ref guid, out pointer);
return pointer;
}
/// <summary>
/// Compares 2 COM objects and return true if the native pointer is the same.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns><c>true</c> if the native pointer is the same, <c>false</c> otherwise</returns>
public static bool EqualsComObject<T>(T left, T right) where T : ComObject
{
if (Equals(left, right))
{
return true;
}
if (left == null || right == null)
{
return false;
}
return (left.NativePointer == right.NativePointer);
}
///<summary>
/// Query this instance for a particular COM interface support.
///</summary>
///<typeparam name="T">The type of the COM interface to query</typeparam>
///<returns>An instance of the queried interface</returns>
/// <exception cref="SharpDXException">If this object doesn't support the interface</exception>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public virtual T QueryInterface<T>() where T : ComObject
{
IntPtr parentPtr;
this.QueryInterface(Utilities.GetGuidFromType(typeof(T)), out parentPtr);
return FromPointer<T>(parentPtr);
}
///<summary>
/// Query this instance for a particular COM interface support.
///</summary>
///<typeparam name="T">The type of the COM interface to query</typeparam>
///<returns>An instance of the queried interface</returns>
/// <exception cref="SharpDXException">If this object doesn't support the interface</exception>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
internal virtual T QueryInterfaceUnsafe<T>()
{
IntPtr parentPtr;
this.QueryInterface(Utilities.GetGuidFromType(typeof(T)), out parentPtr);
return FromPointerUnsafe<T>(parentPtr);
}
/// <summary>
/// Queries a managed object for a particular COM interface support (This method is a shortcut to <see cref="QueryInterface"/>)
/// </summary>
///<typeparam name="T">The type of the COM interface to query</typeparam>
/// <param name="comObject">The managed COM object.</param>
///<returns>An instance of the queried interface</returns>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public static T As<T>(object comObject) where T : ComObject
{
using (var tempObject = new ComObject(Marshal.GetIUnknownForObject(comObject)))
{
return tempObject.QueryInterface<T>();
}
}
/// <summary>
/// Queries a managed object for a particular COM interface support (This method is a shortcut to <see cref="QueryInterface"/>)
/// </summary>
///<typeparam name="T">The type of the COM interface to query</typeparam>
/// <param name="iunknownPtr">The managed COM object.</param>
///<returns>An instance of the queried interface</returns>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public static T As<T>(IntPtr iunknownPtr) where T : ComObject
{
using (var tempObject = new ComObject(iunknownPtr))
{
return tempObject.QueryInterface<T>();
}
}
internal static T AsUnsafe<T>(IntPtr iunknownPtr)
{
using (var tempObject = new ComObject(iunknownPtr))
{
return tempObject.QueryInterfaceUnsafe<T>();
}
}
/// <summary>
/// Queries a managed object for a particular COM interface support.
/// </summary>
///<typeparam name="T">The type of the COM interface to query</typeparam>
/// <param name="comObject">The managed COM object.</param>
///<returns>An instance of the queried interface</returns>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public static T QueryInterface<T>(object comObject) where T : ComObject
{
using (var tempObject = new ComObject(Marshal.GetIUnknownForObject(comObject)))
{
return tempObject.QueryInterface<T>();
}
}
/// <summary>
/// Queries a managed object for a particular COM interface support.
/// </summary>
///<typeparam name="T">The type of the COM interface to query</typeparam>
/// <param name="comPointer">A pointer to a COM object.</param>
///<returns>An instance of the queried interface</returns>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public static T QueryInterfaceOrNull<T>(IntPtr comPointer) where T : ComObject
{
if (comPointer == IntPtr.Zero)
{
return null;
}
var guid = Utilities.GetGuidFromType(typeof(T));
IntPtr pointerT;
var result = (Result)Marshal.QueryInterface(comPointer, ref guid, out pointerT);
return (result.Failure) ? null : FromPointerUnsafe<T>(pointerT);
}
///<summary>
/// Query Interface for a particular interface support.
///</summary>
///<returns>An instance of the queried interface or null if it is not supported</returns>
///<returns></returns>
/// <msdn-id>ms682521</msdn-id>
/// <unmanaged>IUnknown::QueryInterface</unmanaged>
/// <unmanaged-short>IUnknown::QueryInterface</unmanaged-short>
public virtual T QueryInterfaceOrNull<T>() where T : ComObject
{
return FromPointer<T>(QueryInterfaceOrNull(Utilities.GetGuidFromType(typeof(T))));
}
/// <summary>
/// Performs an explicit conversion from <see cref="System.IntPtr"/> to <see cref="SharpDX.ComObject"/>.
/// </summary>
/// <param name="nativePointer">The native pointer.</param>
/// <returns>
/// The result of the conversion.
/// </returns>
public static explicit operator ComObject(IntPtr nativePointer)
{
return nativePointer == IntPtr.Zero ? null : new ComObject(nativePointer);
}
///<summary>
/// Query Interface for a particular interface support and attach to the given instance.
///</summary>
///<typeparam name="T"></typeparam>
///<returns></returns>
protected void QueryInterfaceFrom<T>(T fromObject) where T : ComObject
{
IntPtr parentPtr;
fromObject.QueryInterface(Utilities.GetGuidFromType(this.GetType()), out parentPtr);
NativePointer = parentPtr;
}
Result IUnknown.QueryInterface(ref Guid guid, out IntPtr comObject)
{
return Marshal.QueryInterface(NativePointer, ref guid, out comObject);
}
int IUnknown.AddReference()
{
if (NativePointer == IntPtr.Zero) throw new InvalidOperationException("COM Object pointer is null");
return Marshal.AddRef(NativePointer);
}
int IUnknown.Release()
{
if (NativePointer == IntPtr.Zero) throw new InvalidOperationException("COM Object pointer is null");
return Marshal.Release(NativePointer);
}
/// <summary>
/// Releases unmanaged and - optionally - managed resources
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
/// <msdn-id>ms682317</msdn-id>
/// <unmanaged>IUnknown::Release</unmanaged>
/// <unmanaged-short>IUnknown::Release</unmanaged-short>
protected unsafe override void Dispose(bool disposing)
{
// Only dispose non-zero object
if (NativePointer != IntPtr.Zero)
{
// If object is disposed by the finalizer, emits a warning
if(!disposing && Configuration.EnableTrackingReleaseOnFinalizer)
{
if(!Configuration.EnableReleaseOnFinalizer)
{
var objectReference = ObjectTracker.Find(this);
System.Diagnostics.Debug.WriteLine(string.Format("Warning: Live ComObject [0x{0:X}], potential memory leak: {1}", NativePointer.ToInt64(), objectReference));
}
}
// Release the object
if (disposing || Configuration.EnableReleaseOnFinalizer)
((IUnknown)this).Release();
// Untrack the object
if (Configuration.EnableObjectTracking)
ObjectTracker.UnTrack(this);
// Set pointer to null (using protected members in order to avoid NativePointerUpdat* callbacks.
_nativePointer = (void*)0;
}
base.Dispose(disposing);
}
protected override void NativePointerUpdating()
{
if (Configuration.EnableObjectTracking)
ObjectTracker.UnTrack(this);
}
protected override void NativePointerUpdated(IntPtr oldNativePointer)
{
if (Configuration.EnableObjectTracking)
ObjectTracker.Track(this);
}
}
}