diff --git a/src/coreclr/vm/ecall.cpp b/src/coreclr/vm/ecall.cpp index 993a1299c3ee01..ca48e537c0aab4 100644 --- a/src/coreclr/vm/ecall.cpp +++ b/src/coreclr/vm/ecall.cpp @@ -336,12 +336,9 @@ PCODE ECall::GetFCallImpl(MethodDesc * pMD, BOOL * pfSharedOrDynamicFCallImpl /* return CoreLibBinder::GetMethod(METHOD__DELEGATE__CONSTRUCT_DELEGATE)->GetMultiCallableAddrOfCode(); } - // COM imported classes have special constructors - if (pMT->IsComObjectType() #ifdef FEATURE_COMINTEROP - && (g_pBaseCOMObject == NULL || pMT != g_pBaseCOMObject) -#endif // FEATURE_COMINTEROP - ) + // COM imported classes have special constructors + if (pMT->IsComObjectType() && pMT != g_pBaseCOMObject) { if (pfSharedOrDynamicFCallImpl) *pfSharedOrDynamicFCallImpl = TRUE; @@ -352,6 +349,12 @@ PCODE ECall::GetFCallImpl(MethodDesc * pMD, BOOL * pfSharedOrDynamicFCallImpl /* // FCComCtor does not need to be in the fcall hashtable since it does not erect frame. return GetEEFuncEntryPoint(FCComCtor); } +#else // !FEATURE_COMINTEROP + // This code path is taken when a class marked with ComImport is being created. + // If we get here and COM interop isn't suppported, throw. + if (pMT->IsComObjectType()) + COMPlusThrow(kPlatformNotSupportedException, IDS_EE_ERROR_COM); +#endif // FEATURE_COMINTEROP if (!pMD->GetModule()->IsSystem()) COMPlusThrow(kSecurityException, BFA_ECALLS_MUST_BE_IN_SYS_MOD); diff --git a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs index 8c9f6ea247c580..347c01b7a3c410 100644 --- a/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs +++ b/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs @@ -127,6 +127,22 @@ static GenericHasCctor() } } + [ComImport] + [Guid("00000000-0000-0000-0000-000000000000")] + interface ComInterface + { + void Func(); + } + + // This class is used to test the PrepareMethod API with COM interop on non-Windows platforms. + [ComImport] + [Guid("00000000-0000-0000-0000-000000000000")] + class ComClass : ComInterface + { + [MethodImpl(MethodImplOptions.InternalCall)] + public extern void Func(); + } + [Fact] public static void PrepareMethod() { @@ -139,6 +155,16 @@ public static void PrepareMethod() { Assert.ThrowsAny(() => RuntimeHelpers.PrepareMethod(typeof(IList).GetMethod("Add").MethodHandle)); } + + try + { + // This is expected to either succeed or throw PlatformNotSupportedException depending on the platform + // and runtime flavor + RuntimeHelpers.PrepareMethod(typeof(ComClass).GetMethod("Func").MethodHandle); + } + catch (PlatformNotSupportedException) + { + } } [Fact]