@@ -25,7 +25,7 @@ public interface IClassFactory
25
25
void CreateInstance (
26
26
[ MarshalAs ( UnmanagedType . Interface ) ] object ? pUnkOuter ,
27
27
ref Guid riid ,
28
- [ MarshalAs ( UnmanagedType . Interface ) ] out object ? ppvObject ) ;
28
+ out IntPtr ppvObject ) ;
29
29
30
30
void LockServer ( [ MarshalAs ( UnmanagedType . Bool ) ] bool fLock ) ;
31
31
}
@@ -51,7 +51,7 @@ internal interface IClassFactory2 : IClassFactory
51
51
new void CreateInstance (
52
52
[ MarshalAs ( UnmanagedType . Interface ) ] object ? pUnkOuter ,
53
53
ref Guid riid ,
54
- [ MarshalAs ( UnmanagedType . Interface ) ] out object ? ppvObject ) ;
54
+ out IntPtr ppvObject ) ;
55
55
56
56
new void LockServer ( [ MarshalAs ( UnmanagedType . Bool ) ] bool fLock ) ;
57
57
@@ -66,7 +66,7 @@ void CreateInstanceLic(
66
66
[ MarshalAs ( UnmanagedType . Interface ) ] object ? pUnkReserved ,
67
67
ref Guid riid ,
68
68
[ MarshalAs ( UnmanagedType . BStr ) ] string bstrKey ,
69
- [ MarshalAs ( UnmanagedType . Interface ) ] out object ppvObject ) ;
69
+ out IntPtr ppvObject ) ;
70
70
}
71
71
72
72
[ StructLayout ( LayoutKind . Sequential ) ]
@@ -424,27 +424,31 @@ public static Type GetValidatedInterfaceType(Type classType, ref Guid riid, obje
424
424
throw new InvalidCastException ( ) ;
425
425
}
426
426
427
- public static void ValidateObjectIsMarshallableAsInterface ( object obj , Type interfaceType )
427
+ public static IntPtr GetObjectAsInterface ( object obj , Type interfaceType )
428
428
{
429
- // If the requested "interface type" is type object then return
430
- // because type object is always marshallable.
429
+ // If the requested "interface type" is type object then return as IUnknown
431
430
if ( interfaceType == typeof ( object ) )
432
431
{
433
- return ;
432
+ return Marshal . GetIUnknownForObject ( obj ) ;
434
433
}
435
434
436
435
Debug . Assert ( interfaceType . IsInterface ) ;
437
436
438
- // The intent of this call is to validate the interface can be
437
+ // The intent of this call is to get AND validate the interface can be
439
438
// marshalled to native code. An exception will be thrown if the
440
439
// type is unable to be marshalled to native code.
441
440
// Scenarios where this is relevant:
442
441
// - Interfaces that use Generics
443
442
// - Interfaces that define implementation
444
- IntPtr ptr = Marshal . GetComInterfaceForObject ( obj , interfaceType , CustomQueryInterfaceMode . Ignore ) ;
443
+ IntPtr interfaceMaybe = Marshal . GetComInterfaceForObject ( obj , interfaceType , CustomQueryInterfaceMode . Ignore ) ;
445
444
446
- // Decrement the above 'Marshal.GetComInterfaceForObject()'
447
- Marshal . Release ( ptr ) ;
445
+ if ( interfaceMaybe == IntPtr . Zero )
446
+ {
447
+ // E_NOINTERFACE
448
+ throw new InvalidCastException ( ) ;
449
+ }
450
+
451
+ return interfaceMaybe ;
448
452
}
449
453
450
454
public static object CreateAggregatedObject ( object pUnkOuter , object comObject )
@@ -467,17 +471,17 @@ public static object CreateAggregatedObject(object pUnkOuter, object comObject)
467
471
public void CreateInstance (
468
472
[ MarshalAs ( UnmanagedType . Interface ) ] object ? pUnkOuter ,
469
473
ref Guid riid ,
470
- [ MarshalAs ( UnmanagedType . Interface ) ] out object ? ppvObject )
474
+ out IntPtr ppvObject )
471
475
{
472
476
Type interfaceType = BasicClassFactory . GetValidatedInterfaceType ( _classType , ref riid , pUnkOuter ) ;
473
477
474
- ppvObject = Activator . CreateInstance ( _classType ) ! ;
478
+ object obj = Activator . CreateInstance ( _classType ) ! ;
475
479
if ( pUnkOuter != null )
476
480
{
477
- ppvObject = BasicClassFactory . CreateAggregatedObject ( pUnkOuter , ppvObject ) ;
481
+ obj = BasicClassFactory . CreateAggregatedObject ( pUnkOuter , obj ) ;
478
482
}
479
483
480
- BasicClassFactory . ValidateObjectIsMarshallableAsInterface ( ppvObject , interfaceType ) ;
484
+ ppvObject = BasicClassFactory . GetObjectAsInterface ( obj , interfaceType ) ;
481
485
}
482
486
483
487
public void LockServer ( [ MarshalAs ( UnmanagedType . Bool ) ] bool fLock )
@@ -502,7 +506,7 @@ public LicenseClassFactory(Guid clsid, Type classType)
502
506
public void CreateInstance (
503
507
[ MarshalAs ( UnmanagedType . Interface ) ] object ? pUnkOuter ,
504
508
ref Guid riid ,
505
- [ MarshalAs ( UnmanagedType . Interface ) ] out object ? ppvObject )
509
+ out IntPtr ppvObject )
506
510
{
507
511
CreateInstanceInner ( pUnkOuter , ref riid , key : null , isDesignTime : true , out ppvObject ) ;
508
512
}
@@ -535,7 +539,7 @@ public void CreateInstanceLic(
535
539
[ MarshalAs ( UnmanagedType . Interface ) ] object ? pUnkReserved ,
536
540
ref Guid riid ,
537
541
[ MarshalAs ( UnmanagedType . BStr ) ] string bstrKey ,
538
- [ MarshalAs ( UnmanagedType . Interface ) ] out object ppvObject )
542
+ out IntPtr ppvObject )
539
543
{
540
544
Debug . Assert ( pUnkReserved == null ) ;
541
545
CreateInstanceInner ( pUnkOuter , ref riid , bstrKey , isDesignTime : false , out ppvObject ) ;
@@ -546,17 +550,17 @@ private void CreateInstanceInner(
546
550
ref Guid riid ,
547
551
string ? key ,
548
552
bool isDesignTime ,
549
- out object ppvObject )
553
+ out IntPtr ppvObject )
550
554
{
551
555
Type interfaceType = BasicClassFactory . GetValidatedInterfaceType ( _classType , ref riid , pUnkOuter ) ;
552
556
553
- ppvObject = _licenseProxy . AllocateAndValidateLicense ( _classType , key , isDesignTime ) ;
557
+ object obj = _licenseProxy . AllocateAndValidateLicense ( _classType , key , isDesignTime ) ;
554
558
if ( pUnkOuter != null )
555
559
{
556
- ppvObject = BasicClassFactory . CreateAggregatedObject ( pUnkOuter , ppvObject ) ;
560
+ obj = BasicClassFactory . CreateAggregatedObject ( pUnkOuter , obj ) ;
557
561
}
558
562
559
- BasicClassFactory . ValidateObjectIsMarshallableAsInterface ( ppvObject , interfaceType ) ;
563
+ ppvObject = BasicClassFactory . GetObjectAsInterface ( obj , interfaceType ) ;
560
564
}
561
565
}
562
566
}
0 commit comments