private void RegisterPrimaryInteropAssembly(Assembly assembly, String strAsmCodeBase, PrimaryInteropAssemblyAttribute attr)
        {
            // Validate that the PIA has a strong name.
            if (assembly.nGetPublicKey() == null)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_PIAMustBeStrongNamed"));
            }

            String strTlbId   = "{" + Marshal.GetTypeLibGuidForAssembly(assembly).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            String strVersion = attr.MajorVersion + "." + attr.MinorVersion;

            // Create the HKEY_CLASS_ROOT\TypeLib<TLBID> key.
            RegistryKey TypeLibRootKey = Registry.ClassesRoot.CreateSubKey("TypeLib");
            RegistryKey TypeLibKey     = TypeLibRootKey.CreateSubKey(strTlbId);

            // Create the HKEY_CLASS_ROOT\TypeLib<TLBID>\<Major.Minor> key.
            RegistryKey VersionKey = TypeLibKey.CreateSubKey(strVersion);

            // Create the HKEY_CLASS_ROOT\TypeLib<TLBID>\PrimaryInteropAssembly key.
            VersionKey.SetValue("PrimaryInteropAssemblyName", assembly.FullName);
            if (strAsmCodeBase != null)
            {
                VersionKey.SetValue("PrimaryInteropAssemblyCodeBase", strAsmCodeBase);
            }

            // Close the registry keys.
            VersionKey.Close();
            TypeLibKey.Close();
            TypeLibRootKey.Close();
        }
예제 #2
0
        private void RegisterPrimaryInteropAssembly(RuntimeAssembly assembly, string strAsmCodeBase, PrimaryInteropAssemblyAttribute attr)
        {
            if (assembly.GetPublicKey().Length == 0)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_PIAMustBeStrongNamed"));
            }
            string subkey1 = "{" + Marshal.GetTypeLibGuidForAssembly((Assembly)assembly).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            string subkey2 = attr.MajorVersion.ToString("x", (IFormatProvider)CultureInfo.InvariantCulture) + "." + attr.MinorVersion.ToString("x", (IFormatProvider)CultureInfo.InvariantCulture);

            using (RegistryKey subKey1 = Registry.ClassesRoot.CreateSubKey("TypeLib"))
            {
                using (RegistryKey subKey2 = subKey1.CreateSubKey(subkey1))
                {
                    using (RegistryKey subKey3 = subKey2.CreateSubKey(subkey2))
                    {
                        subKey3.SetValue("PrimaryInteropAssemblyName", (object)assembly.FullName);
                        if (strAsmCodeBase == null)
                        {
                            return;
                        }
                        subKey3.SetValue("PrimaryInteropAssemblyCodeBase", (object)strAsmCodeBase);
                    }
                }
            }
        }
예제 #3
0
        private void UnregisterPrimaryInteropAssembly(Assembly assembly, PrimaryInteropAssemblyAttribute attr)
        {
            string str1    = "{" + Marshal.GetTypeLibGuidForAssembly(assembly).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            int    num     = attr.MajorVersion;
            string string1 = num.ToString("x", (IFormatProvider)CultureInfo.InvariantCulture);
            string str2    = ".";

            num = attr.MinorVersion;
            string string2 = num.ToString("x", (IFormatProvider)CultureInfo.InvariantCulture);
            string str3    = string1 + str2 + string2;

            using (RegistryKey registryKey1 = Registry.ClassesRoot.OpenSubKey("TypeLib", true))
            {
                if (registryKey1 == null)
                {
                    return;
                }
                using (RegistryKey registryKey2 = registryKey1.OpenSubKey(str1, true))
                {
                    if (registryKey2 != null)
                    {
                        using (RegistryKey registryKey3 = registryKey2.OpenSubKey(str3, true))
                        {
                            if (registryKey3 != null)
                            {
                                registryKey3.DeleteValue("PrimaryInteropAssemblyName", false);
                                registryKey3.DeleteValue("PrimaryInteropAssemblyCodeBase", false);
                                if (registryKey3.SubKeyCount == 0)
                                {
                                    if (registryKey3.ValueCount == 0)
                                    {
                                        registryKey2.DeleteSubKey(str3);
                                    }
                                }
                            }
                        }
                        if (registryKey2.SubKeyCount == 0)
                        {
                            if (registryKey2.ValueCount == 0)
                            {
                                registryKey1.DeleteSubKey(str1);
                            }
                        }
                    }
                }
                if (registryKey1.SubKeyCount != 0 || registryKey1.ValueCount != 0)
                {
                    return;
                }
                Registry.ClassesRoot.DeleteSubKey("TypeLib");
            }
        }
        private void UnregisterPrimaryInteropAssembly(Assembly assembly, PrimaryInteropAssemblyAttribute attr)
        {
            String strTlbId   = "{" + Marshal.GetTypeLibGuidForAssembly(assembly).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            String strVersion = attr.MajorVersion + "." + attr.MinorVersion;

            // Try to open the HKEY_CLASS_ROOT\TypeLib key.
            RegistryKey TypeLibRootKey = Registry.ClassesRoot.OpenSubKey("TypeLib", true);

            if (TypeLibRootKey != null)
            {
                // Try to open the HKEY_CLASS_ROOT\TypeLib\<TLBID> key.
                RegistryKey TypeLibKey = TypeLibRootKey.OpenSubKey(strTlbId);
                if (TypeLibKey != null)
                {
                    // Try to open the HKEY_CLASS_ROOT\TypeLib<TLBID>\<Major.Minor> key.
                    RegistryKey VersionKey = TypeLibKey.OpenSubKey(strVersion, true);
                    if (VersionKey != null)
                    {
                        // Delete the values we created.
                        VersionKey.DeleteValue("PrimaryInteropAssemblyName", false);
                        VersionKey.DeleteValue("PrimaryInteropAssemblyCodeBase", false);

                        // If there are no other values or subkeys then we can delete the VersionKey.
                        if ((VersionKey.SubKeyCount == 0) && (VersionKey.ValueCount == 0))
                        {
                            TypeLibKey.DeleteSubKey(strVersion);
                        }

                        // Close the key.
                        VersionKey.Close();
                    }

                    // If there are no other values or subkeys then we can delete the TypeLibKey.
                    if ((TypeLibKey.SubKeyCount == 0) && (TypeLibKey.ValueCount == 0))
                    {
                        TypeLibRootKey.DeleteSubKey(strTlbId);
                    }

                    // Close the TypeLibKey.
                    TypeLibKey.Close();
                }

                // Close the TypeLibRootKey.
                TypeLibRootKey.Close();
            }
        }
예제 #5
0
        private void UnregisterPrimaryInteropAssembly(Assembly assembly, PrimaryInteropAssemblyAttribute attr)
        {
            string text  = "{" + Marshal.GetTypeLibGuidForAssembly(assembly).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            string text2 = attr.MajorVersion.ToString("x", CultureInfo.InvariantCulture) + "." + attr.MinorVersion.ToString("x", CultureInfo.InvariantCulture);

            using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("TypeLib", true))
            {
                if (registryKey != null)
                {
                    using (RegistryKey registryKey2 = registryKey.OpenSubKey(text, true))
                    {
                        if (registryKey2 != null)
                        {
                            using (RegistryKey registryKey3 = registryKey2.OpenSubKey(text2, true))
                            {
                                if (registryKey3 != null)
                                {
                                    registryKey3.DeleteValue("PrimaryInteropAssemblyName", false);
                                    registryKey3.DeleteValue("PrimaryInteropAssemblyCodeBase", false);
                                    if (registryKey3.SubKeyCount == 0 && registryKey3.ValueCount == 0)
                                    {
                                        registryKey2.DeleteSubKey(text2);
                                    }
                                }
                            }
                            if (registryKey2.SubKeyCount == 0 && registryKey2.ValueCount == 0)
                            {
                                registryKey.DeleteSubKey(text);
                            }
                        }
                    }
                    if (registryKey.SubKeyCount == 0 && registryKey.ValueCount == 0)
                    {
                        Registry.ClassesRoot.DeleteSubKey("TypeLib");
                    }
                }
            }
        }