@@ -195,6 +195,9 @@ private static void PrelinkCore(MethodInfo m)
195
195
[ DllImport ( RuntimeHelpers . QCall , CharSet = CharSet . Unicode ) ]
196
196
private static extern void InternalPrelink ( RuntimeMethodHandleInternal m ) ;
197
197
198
+ [ DllImport ( RuntimeHelpers . QCall ) ]
199
+ private static extern bool IsComSupportedInternal ( ) ;
200
+
198
201
[ MethodImpl ( MethodImplOptions . InternalCall ) ]
199
202
public static extern /* struct _EXCEPTION_POINTERS* */ IntPtr GetExceptionPointers ( ) ;
200
203
@@ -233,6 +236,10 @@ private static object PtrToStructureHelper(IntPtr ptr, Type structureType)
233
236
[ MethodImpl ( MethodImplOptions . InternalCall ) ]
234
237
internal static extern bool IsPinnable ( object ? obj ) ;
235
238
239
+ internal static bool IsComSupported { get ; } = InitializeIsComSupported ( ) ;
240
+
241
+ private static bool InitializeIsComSupported ( ) => IsComSupportedInternal ( ) ;
242
+
236
243
#if TARGET_WINDOWS
237
244
/// <summary>
238
245
/// Returns the HInstance for this module. Returns -1 if the module doesn't have
@@ -289,6 +296,11 @@ public static string GetTypeInfoName(ITypeInfo typeInfo)
289
296
// on Marshal for more consistent API surface.
290
297
internal static Type ? GetTypeFromCLSID ( Guid clsid , string ? server , bool throwOnError )
291
298
{
299
+ if ( ! IsComSupported )
300
+ {
301
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
302
+ }
303
+
292
304
// Note: "throwOnError" is a vacuous parameter. Any errors due to the CLSID not being registered or the server not being found will happen
293
305
// on the Activator.CreateInstance() call. GetTypeFromCLSID() merely wraps the data in a Type object without any validation.
294
306
@@ -429,12 +441,27 @@ public static object GetUniqueObjectForIUnknown(IntPtr unknown)
429
441
public static extern object GetTypedObjectForIUnknown ( IntPtr /* IUnknown* */ pUnk , Type t ) ;
430
442
431
443
[ SupportedOSPlatform ( "windows" ) ]
444
+ public static IntPtr CreateAggregatedObject ( IntPtr pOuter , object o )
445
+ {
446
+ if ( ! IsComSupported )
447
+ {
448
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
449
+ }
450
+
451
+ return CreateAggregatedObjectNative ( pOuter , o ) ;
452
+ }
453
+
432
454
[ MethodImpl ( MethodImplOptions . InternalCall ) ]
433
- public static extern IntPtr CreateAggregatedObject ( IntPtr pOuter , object o ) ;
455
+ private static extern IntPtr CreateAggregatedObjectNative ( IntPtr pOuter , object o ) ;
434
456
435
457
[ SupportedOSPlatform ( "windows" ) ]
436
458
public static IntPtr CreateAggregatedObject < T > ( IntPtr pOuter , T o ) where T : notnull
437
459
{
460
+ if ( ! IsComSupported )
461
+ {
462
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
463
+ }
464
+
438
465
return CreateAggregatedObject ( pOuter , ( object ) o ) ;
439
466
}
440
467
@@ -457,6 +484,11 @@ public static IntPtr CreateAggregatedObject<T>(IntPtr pOuter, T o) where T : not
457
484
[ SupportedOSPlatform ( "windows" ) ]
458
485
public static int ReleaseComObject ( object o )
459
486
{
487
+ if ( ! IsComSupported )
488
+ {
489
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
490
+ }
491
+
460
492
if ( o is null )
461
493
{
462
494
// Match .NET Framework behaviour.
@@ -480,6 +512,11 @@ public static int ReleaseComObject(object o)
480
512
[ SupportedOSPlatform ( "windows" ) ]
481
513
public static int FinalReleaseComObject ( object o )
482
514
{
515
+ if ( ! IsComSupported )
516
+ {
517
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
518
+ }
519
+
483
520
if ( o is null )
484
521
{
485
522
throw new ArgumentNullException ( nameof ( o ) ) ;
@@ -499,6 +536,11 @@ public static int FinalReleaseComObject(object o)
499
536
[ SupportedOSPlatform ( "windows" ) ]
500
537
public static object ? GetComObjectData ( object obj , object key )
501
538
{
539
+ if ( ! IsComSupported )
540
+ {
541
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
542
+ }
543
+
502
544
if ( obj is null )
503
545
{
504
546
throw new ArgumentNullException ( nameof ( obj ) ) ;
@@ -525,6 +567,11 @@ public static int FinalReleaseComObject(object o)
525
567
[ SupportedOSPlatform ( "windows" ) ]
526
568
public static bool SetComObjectData ( object obj , object key , object ? data )
527
569
{
570
+ if ( ! IsComSupported )
571
+ {
572
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
573
+ }
574
+
528
575
if ( obj is null )
529
576
{
530
577
throw new ArgumentNullException ( nameof ( obj ) ) ;
@@ -550,6 +597,11 @@ public static bool SetComObjectData(object obj, object key, object? data)
550
597
[ return : NotNullIfNotNull ( "o" ) ]
551
598
public static object ? CreateWrapperOfType ( object ? o , Type t )
552
599
{
600
+ if ( ! IsComSupported )
601
+ {
602
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
603
+ }
604
+
553
605
if ( t is null )
554
606
{
555
607
throw new ArgumentNullException ( nameof ( t ) ) ;
@@ -600,6 +652,11 @@ public static bool SetComObjectData(object obj, object key, object? data)
600
652
[ SupportedOSPlatform ( "windows" ) ]
601
653
public static TWrapper CreateWrapperOfType < T , TWrapper > ( T ? o )
602
654
{
655
+ if ( ! IsComSupported )
656
+ {
657
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
658
+ }
659
+
603
660
return ( TWrapper ) CreateWrapperOfType ( o , typeof ( TWrapper ) ) ! ;
604
661
}
605
662
@@ -613,32 +670,77 @@ public static TWrapper CreateWrapperOfType<T, TWrapper>(T? o)
613
670
public static extern bool IsTypeVisibleFromCom ( Type t ) ;
614
671
615
672
[ SupportedOSPlatform ( "windows" ) ]
673
+ public static void GetNativeVariantForObject ( object ? obj , /* VARIANT * */ IntPtr pDstNativeVariant )
674
+ {
675
+ if ( ! IsComSupported )
676
+ {
677
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
678
+ }
679
+
680
+ GetNativeVariantForObjectNative ( obj , pDstNativeVariant ) ;
681
+ }
682
+
616
683
[ MethodImpl ( MethodImplOptions . InternalCall ) ]
617
- public static extern void GetNativeVariantForObject ( object ? obj , /* VARIANT * */ IntPtr pDstNativeVariant ) ;
684
+ private static extern void GetNativeVariantForObjectNative ( object ? obj , /* VARIANT * */ IntPtr pDstNativeVariant ) ;
618
685
619
686
[ SupportedOSPlatform ( "windows" ) ]
620
687
public static void GetNativeVariantForObject < T > ( T ? obj , IntPtr pDstNativeVariant )
621
688
{
689
+ if ( ! IsComSupported )
690
+ {
691
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
692
+ }
693
+
622
694
GetNativeVariantForObject ( ( object ? ) obj , pDstNativeVariant ) ;
623
695
}
624
696
625
697
[ SupportedOSPlatform ( "windows" ) ]
698
+ public static object ? GetObjectForNativeVariant ( /* VARIANT * */ IntPtr pSrcNativeVariant )
699
+ {
700
+ if ( ! IsComSupported )
701
+ {
702
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
703
+ }
704
+
705
+ return GetObjectForNativeVariantNative ( pSrcNativeVariant ) ;
706
+ }
707
+
626
708
[ MethodImpl ( MethodImplOptions . InternalCall ) ]
627
- public static extern object ? GetObjectForNativeVariant ( /* VARIANT * */ IntPtr pSrcNativeVariant ) ;
709
+ private static extern object ? GetObjectForNativeVariantNative ( /* VARIANT * */ IntPtr pSrcNativeVariant ) ;
628
710
629
711
[ SupportedOSPlatform ( "windows" ) ]
630
712
public static T ? GetObjectForNativeVariant < T > ( IntPtr pSrcNativeVariant )
631
713
{
714
+ if ( ! IsComSupported )
715
+ {
716
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
717
+ }
718
+
632
719
return ( T ? ) GetObjectForNativeVariant ( pSrcNativeVariant ) ;
633
720
}
634
721
635
722
[ SupportedOSPlatform ( "windows" ) ]
723
+ public static object ? [ ] GetObjectsForNativeVariants ( /* VARIANT * */ IntPtr aSrcNativeVariant , int cVars )
724
+ {
725
+ if ( ! IsComSupported )
726
+ {
727
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
728
+ }
729
+
730
+ return GetObjectsForNativeVariantsNative ( aSrcNativeVariant , cVars ) ;
731
+ }
732
+
636
733
[ MethodImpl ( MethodImplOptions . InternalCall ) ]
637
- public static extern object ? [ ] GetObjectsForNativeVariants ( /* VARIANT * */ IntPtr aSrcNativeVariant , int cVars ) ;
734
+ private static extern object ? [ ] GetObjectsForNativeVariantsNative ( /* VARIANT * */ IntPtr aSrcNativeVariant , int cVars ) ;
638
735
639
736
[ SupportedOSPlatform ( "windows" ) ]
640
737
public static T [ ] GetObjectsForNativeVariants < T > ( IntPtr aSrcNativeVariant , int cVars )
641
738
{
739
+ if ( ! IsComSupported )
740
+ {
741
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
742
+ }
743
+
642
744
object ? [ ] objects = GetObjectsForNativeVariants ( aSrcNativeVariant , cVars ) ;
643
745
644
746
T [ ] result = new T [ objects . Length ] ;
@@ -665,6 +767,11 @@ public static T[] GetObjectsForNativeVariants<T>(IntPtr aSrcNativeVariant, int c
665
767
[ SupportedOSPlatform ( "windows" ) ]
666
768
public static object BindToMoniker ( string monikerName )
667
769
{
770
+ if ( ! IsComSupported )
771
+ {
772
+ throw new NotSupportedException ( SR . NotSupported_COM ) ;
773
+ }
774
+
668
775
CreateBindCtx ( 0 , out IBindCtx bindctx ) ;
669
776
670
777
MkParseDisplayName ( bindctx , monikerName , out _ , out IMoniker pmoniker ) ;
0 commit comments