forked from emescey/bcit-courses
-
Notifications
You must be signed in to change notification settings - Fork 0
/
COMWrapper.cs
executable file
·540 lines (476 loc) · 15.1 KB
/
COMWrapper.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
using System;
using System.Diagnostics;
using System.Runtime.Remoting.Proxies;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
using System.Reflection;
namespace ManagedOffice
{
/// <summary>
/// Wraps a late-bound COM server.
/// </summary>
public sealed class COMWrapper : RealProxy, IDisposable
{
#region Private Data
/// <summary>
/// Holds reference to the actual COM object which is wrapped by this proxy
/// </summary>
private object _COMObject;
/// <summary>
/// Type of the COM object, set on constructor after getting the COM reference
/// </summary>
private Type _COMType;
/// <summary>
/// The type of which method calls are intercepted and executed on the COM object.
/// </summary>
private Type _InterceptType;
/// <summary>
/// The class which provides events for this object
/// </summary>
private ComEventProvider _ComEventProvider;
#endregion
#region Construction
/// <summary>
/// Creates a COM object and returns the transparent proxy
/// which intercepts all calls to the object
/// </summary>
/// <param name="type">Interface which defines the method and properties to intercept</param>
/// <returns>Transparent proxy to the real proxy for the object</returns>
/// <remarks>
/// The <paramref name="type"/> must be an interface decorated with
/// the <see cref="ComProgIdAttribute"/> attribute.
/// </remarks>
public static object CreateInstance(Type type)
{
if (null == type) throw new ArgumentNullException("type");
if (!type.IsInterface) throw new ArgumentException("The specified type must be an interface.", "type");
ComProgIdAttribute progID = ComProgIdAttribute.GetAttribute(type);
if (null == progID || null == progID.Value || 0 == progID.Value.Length)
throw new ArgumentException("The specified type must define a ComProgId attribute.", "type");
Type comType = Type.GetTypeFromProgID(progID.Value, true);
object comObject = Activator.CreateInstance(comType);
if (null == comObject)
{
throw new TypeLoadException(
string.Format("Unable to create an instance of the specified COM server \"{0}\".",
progID.Value));
}
COMWrapper wrapper = new COMWrapper(comObject, type);
return wrapper.GetTransparentProxy();
}
/// <summary>
/// Wrap an object and return the transparent proxy which intercepts all calls
/// to the object
/// </summary>
/// <param name="comObject">An object to intercept</param>
/// <param name="type">Interface which defines the method and properties to intercept</param>
/// <returns>Transparent proxy to the real proxy for the object</returns>
public static object Wrap(object comObject, Type type)
{
if (null == comObject) throw new ArgumentNullException("comObject");
if (null == type) throw new ArgumentNullException("type");
COMWrapper wrapper = new COMWrapper(comObject, type);
return wrapper.GetTransparentProxy();
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="comObject">
/// The COM object to wrap.
/// </param>
/// <param name="type">
/// The interface type to impersonate.
/// </param>
private COMWrapper(object comObject, Type type) : base( type )
{
this._COMObject = comObject;
this._COMType = comObject.GetType();
this._InterceptType = type;
}
#endregion
#region Clean up
/// <summary>
/// If <see cref="Dispose"/> is not called, we need to make
/// sure that the COM object is still cleaned up.
/// </summary>
~COMWrapper()
{
Debug.WriteLine("Finalize " + this._InterceptType.ToString());
this.Dispose(false);
}
/// <summary>
/// Cleans up the COM object.
/// </summary>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <summary>
/// Release the COM reference
/// </summary>
/// <param name="disposing">
/// <see langword="true"/> if this was called from the
/// <see cref="IDisposable"/> interface.
/// </param>
private void Dispose(bool disposing)
{
if (disposing)
{
if (null != this._ComEventProvider)
{
this._ComEventProvider.Dispose();
this._ComEventProvider = null;
}
}
if (null != this._COMObject)
{
if(Marshal.IsComObject(this._COMObject))
while( Marshal.ReleaseComObject(this._COMObject) > 0 );
this._COMObject = null;
}
}
#endregion
#region Object methods
/// <summary>
/// Returns a string representing the wrapped object.
/// </summary>
/// <returns>
/// The full name of the intercepted type.
/// </returns>
public override string ToString()
{
return this._InterceptType.FullName;
}
/// <summary>
/// Returns the hash code of the wrapped object.
/// </summary>
/// <returns>
/// The hash code of the wrapped object.
/// </returns>
public override int GetHashCode()
{
return this._COMObject.GetHashCode();
}
/// <summary>
/// Compares this object to another.
/// </summary>
/// <param name="value">
/// The value to compare to.
/// </param>
/// <returns>
/// <see langword="true"/> if the objects are equal.
/// </returns>
public override bool Equals(object value)
{
if (null != value && RemotingServices.IsTransparentProxy(value))
{
COMWrapper wrapper = RemotingServices.GetRealProxy(value) as COMWrapper;
if (null != wrapper) return this._COMObject == wrapper._COMObject;
}
return base.Equals(value);
}
/// <summary>
/// Returns the base type for a reference type.
/// </summary>
/// <param name="byRefType">
/// The reference type.
/// </param>
/// <returns>
/// The base value type.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="byRefType"/> is <see langword="null"/>.
/// </exception>
private static Type GetByValType(Type byRefType)
{
if (null == byRefType) throw new ArgumentNullException("byRefType");
if (byRefType.IsByRef)
{
string name = byRefType.FullName;
name = name.Substring(0, name.Length - 1);
byRefType = byRefType.Assembly.GetType(name, true);
}
return byRefType;
}
#endregion
#region Event Handling
/// <summary>
/// Attaches an event handler to the COM object.
/// </summary>
/// <param name="eventName">
/// The name of the event.
/// </param>
/// <param name="handler">
/// The event handler delegate.
/// </param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="eventName"/> is <see langword="null"/></para>
/// <para>-or-</para>
/// <para><paramref name="handler"/> is <see langword="null"/></para>
/// </exception>
private void AttachEventHandler(string eventName, Delegate handler)
{
if (null == eventName || 0 == eventName.Length) throw new ArgumentNullException("eventName");
if (null == handler) throw new ArgumentNullException("handler");
lock(this)
{
if (null == _ComEventProvider)
_ComEventProvider = new ComEventProvider(_COMObject, _InterceptType, this);
_ComEventProvider.AddEventHandler(eventName, handler);
}
}
/// <summary>
/// Removes an event handler from the COM object.
/// </summary>
/// <param name="eventName">
/// The name of the event.
/// </param>
/// <param name="handler">
/// The event handler delegate.
/// </param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="eventName"/> is <see langword="null"/></para>
/// <para>-or-</para>
/// <para><paramref name="handler"/> is <see langword="null"/></para>
/// </exception>
private void RemoveEventHandler(string eventName, Delegate handler)
{
if (null == eventName || 0 == eventName.Length) throw new ArgumentNullException("eventName");
if (null == handler) throw new ArgumentNullException("handler");
lock(this)
{
if (null == _ComEventProvider) return;
_ComEventProvider.RemoveEventHandler(eventName, handler);
}
}
#endregion
/// <summary>
/// Intercept method calls
/// </summary>
/// <param name="myMessage">
/// Contains information about the method being called
/// </param>
/// <returns>
/// A <see cref="ReturnMessage"/>.
/// </returns>
public override IMessage Invoke(IMessage myMessage)
{
IMethodCallMessage callMessage = myMessage as IMethodCallMessage;
if (null == callMessage)
{
Debug.WriteLine("Message type not implemented: " + myMessage.GetType().ToString());
return null;
}
MethodInfo method = callMessage.MethodBase as MethodInfo;
if (null == method)
{
Debug.WriteLine("Unrecognized Invoke call: " + callMessage.MethodBase.ToString());
return null;
}
object returnValue = null;
object[] outArgs = null;
int outArgsCount = 0;
string methodName = method.Name;
Type returnType = method.ReturnType;
BindingFlags flags = BindingFlags.InvokeMethod;
int argCount = callMessage.ArgCount;
object invokeObject;
Type invokeType;
Type byValType;
object[] args;
object arg;
COMWrapper[] originalArgs;
COMWrapper wrapper;
ParameterModifier[] argModifiers = null;
ParameterInfo[] parameters = null;
ParameterInfo parameter;
if ("Dispose" == methodName && 0 == argCount && typeof(void) == returnType)
{
this.Dispose();
}
else if ("ToString" == methodName && 0 == argCount && typeof(string) == returnType)
{
returnValue = this.ToString();
}
else if ("GetType" == methodName && 0 == argCount && typeof(System.Type) == returnType)
{
returnValue = this._InterceptType;
}
else if ("GetHashCode" == methodName && 0 == argCount && typeof(int) == returnType)
{
returnValue = this.GetHashCode();
}
else if ("Equals" == methodName && 1 == argCount && typeof(bool) == returnType)
{
returnValue = this.Equals(callMessage.Args[0]);
}
else if (1 == argCount && typeof(void) == returnType
&& (methodName.StartsWith("add_") || methodName.StartsWith("remove_")))
{
bool removeHandler = methodName.StartsWith("remove_");
methodName = methodName.Substring(removeHandler ? 7 : 4);
Delegate handler = callMessage.InArgs[0] as Delegate;
if (null == handler)
return new ReturnMessage(new ArgumentNullException("handler"), callMessage);
try
{
if (removeHandler)
RemoveEventHandler(methodName, handler);
else
AttachEventHandler(methodName, handler);
}
catch(Exception ex)
{
return new ReturnMessage(ex, callMessage);
}
}
else
{
invokeObject = this._COMObject;
invokeType = this._COMType;
if (methodName.StartsWith("get_"))
{
// Property Get
methodName = methodName.Substring(4);
flags = BindingFlags.GetProperty;
args = callMessage.InArgs;
}
else if (methodName.StartsWith("set_"))
{
// Property Set
methodName = methodName.Substring(4);
flags = BindingFlags.SetProperty;
args = callMessage.InArgs;
}
else
{
args = callMessage.Args;
if (null != args && 0 != args.Length)
{
// Modifiers for ref / out parameters
argModifiers = new ParameterModifier[1];
argModifiers[0] = new ParameterModifier(args.Length);
parameters = method.GetParameters();
for(int i=0; i<parameters.Length; i++)
{
parameter = parameters[i];
if (parameter.IsOut || parameter.ParameterType.IsByRef)
{
argModifiers[0][i] = true;
outArgsCount++;
}
}
if (0 == outArgsCount) argModifiers = null;
}
}
// Un-wrap wrapped COM objects before passing to the method
if (null == args || 0 == args.Length)
{
originalArgs = null;
}
else
{
originalArgs = new COMWrapper[args.Length];
for(int i=0; i<args.Length; i++)
{
if (null != args[i] && RemotingServices.IsTransparentProxy(args[i]))
{
wrapper = RemotingServices.GetRealProxy(args[i]) as COMWrapper;
if (null != wrapper)
{
originalArgs[i] = wrapper;
args[i] = wrapper._COMObject;
}
}
else if (0 != outArgsCount && argModifiers[0][i])
{
byValType = GetByValType(parameters[i].ParameterType);
if (byValType.IsInterface)
{
// If we're passing a COM object by reference, and
// the parameter is null, we need to pass a
// DispatchWrapper to avoid a type mismatch exception.
if (null == args[i]) args[i] = new DispatchWrapper(null);
}
else if (typeof(Decimal) == byValType)
{
// If we're passing a decimal value by reference,
// we need to pass a CurrencyWrapper to avoid a
// type mismatch exception.
// http://support.microsoft.com/?kbid=837378
args[i] = new CurrencyWrapper(args[i]);
}
}
}
}
try
{
returnValue = invokeType.InvokeMember(methodName,
flags, null, invokeObject, args, argModifiers, null, null);
}
catch(Exception ex)
{
return new ReturnMessage(ex, callMessage);
}
// Handle enum and interface return types
if (null != returnValue)
{
if (returnType.IsInterface)
{
// Wrap the returned value in an intercepting COM wrapper
if (Marshal.IsComObject(returnValue))
returnValue = COMWrapper.Wrap(returnValue, returnType);
}
else if (returnType.IsEnum)
{
// Convert to proper Enum type
returnValue = Enum.Parse(returnType, returnValue.ToString());
}
}
// Handle out args
if (0 != outArgsCount)
{
outArgs = new object[args.Length];
for(int i=0; i<parameters.Length; i++)
{
if (!argModifiers[0][i]) continue;
arg = args[i];
if (null == arg) continue;
parameter = parameters[i];
wrapper = null;
byValType = GetByValType(parameter.ParameterType);
if (typeof(Decimal) == byValType)
{
if (arg is CurrencyWrapper) arg = ((CurrencyWrapper)arg).WrappedObject;
}
else if (byValType.IsEnum)
{
arg = Enum.Parse(byValType, arg.ToString());
}
else if (byValType.IsInterface)
{
if (Marshal.IsComObject(arg))
{
wrapper = originalArgs[i];
if (null != wrapper && wrapper._COMObject != arg)
{
wrapper.Dispose();
wrapper = null;
}
if (null == wrapper) wrapper = new COMWrapper(arg, byValType);
arg = wrapper.GetTransparentProxy();
}
}
outArgs[i] = arg;
}
}
}
return new ReturnMessage(returnValue,
outArgs, outArgsCount,
callMessage.LogicalCallContext,
callMessage);
}
}
}