// The CLR invokes this whenever a COM client invokes // IClassFactory2::GetLicInfo on a managed class. // // COM normally doesn't expect this function to fail so this method // should only throw in the case of a catastrophic error (stack, memory, etc.) private void GetLicInfo(RuntimeTypeHandle rth, ref int pRuntimeKeyAvail, ref int pLicVerified) { pRuntimeKeyAvail = 0; pLicVerified = 0; Type type = Type.GetTypeFromHandle(rth); License license; string licenseKey; if (helperContext == null) { helperContext = new DesigntimeLicenseContext(); } else { helperContext.savedLicenseKeys.Clear(); } if (LicenseManager.ValidateInternalRecursive(helperContext, type, null, false, out license, out licenseKey)) { if (helperContext.savedLicenseKeys.Contains(type.AssemblyQualifiedName)) { pRuntimeKeyAvail = 1; } if (license != null) { license.Dispose(); license = null; pLicVerified = 1; } } }
// The CLR invokes this whenever a COM client invokes // IClassFactory2::RequestLicKey on a managed class. // // This method should return the appropriate HRESULT and set pbstrKey // to the licensing key. private static int RequestLicKey(RuntimeTypeHandle rth, ref IntPtr pbstrKey) { Type type = Type.GetTypeFromHandle(rth); License license; string licenseKey; // license will be null, since we passed no instance, // however we can still retrieve the "first" license // key from the file. This really will only // work for simple COM-compatible license providers // like LicFileLicenseProvider that don't require the // instance to grant a key. // if (!LicenseManager.ValidateInternalRecursive(LicenseManager.CurrentContext, type, null, false, out license, out licenseKey)) { return(E_FAIL); } if (licenseKey == null) { return(E_FAIL); } pbstrKey = Marshal.StringToBSTR(licenseKey); if (license != null) { license.Dispose(); license = null; } return(S_OK); }
/// <summary>Creates an instance of the specified type with the specified arguments, given a context in which you can use the licensed instance.</summary> /// <returns>An instance of the specified type with the given array of arguments.</returns> /// <param name="type">A <see cref="T:System.Type" /> that represents the type to create. </param> /// <param name="creationContext">A <see cref="T:System.ComponentModel.LicenseContext" /> that specifies when you can use the licensed instance. </param> /// <param name="args">An array of type <see cref="T:System.Object" /> that represents the arguments for the type. </param> public static object CreateWithContext(Type type, LicenseContext creationContext, object[] args) { object result = null; object obj = LicenseManager.lockObject; lock (obj) { object contextUser = new object(); LicenseContext currentContext = LicenseManager.CurrentContext; LicenseManager.CurrentContext = creationContext; LicenseManager.LockContext(contextUser); try { result = Activator.CreateInstance(type, args); } catch (TargetInvocationException ex) { throw ex.InnerException; } finally { LicenseManager.UnlockContext(contextUser); LicenseManager.CurrentContext = currentContext; } } return(result); }
/// <summary>Determines whether a license can be granted for the instance of the specified type.</summary> /// <returns>A valid <see cref="T:System.ComponentModel.License" />.</returns> /// <param name="type">A <see cref="T:System.Type" /> that represents the type of object that requests the license. </param> /// <param name="instance">An <see cref="T:System.Object" /> of the specified type or a type derived from the specified type. </param> /// <exception cref="T:System.ComponentModel.LicenseException">The type is licensed, but a <see cref="T:System.ComponentModel.License" /> cannot be granted. </exception> public static License Validate(Type type, object instance) { License result = null; if (!LicenseManager.privateGetLicense(type, instance, true, out result)) { throw new LicenseException(type, instance); } return(result); }
/// <summary>Determines whether a license can be granted for the specified type.</summary> /// <param name="type">A <see cref="T:System.Type" /> that represents the type of object that requests the license. </param> /// <exception cref="T:System.ComponentModel.LicenseException">A <see cref="T:System.ComponentModel.License" /> cannot be granted. </exception> public static void Validate(Type type) { License license = null; if (!LicenseManager.privateGetLicense(type, null, true, out license)) { throw new LicenseException(type, null); } if (license != null) { license.Dispose(); } }
/// <summary>Determines whether a valid license can be granted for the specified type.</summary> /// <returns>true if a valid license can be granted; otherwise, false.</returns> /// <param name="type">A <see cref="T:System.Type" /> that represents the type of object that requests the <see cref="T:System.ComponentModel.License" />. </param> public static bool IsValid(Type type) { License license = null; if (!LicenseManager.privateGetLicense(type, null, false, out license)) { return(false); } if (license != null) { license.Dispose(); } return(true); }
// Used to validate a type and retrieve license details // when activating a managed COM server from an IClassFactory2 instance. public static bool ValidateAndRetrieveLicenseDetails( LicenseContext context, Type type, out License?license, out string?licenseKey) { context ??= LicenseManager.CurrentContext; return(LicenseManager.ValidateInternalRecursive( context, type, instance: null, allowExceptions: false, out license, out licenseKey)); }
// The CLR invokes this whenever a COM client invokes // IClassFactory::CreateInstance() or IClassFactory2::CreateInstanceLic() // on a managed managed that has a LicenseProvider custom attribute. // // If we are being entered because of a call to ICF::CreateInstance(), // fDesignTime will be "true". // // If we are being entered because of a call to ICF::CreateInstanceLic(), // fDesignTime will be "false" and bstrKey will point a non-null // license key. private static object AllocateAndValidateLicense(RuntimeTypeHandle rth, IntPtr bstrKey, int fDesignTime) { Type type = Type.GetTypeFromHandle(rth); CLRLicenseContext licensecontext = new CLRLicenseContext(fDesignTime != 0 ? LicenseUsageMode.Designtime : LicenseUsageMode.Runtime, type); if (fDesignTime == 0 && bstrKey != (IntPtr)0) { licensecontext.SetSavedLicenseKey(type, Marshal.PtrToStringBSTR(bstrKey)); } try { return(LicenseManager.CreateWithContext(type, licensecontext)); } catch (LicenseException lexp) { throw new COMException(lexp.Message, CLASS_E_NOTLICENSED); } }
private static object AllocateAndValidateLicense(RuntimeTypeHandle rth, IntPtr bstrKey, int fDesignTime) { object obj2; Type typeFromHandle = Type.GetTypeFromHandle(rth); CLRLicenseContext creationContext = new CLRLicenseContext((fDesignTime != 0) ? LicenseUsageMode.Designtime : LicenseUsageMode.Runtime, typeFromHandle); if ((fDesignTime == 0) && (bstrKey != IntPtr.Zero)) { creationContext.SetSavedLicenseKey(typeFromHandle, Marshal.PtrToStringBSTR(bstrKey)); } try { obj2 = LicenseManager.CreateWithContext(typeFromHandle, creationContext); } catch (LicenseException exception) { throw new COMException(exception.Message, -2147221230); } return(obj2); }
private static int RequestLicKey(RuntimeTypeHandle rth, ref IntPtr pbstrKey) { License license; string str; Type typeFromHandle = Type.GetTypeFromHandle(rth); if (!LicenseManager.ValidateInternalRecursive(LicenseManager.CurrentContext, typeFromHandle, null, false, out license, out str)) { return(-2147483640); } if (str == null) { return(-2147483640); } pbstrKey = Marshal.StringToBSTR(str); if (license != null) { license.Dispose(); license = null; } return(0); }
/// <summary>Creates an instance of the specified type, given a context in which you can use the licensed instance.</summary> /// <returns>An instance of the specified type.</returns> /// <param name="type">A <see cref="T:System.Type" /> that represents the type to create. </param> /// <param name="creationContext">A <see cref="T:System.ComponentModel.LicenseContext" /> that specifies when you can use the licensed instance. </param> public static object CreateWithContext(Type type, LicenseContext creationContext) { return(LicenseManager.CreateWithContext(type, creationContext, new object[0])); }
/// <summary>Determines whether a valid license can be granted for the specified instance of the type. This method creates a valid <see cref="T:System.ComponentModel.License" />.</summary> /// <returns>true if a valid <see cref="T:System.ComponentModel.License" /> can be granted; otherwise, false.</returns> /// <param name="type">A <see cref="T:System.Type" /> that represents the type of object that requests the license. </param> /// <param name="instance">An object of the specified type or a type derived from the specified type. </param> /// <param name="license">A <see cref="T:System.ComponentModel.License" /> that is a valid license, or null if a valid license cannot be granted. </param> public static bool IsValid(Type type, object instance, out License license) { return(LicenseManager.privateGetLicense(type, null, false, out license)); }