From 167b9fd3f3370a1a94d8085119ec508c0fafc585 Mon Sep 17 00:00:00 2001 From: Aaron Robinson Date: Mon, 4 Mar 2019 16:41:37 -0800 Subject: [PATCH] Update API for new RCW license impl in coreclr (dotnet/corefx#35767) Commit migrated from https://github.com/dotnet/corefx/commit/4f9e62149207fb1bbef18426bb302cea6869b78a --- .../src/ILLinkTrim.xml | 5 +- .../LicenseManager.LicenseInteropHelper.cs | 62 ++++------------------ .../src/System/ComponentModel/LicenseManager.cs | 8 --- .../tests/LicenseManagerInteropTests.cs | 42 --------------- ...ystem.ComponentModel.TypeConverter.Tests.csproj | 1 - 5 files changed, 10 insertions(+), 108 deletions(-) delete mode 100644 src/libraries/System.ComponentModel.TypeConverter/tests/LicenseManagerInteropTests.cs diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml b/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml index 3205d2f..badebaa 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml +++ b/src/libraries/System.ComponentModel.TypeConverter/src/ILLinkTrim.xml @@ -5,10 +5,7 @@ - - - - + diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.LicenseInteropHelper.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.LicenseInteropHelper.cs index c2d8e66..497dd6e 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.LicenseInteropHelper.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.LicenseInteropHelper.cs @@ -81,20 +81,8 @@ namespace System.ComponentModel // This is a helper class that supports the CLR's IClassFactory2 marshaling // support. - // - // When the CLR consumes an unmanaged COM object, the CLR invokes - // GetCurrentContextInfo() to figure out the licensing context - // and decide whether to call ICF::CreateInstance() (designtime) or - // ICF::CreateInstanceLic() (runtime). In the former case, it also - // requests the class factory for a runtime license key and invokes - // SaveKeyInCurrentContext() to stash a copy in the current licensing - // context - // private class LicenseInteropHelper { - private LicenseContext _savedLicenseContext; - private Type _savedType; - // Used to validate a type and retrieve license details // when activating a managed COM server from an IClassFactory2 instance. public static bool ValidateAndRetrieveLicenseDetails( @@ -118,51 +106,19 @@ namespace System.ComponentModel } // The CLR invokes this when instantiating an unmanaged COM - // object. The purpose is to decide which classfactory method to + // object. The purpose is to decide which IClassFactory method to // use. - // - // If the current context is design time, the CLR will - // use ICF::CreateInstance(). - // - // If the current context is runtime and the current context - // exposes a non-null license key and the COM object supports - // IClassFactory2, the CLR will use ICF2::CreateInstanceLic(). - // Otherwise, the CLR will use ICF::CreateInstance. - // - // Arguments: - // ref int fDesignTime: on exit, this will be set to indicate - // the nature of the current license context. - // ref int bstrKey: on exit, this will point to the - // licensekey saved inside the license context. - // (only if the license context is runtime) - // RuntimeTypeHandle rth: the managed type of the wrapper - private void GetCurrentContextInfo(ref int fDesignTime, ref IntPtr bstrKey, RuntimeTypeHandle rth) - { - _savedLicenseContext = LicenseManager.CurrentContext; - _savedType = Type.GetTypeFromHandle(rth); - if (_savedLicenseContext.UsageMode == LicenseUsageMode.Designtime) - { - fDesignTime = 1; - bstrKey = (IntPtr)0; - } - else - { - fDesignTime = 0; - String key = _savedLicenseContext.GetSavedLicenseKey(_savedType, null); - bstrKey = Marshal.StringToBSTR(key); - } - } - - // The CLR invokes this when instantiating a licensed COM - // object inside a designtime license context. - // It's purpose is to save away the license key that the CLR - // retrieved using RequestLicKey(). This license key can be NULL. - private void SaveKeyInCurrentContext(IntPtr bstrKey) + public static LicenseContext GetCurrentContextInfo(Type type, out bool isDesignTime, out string key) { - if (bstrKey != (IntPtr)0) + LicenseContext licContext = LicenseManager.CurrentContext; + isDesignTime = licContext.UsageMode == LicenseUsageMode.Designtime; + key = null; + if (!isDesignTime) { - _savedLicenseContext.SetSavedLicenseKey(_savedType, Marshal.PtrToStringBSTR(bstrKey)); + key = licContext.GetSavedLicenseKey(type, resourceAssembly: null); } + + return licContext; } } } diff --git a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.cs b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.cs index 3416809..2740767 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.cs +++ b/src/libraries/System.ComponentModel.TypeConverter/src/System/ComponentModel/LicenseManager.cs @@ -357,13 +357,5 @@ namespace System.ComponentModel return lic; } - - /// - /// Retrieves the typehandle of the interop helper - /// - private static IntPtr GetLicenseInteropHelperType() - { - return typeof(LicenseInteropHelper).TypeHandle.Value; - } } } diff --git a/src/libraries/System.ComponentModel.TypeConverter/tests/LicenseManagerInteropTests.cs b/src/libraries/System.ComponentModel.TypeConverter/tests/LicenseManagerInteropTests.cs deleted file mode 100644 index 29adff1..0000000 --- a/src/libraries/System.ComponentModel.TypeConverter/tests/LicenseManagerInteropTests.cs +++ /dev/null @@ -1,42 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// See the LICENSE file in the project root for more information. - -using Xunit; -using System.Runtime.InteropServices; - -namespace System.ComponentModel.Tests -{ - public class LicenseManagerInteropTests - { - [ComImport] - [CoClass(typeof(SpellCheckerFactoryCoClass))] - [Guid("8E018A9D-2415-4677-BF08-794EA61F94BB")] - internal interface SpellCheckerFactoryClass - { - } - - [ComImport] - [Guid("7AB36653-1796-484B-BDFA-E74F1DB7C1DC")] - [TypeLibType(TypeLibTypeFlags.FCanCreate)] - [ClassInterface(ClassInterfaceType.None)] - internal class SpellCheckerFactoryCoClass - { - } - - - // HasWindowsShell test eliminates IoT, Server Core, and Nano Server - [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.HasWindowsShell), nameof(PlatformDetection.IsWindows))] - public void CanCreateSpellChecker_Regression_core_1994() - { - // regression test for https://github.com/dotnet/core/issues/1994 - // ensure that we can create this object which implements IClassFactory2 - // we're mainly concerned that we don't AV - try - { - var spellCheck = new SpellCheckerFactoryClass(); - } - catch (InvalidCastException) - { } - } - } -} diff --git a/src/libraries/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj b/src/libraries/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj index fd14dda..83e2e8e 100644 --- a/src/libraries/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj +++ b/src/libraries/System.ComponentModel.TypeConverter/tests/System.ComponentModel.TypeConverter.Tests.csproj @@ -14,7 +14,6 @@ - -- 2.7.4