Пример #1
0
        private bool UnregisterComImportedType(Type type, string strAsmVersion)
        {
            bool   flag = true;
            string name = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";

            using (RegistryKey key = Registry.ClassesRoot.OpenSubKey("CLSID", true))
            {
                if (key == null)
                {
                    return(flag);
                }
                using (RegistryKey key2 = key.OpenSubKey(name, true))
                {
                    if (key2 != null)
                    {
                        using (RegistryKey key3 = key2.OpenSubKey("InprocServer32", true))
                        {
                            if (key3 != null)
                            {
                                key3.DeleteValue("Assembly", false);
                                key3.DeleteValue("Class", false);
                                key3.DeleteValue("RuntimeVersion", false);
                                key3.DeleteValue("CodeBase", false);
                                using (RegistryKey key4 = key3.OpenSubKey(strAsmVersion, true))
                                {
                                    if (key4 != null)
                                    {
                                        key4.DeleteValue("Assembly", false);
                                        key4.DeleteValue("Class", false);
                                        key4.DeleteValue("RuntimeVersion", false);
                                        key4.DeleteValue("CodeBase", false);
                                        if ((key4.SubKeyCount == 0) && (key4.ValueCount == 0))
                                        {
                                            key3.DeleteSubKey(strAsmVersion);
                                        }
                                    }
                                }
                                if (key3.SubKeyCount != 0)
                                {
                                    flag = false;
                                }
                                if ((key3.SubKeyCount == 0) && (key3.ValueCount == 0))
                                {
                                    key2.DeleteSubKey("InprocServer32");
                                }
                            }
                        }
                        if ((key2.SubKeyCount == 0) && (key2.ValueCount == 0))
                        {
                            key.DeleteSubKey(name);
                        }
                    }
                }
                if ((key.SubKeyCount == 0) && (key.ValueCount == 0))
                {
                    Registry.ClassesRoot.DeleteSubKey("CLSID");
                }
            }
            return(flag);
        }
        /// <include file='doc\RegistrationServices.uex' path='docs/doc[@for="RegistrationServices.TypeRepresentsComType"]/*' />
        public virtual bool TypeRepresentsComType(Type type)
        {
            // If the type is not a COM import, then it does not represent a COM type.
            if (!type.IsCOMObject)
            {
                return(false);
            }

            // If it is marked as tdImport, then it represents a COM type directly.
            if (type.IsImport)
            {
                return(true);
            }

            // If the type is derived from a tdImport class and has the same GUID as the
            // imported class, then it represents a COM type.
            Type baseComImportType = GetBaseComImportType(type);

            BCLDebug.Assert(baseComImportType != null, "baseComImportType != null");
            if (Marshal.GenerateGuidForType(type) == Marshal.GenerateGuidForType(baseComImportType))
            {
                return(true);
            }

            return(false);
        }
Пример #3
0
        private void RegisterComImportedType(Type type, string strAsmName, string strAsmVersion, string strAsmCodeBase, string strRuntimeVersion)
        {
            string subkey = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";

            using (RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey("CLSID"))
            {
                using (RegistryKey registryKey2 = registryKey.CreateSubKey(subkey))
                {
                    using (RegistryKey registryKey3 = registryKey2.CreateSubKey("InprocServer32"))
                    {
                        registryKey3.SetValue("Class", type.FullName);
                        registryKey3.SetValue("Assembly", strAsmName);
                        registryKey3.SetValue("RuntimeVersion", strRuntimeVersion);
                        if (strAsmCodeBase != null)
                        {
                            registryKey3.SetValue("CodeBase", strAsmCodeBase);
                        }
                        using (RegistryKey registryKey4 = registryKey3.CreateSubKey(strAsmVersion))
                        {
                            registryKey4.SetValue("Class", type.FullName);
                            registryKey4.SetValue("Assembly", strAsmName);
                            registryKey4.SetValue("RuntimeVersion", strRuntimeVersion);
                            if (strAsmCodeBase != null)
                            {
                                registryKey4.SetValue("CodeBase", strAsmCodeBase);
                            }
                        }
                    }
                }
            }
        }
        private void RegisterValueType(Type type, String strAsmName, String strAsmVersion, String strAsmCodeBase)
        {
            // Retrieve some information that will be used during the registration process.
            String strRecordId = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper() + "}";

            // Create the HKEY_CLASS_ROOT\Record key.
            RegistryKey RecordRootKey = Registry.ClassesRoot.CreateSubKey(strRecordRootName);

            // Create the HKEY_CLASS_ROOT\Record\<RecordID> key.
            RegistryKey RecordKey = RecordRootKey.CreateSubKey(strRecordId);

            // Create the HKCR\Record\<RecordId>\<version> key.
            RegistryKey RecordVersionKey = RecordKey.CreateSubKey(strAsmVersion);

            // Set the class value.
            RecordVersionKey.SetValue("Class", type.FullName);

            // Set the assembly value.
            RecordVersionKey.SetValue("Assembly", strAsmName);

            // Set the assembly code base value if a code base was specified.
            if (strAsmCodeBase != null)
            {
                RecordVersionKey.SetValue("CodeBase", strAsmCodeBase);
            }

            // Close the registry keys.
            RecordVersionKey.Close();
            RecordKey.Close();
            RecordRootKey.Close();
        }
Пример #5
0
        private void RegisterManagedType(Type type, string strAsmName, string strAsmVersion, string strAsmCodeBase, string strRuntimeVersion)
        {
            string str           = type.FullName ?? "";
            string subkey        = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            string progIdForType = this.GetProgIdForType(type);

            if (progIdForType != string.Empty)
            {
                using (RegistryKey subKey1 = Registry.ClassesRoot.CreateSubKey(progIdForType))
                {
                    subKey1.SetValue("", (object)str);
                    using (RegistryKey subKey2 = subKey1.CreateSubKey("CLSID"))
                        subKey2.SetValue("", (object)subkey);
                }
            }
            using (RegistryKey subKey1 = Registry.ClassesRoot.CreateSubKey("CLSID"))
            {
                using (RegistryKey subKey2 = subKey1.CreateSubKey(subkey))
                {
                    subKey2.SetValue("", (object)str);
                    using (RegistryKey subKey3 = subKey2.CreateSubKey("InprocServer32"))
                    {
                        subKey3.SetValue("", (object)"mscoree.dll");
                        subKey3.SetValue("ThreadingModel", (object)"Both");
                        subKey3.SetValue("Class", (object)type.FullName);
                        subKey3.SetValue("Assembly", (object)strAsmName);
                        subKey3.SetValue("RuntimeVersion", (object)strRuntimeVersion);
                        if (strAsmCodeBase != null)
                        {
                            subKey3.SetValue("CodeBase", (object)strAsmCodeBase);
                        }
                        using (RegistryKey subKey4 = subKey3.CreateSubKey(strAsmVersion))
                        {
                            subKey4.SetValue("Class", (object)type.FullName);
                            subKey4.SetValue("Assembly", (object)strAsmName);
                            subKey4.SetValue("RuntimeVersion", (object)strRuntimeVersion);
                            if (strAsmCodeBase != null)
                            {
                                subKey4.SetValue("CodeBase", (object)strAsmCodeBase);
                            }
                        }
                        if (progIdForType != string.Empty)
                        {
                            using (RegistryKey subKey4 = subKey2.CreateSubKey("ProgId"))
                                subKey4.SetValue("", (object)progIdForType);
                        }
                    }
                    using (RegistryKey subKey3 = subKey2.CreateSubKey("Implemented Categories"))
                    {
                        using (subKey3.CreateSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}"))
                            ;
                    }
                }
            }
            this.EnsureManagedCategoryExists();
        }
Пример #6
0
        private bool UnregisterValueType(Type type, string strAsmVersion)
        {
            bool   flag = true;
            string str  = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";

            using (RegistryKey registryKey1 = Registry.ClassesRoot.OpenSubKey("Record", true))
            {
                if (registryKey1 != null)
                {
                    using (RegistryKey registryKey2 = registryKey1.OpenSubKey(str, true))
                    {
                        if (registryKey2 != null)
                        {
                            using (RegistryKey registryKey3 = registryKey2.OpenSubKey(strAsmVersion, true))
                            {
                                if (registryKey3 != null)
                                {
                                    registryKey3.DeleteValue("Assembly", false);
                                    registryKey3.DeleteValue("Class", false);
                                    registryKey3.DeleteValue("CodeBase", false);
                                    registryKey3.DeleteValue("RuntimeVersion", false);
                                    if (registryKey3.SubKeyCount == 0)
                                    {
                                        if (registryKey3.ValueCount == 0)
                                        {
                                            registryKey2.DeleteSubKey(strAsmVersion);
                                        }
                                    }
                                }
                            }
                            if (registryKey2.SubKeyCount != 0)
                            {
                                flag = false;
                            }
                            if (registryKey2.SubKeyCount == 0)
                            {
                                if (registryKey2.ValueCount == 0)
                                {
                                    registryKey1.DeleteSubKey(str);
                                }
                            }
                        }
                    }
                    if (registryKey1.SubKeyCount == 0)
                    {
                        if (registryKey1.ValueCount == 0)
                        {
                            Registry.ClassesRoot.DeleteSubKey("Record");
                        }
                    }
                }
            }
            return(flag);
        }
Пример #7
0
        public virtual bool TypeRepresentsComType(Type type)
        {
            if (!type.IsCOMObject)
            {
                return(false);
            }
            if (type.IsImport)
            {
                return(true);
            }
            Type baseComImportType = this.GetBaseComImportType(type);

            return(Marshal.GenerateGuidForType(type) == Marshal.GenerateGuidForType(baseComImportType));
        }
        private void RegisterComImportedType(Type type, String strAsmName, String strAsmVersion, String strAsmCodeBase, String strRuntimeVersion)
        {
            // Retrieve some information that will be used during the registration process.
            String strClsId = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";

            // Create the HKEY_CLASS_ROOT\CLSID\<CLSID> key.
            RegistryKey ClsIdRootKey = Registry.ClassesRoot.CreateSubKey("CLSID");
            RegistryKey ClsIdKey     = ClsIdRootKey.CreateSubKey(strClsId);

            // Create the InProcServer32 key.
            RegistryKey InProcServerKey = ClsIdKey.CreateSubKey("InprocServer32");

            // Set the class value.
            InProcServerKey.SetValue("Class", type.FullName);

            // Set the assembly value.
            InProcServerKey.SetValue("Assembly", strAsmName);

            // Set the runtime version value.
            InProcServerKey.SetValue("RuntimeVersion", strRuntimeVersion);

            // Set the assembly code base value if a code base was specified.
            if (strAsmCodeBase != null)
            {
                InProcServerKey.SetValue("CodeBase", strAsmCodeBase);
            }

            // Create the HKEY_CLASS_ROOT\CLSID\<CLSID>\InprocServer32\<Version> subkey
            RegistryKey VersionSubKey = InProcServerKey.CreateSubKey(strAsmVersion);

            VersionSubKey.SetValue("Class", type.FullName);
            VersionSubKey.SetValue("Assembly", strAsmName);
            VersionSubKey.SetValue("RuntimeVersion", strRuntimeVersion);
            if (strAsmCodeBase != null)
            {
                VersionSubKey.SetValue("CodeBase", strAsmCodeBase);
            }

            // Close the registry keys.
            VersionSubKey.Close();
            InProcServerKey.Close();
            ClsIdKey.Close();
            ClsIdRootKey.Close();
        }
Пример #9
0
        private void RegisterValueType(Type type, string strAsmName, string strAsmVersion, string strAsmCodeBase, string strRuntimeVersion)
        {
            string subkey = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";

            using (RegistryKey key = Registry.ClassesRoot.CreateSubKey("Record"))
            {
                using (RegistryKey key2 = key.CreateSubKey(subkey))
                {
                    using (RegistryKey key3 = key2.CreateSubKey(strAsmVersion))
                    {
                        key3.SetValue("Class", type.FullName);
                        key3.SetValue("Assembly", strAsmName);
                        key3.SetValue("RuntimeVersion", strRuntimeVersion);
                        if (strAsmCodeBase != null)
                        {
                            key3.SetValue("CodeBase", strAsmCodeBase);
                        }
                    }
                }
            }
        }
Пример #10
0
        private bool UnregisterManagedType(Type type, string strAsmVersion)
        {
            bool   flag          = true;
            string text          = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            string progIdForType = this.GetProgIdForType(type);

            using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey("CLSID", true))
            {
                if (registryKey != null)
                {
                    using (RegistryKey registryKey2 = registryKey.OpenSubKey(text, true))
                    {
                        if (registryKey2 != null)
                        {
                            using (RegistryKey registryKey3 = registryKey2.OpenSubKey("InprocServer32", true))
                            {
                                if (registryKey3 != null)
                                {
                                    using (RegistryKey registryKey4 = registryKey3.OpenSubKey(strAsmVersion, true))
                                    {
                                        if (registryKey4 != null)
                                        {
                                            registryKey4.DeleteValue("Assembly", false);
                                            registryKey4.DeleteValue("Class", false);
                                            registryKey4.DeleteValue("RuntimeVersion", false);
                                            registryKey4.DeleteValue("CodeBase", false);
                                            if (registryKey4.SubKeyCount == 0 && registryKey4.ValueCount == 0)
                                            {
                                                registryKey3.DeleteSubKey(strAsmVersion);
                                            }
                                        }
                                    }
                                    if (registryKey3.SubKeyCount != 0)
                                    {
                                        flag = false;
                                    }
                                    if (flag)
                                    {
                                        registryKey3.DeleteValue("", false);
                                        registryKey3.DeleteValue("ThreadingModel", false);
                                    }
                                    registryKey3.DeleteValue("Assembly", false);
                                    registryKey3.DeleteValue("Class", false);
                                    registryKey3.DeleteValue("RuntimeVersion", false);
                                    registryKey3.DeleteValue("CodeBase", false);
                                    if (registryKey3.SubKeyCount == 0 && registryKey3.ValueCount == 0)
                                    {
                                        registryKey2.DeleteSubKey("InprocServer32");
                                    }
                                }
                            }
                            if (flag)
                            {
                                registryKey2.DeleteValue("", false);
                                if (progIdForType != string.Empty)
                                {
                                    using (RegistryKey registryKey5 = registryKey2.OpenSubKey("ProgId", true))
                                    {
                                        if (registryKey5 != null)
                                        {
                                            registryKey5.DeleteValue("", false);
                                            if (registryKey5.SubKeyCount == 0 && registryKey5.ValueCount == 0)
                                            {
                                                registryKey2.DeleteSubKey("ProgId");
                                            }
                                        }
                                    }
                                }
                                using (RegistryKey registryKey6 = registryKey2.OpenSubKey("Implemented Categories", true))
                                {
                                    if (registryKey6 != null)
                                    {
                                        using (RegistryKey registryKey7 = registryKey6.OpenSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}", true))
                                        {
                                            if (registryKey7 != null && registryKey7.SubKeyCount == 0 && registryKey7.ValueCount == 0)
                                            {
                                                registryKey6.DeleteSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}");
                                            }
                                        }
                                        if (registryKey6.SubKeyCount == 0 && registryKey6.ValueCount == 0)
                                        {
                                            registryKey2.DeleteSubKey("Implemented Categories");
                                        }
                                    }
                                }
                            }
                            if (registryKey2.SubKeyCount == 0 && registryKey2.ValueCount == 0)
                            {
                                registryKey.DeleteSubKey(text);
                            }
                        }
                    }
                    if (registryKey.SubKeyCount == 0 && registryKey.ValueCount == 0)
                    {
                        Registry.ClassesRoot.DeleteSubKey("CLSID");
                    }
                }
                if (flag && progIdForType != string.Empty)
                {
                    using (RegistryKey registryKey8 = Registry.ClassesRoot.OpenSubKey(progIdForType, true))
                    {
                        if (registryKey8 != null)
                        {
                            registryKey8.DeleteValue("", false);
                            using (RegistryKey registryKey9 = registryKey8.OpenSubKey("CLSID", true))
                            {
                                if (registryKey9 != null)
                                {
                                    registryKey9.DeleteValue("", false);
                                    if (registryKey9.SubKeyCount == 0 && registryKey9.ValueCount == 0)
                                    {
                                        registryKey8.DeleteSubKey("CLSID");
                                    }
                                }
                            }
                            if (registryKey8.SubKeyCount == 0 && registryKey8.ValueCount == 0)
                            {
                                Registry.ClassesRoot.DeleteSubKey(progIdForType);
                            }
                        }
                    }
                }
            }
            return(flag);
        }
Пример #11
0
        private void CallUserDefinedRegistrationMethod(Type type, bool bRegister)
        {
            bool flag          = false;
            Type attributeType = !bRegister ? typeof(ComUnregisterFunctionAttribute) : typeof(ComRegisterFunctionAttribute);

            for (Type type1 = type; !flag && type1 != (Type)null; type1 = type1.BaseType)
            {
                MethodInfo[] methods = type1.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                int          length  = methods.Length;
                for (int index = 0; index < length; ++index)
                {
                    MethodInfo methodInfo = methods[index];
                    if (methodInfo.GetCustomAttributes(attributeType, true).Length != 0)
                    {
                        if (!methodInfo.IsStatic)
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComRegFunction", (object)methodInfo.Name, (object)type1.Name));
                            }
                            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComUnRegFunction", (object)methodInfo.Name, (object)type1.Name));
                        }
                        ParameterInfo[] parameters1 = methodInfo.GetParameters();
                        if (methodInfo.ReturnType != typeof(void) || parameters1 == null || parameters1.Length != 1 || parameters1[0].ParameterType != typeof(string) && parameters1[0].ParameterType != typeof(Type))
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComRegFunctionSig", (object)methodInfo.Name, (object)type1.Name));
                            }
                            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComUnRegFunctionSig", (object)methodInfo.Name, (object)type1.Name));
                        }
                        if (flag)
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComRegFunctions", (object)type1.Name));
                            }
                            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComUnRegFunctions", (object)type1.Name));
                        }
                        object[] parameters2 = new object[1] {
                            !(parameters1[0].ParameterType == typeof(string)) ? (object)type : (object)("HKEY_CLASSES_ROOT\\CLSID\\{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}")
                        };
                        methodInfo.Invoke((object)null, parameters2);
                        flag = true;
                    }
                }
            }
        }
Пример #12
0
        private void RegisterManagedType(Type type, string strAsmName, string strAsmVersion, string strAsmCodeBase, string strRuntimeVersion)
        {
            string value         = type.FullName ?? "";
            string text          = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            string progIdForType = this.GetProgIdForType(type);

            if (progIdForType != string.Empty)
            {
                using (RegistryKey registryKey = Registry.ClassesRoot.CreateSubKey(progIdForType))
                {
                    registryKey.SetValue("", value);
                    using (RegistryKey registryKey2 = registryKey.CreateSubKey("CLSID"))
                    {
                        registryKey2.SetValue("", text);
                    }
                }
            }
            using (RegistryKey registryKey3 = Registry.ClassesRoot.CreateSubKey("CLSID"))
            {
                using (RegistryKey registryKey4 = registryKey3.CreateSubKey(text))
                {
                    registryKey4.SetValue("", value);
                    using (RegistryKey registryKey5 = registryKey4.CreateSubKey("InprocServer32"))
                    {
                        registryKey5.SetValue("", "mscoree.dll");
                        registryKey5.SetValue("ThreadingModel", "Both");
                        registryKey5.SetValue("Class", type.FullName);
                        registryKey5.SetValue("Assembly", strAsmName);
                        registryKey5.SetValue("RuntimeVersion", strRuntimeVersion);
                        if (strAsmCodeBase != null)
                        {
                            registryKey5.SetValue("CodeBase", strAsmCodeBase);
                        }
                        using (RegistryKey registryKey6 = registryKey5.CreateSubKey(strAsmVersion))
                        {
                            registryKey6.SetValue("Class", type.FullName);
                            registryKey6.SetValue("Assembly", strAsmName);
                            registryKey6.SetValue("RuntimeVersion", strRuntimeVersion);
                            if (strAsmCodeBase != null)
                            {
                                registryKey6.SetValue("CodeBase", strAsmCodeBase);
                            }
                        }
                        if (progIdForType != string.Empty)
                        {
                            using (RegistryKey registryKey7 = registryKey4.CreateSubKey("ProgId"))
                            {
                                registryKey7.SetValue("", progIdForType);
                            }
                        }
                    }
                    using (RegistryKey registryKey8 = registryKey4.CreateSubKey("Implemented Categories"))
                    {
                        using (registryKey8.CreateSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}"))
                        {
                        }
                    }
                }
            }
            this.EnsureManagedCategoryExists();
        }
Пример #13
0
        private void CallUserDefinedRegistrationMethod(Type type, bool bRegister)
        {
            bool flag          = false;
            Type attributeType = null;

            if (bRegister)
            {
                attributeType = typeof(ComRegisterFunctionAttribute);
            }
            else
            {
                attributeType = typeof(ComUnregisterFunctionAttribute);
            }
            for (Type type3 = type; !flag && (type3 != null); type3 = type3.BaseType)
            {
                MethodInfo[] methods = type3.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
                int          length  = methods.Length;
                for (int i = 0; i < length; i++)
                {
                    MethodInfo info = methods[i];
                    if (info.GetCustomAttributes(attributeType, true).Length != 0)
                    {
                        if (!info.IsStatic)
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComRegFunction", new object[] { info.Name, type3.Name }));
                            }
                            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComUnRegFunction", new object[] { info.Name, type3.Name }));
                        }
                        ParameterInfo[] parameters = info.GetParameters();
                        if (((info.ReturnType != typeof(void)) || (parameters == null)) || ((parameters.Length != 1) || ((parameters[0].ParameterType != typeof(string)) && (parameters[0].ParameterType != typeof(Type)))))
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComRegFunctionSig", new object[] { info.Name, type3.Name }));
                            }
                            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComUnRegFunctionSig", new object[] { info.Name, type3.Name }));
                        }
                        if (flag)
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComRegFunctions", new object[] { type3.Name }));
                            }
                            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComUnRegFunctions", new object[] { type3.Name }));
                        }
                        object[] objArray = new object[1];
                        if (parameters[0].ParameterType == typeof(string))
                        {
                            objArray[0] = @"HKEY_CLASSES_ROOT\CLSID\{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
                        }
                        else
                        {
                            objArray[0] = type;
                        }
                        info.Invoke(null, objArray);
                        flag = true;
                    }
                }
            }
        }
        private void CallUserDefinedRegistrationMethod(Type type, bool bRegister)
        {
            bool bFunctionCalled = false;

            // Retrieve the attribute type to use to determine if a function is the requested user defined
            // registration function.
            Type RegFuncAttrType = null;

            if (bRegister)
            {
                RegFuncAttrType = typeof(ComRegisterFunctionAttribute);
            }
            else
            {
                RegFuncAttrType = typeof(ComUnregisterFunctionAttribute);
            }

            for (Type currType = type; !bFunctionCalled && currType != null; currType = currType.BaseType)
            {
                // Retrieve all the methods.
                MethodInfo[] aMethods   = currType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
                int          NumMethods = aMethods.Length;

                // Go through all the methods and check for the ComRegisterMethod custom attribute.
                for (int cMethods = 0; cMethods < NumMethods; cMethods++)
                {
                    MethodInfo CurrentMethod = aMethods[cMethods];

                    // Check to see if the method has the custom attribute.
                    if (CurrentMethod.GetCustomAttributes(RegFuncAttrType, true).Length != 0)
                    {
                        // Check to see if the method is static before we call it.
                        if (!CurrentMethod.IsStatic)
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(String.Format(Environment.GetResourceString("InvalidOperation_NonStaticComRegFunction"), CurrentMethod.Name, currType.Name));
                            }
                            else
                            {
                                throw new InvalidOperationException(String.Format(Environment.GetResourceString("InvalidOperation_NonStaticComUnRegFunction"), CurrentMethod.Name, currType.Name));
                            }
                        }

                        // Finally check that the signature is string ret void.
                        ParameterInfo[] aParams = CurrentMethod.GetParameters();
                        if (CurrentMethod.ReturnType != typeof(void) ||
                            aParams == null ||
                            aParams.Length != 1 ||
                            (aParams[0].ParameterType != typeof(String) && aParams[0].ParameterType != typeof(Type)))
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(String.Format(Environment.GetResourceString("InvalidOperation_InvalidComRegFunctionSig"), CurrentMethod.Name, currType.Name));
                            }
                            else
                            {
                                throw new InvalidOperationException(String.Format(Environment.GetResourceString("InvalidOperation_InvalidComUnRegFunctionSig"), CurrentMethod.Name, currType.Name));
                            }
                        }

                        // There can only be one register and one unregister function per type.
                        if (bFunctionCalled)
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(String.Format(Environment.GetResourceString("InvalidOperation_MultipleComRegFunctions"), currType.Name));
                            }
                            else
                            {
                                throw new InvalidOperationException(String.Format(Environment.GetResourceString("InvalidOperation_MultipleComUnRegFunctions"), currType.Name));
                            }
                        }

                        // The function is valid so set up the arguments to call it.
                        Object[] objs = new Object[1];
                        if (aParams[0].ParameterType == typeof(String))
                        {
                            // We are dealing with the string overload of the function.
                            objs[0] = "HKEY_CLASSES_ROOT\\CLSID\\{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
                        }
                        else
                        {
                            // We are dealing with the type overload of the function.
                            objs[0] = type;
                        }

                        // Invoke the COM register function.
                        CurrentMethod.Invoke(null, objs);

                        // Mark the function has having been called.
                        bFunctionCalled = true;
                    }
                }
            }
        }
        // UnregisterComImportedType
        // Return:
        //      true:      All version information are gone.
        //      false:     There are still some version left in registry
        private bool UnregisterComImportedType(Type type, String strAsmVersion)
        {
            // Try to open the <CLSID> key.
            String strClsId         = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            bool   bAllVersionsGone = true;

            RegistryKey ClsIdRootKey = Registry.ClassesRoot.OpenSubKey("CLSID", true);

            if (ClsIdRootKey != null)
            {
                RegistryKey ClsIdKey = ClsIdRootKey.OpenSubKey(strClsId, true);
                if (ClsIdKey != null)
                {
                    // Open the InProcServer32 key.
                    RegistryKey InProcServerKey = ClsIdKey.OpenSubKey("InprocServer32", true);
                    if (InProcServerKey != null)
                    {
                        // Delete the values we created.
                        InProcServerKey.DeleteValue("Assembly", false);
                        InProcServerKey.DeleteValue("Class", false);
                        InProcServerKey.DeleteValue("RuntimeVersion", false);
                        InProcServerKey.DeleteValue("CodeBase", false);

                        // Remove the entries in HKEY_CLASS_ROOT\CLSID\<CLSID>\<Version>
                        RegistryKey VersionSubKey = InProcServerKey.OpenSubKey(strAsmVersion, true);
                        if (VersionSubKey != null)
                        {
                            // Delete the value we created
                            VersionSubKey.DeleteValue("Assembly", false);
                            VersionSubKey.DeleteValue("Class", false);
                            VersionSubKey.DeleteValue("RuntimeVersion", false);
                            VersionSubKey.DeleteValue("CodeBase", false);

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

                        // If there are sub keys left then there are versions left.
                        if (InProcServerKey.SubKeyCount != 0)
                        {
                            bAllVersionsGone = false;
                        }

                        // If there are no other values or subkeys then we can delete the InProcServerKey.
                        if ((InProcServerKey.SubKeyCount == 0) && (InProcServerKey.ValueCount == 0))
                        {
                            ClsIdKey.DeleteSubKey("InprocServer32");
                        }

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

                    // If there are no other values or subkeys then we can delete the ClsIdKey.
                    if ((ClsIdKey.SubKeyCount == 0) && (ClsIdKey.ValueCount == 0))
                    {
                        Registry.ClassesRoot.OpenSubKey("CLSID", true).DeleteSubKey(strClsId);
                    }

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

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

            return(bAllVersionsGone);
        }
        // UnregisterManagedType
        //
        // Return :
        //      true:   All versions are gone.
        //      false:  Some versions are still left in registry
        private bool UnregisterManagedType(Type type, String strAsmVersion)
        {
            //
            // Create the CLSID string.
            //

            String strClsId         = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper() + "}";
            String strProgId        = GetProgIdForType(type);
            bool   bAllVersionsGone = true;


            //
            // Remove the entries under HKEY_CLASS_ROOT\CLSID\<CLSID> key.
            //

            RegistryKey ClsIdRootKey = Registry.ClassesRoot.OpenSubKey("CLSID", true);

            if (ClsIdRootKey != null)
            {
                RegistryKey ClsIdKey = ClsIdRootKey.OpenSubKey(strClsId, true);
                if (ClsIdKey != null)
                {
                    //
                    // Remove the entries in the HKEY_CLASS_ROOT\CLSID\<CLSID>\InprocServer32 key.
                    //

                    // Open the InProcServer32 key.
                    RegistryKey InProcServerKey = ClsIdKey.OpenSubKey("InprocServer32", true);
                    if (InProcServerKey != null)
                    {
                        //
                        // Remove the entries in HKEY_CLASS_ROOT\CLSID\<CLSID>\InprocServer32\<Version>
                        //
                        RegistryKey VersionSubKey = InProcServerKey.OpenSubKey(strAsmVersion, true);
                        if (VersionSubKey != null)
                        {
                            // Delete the values we created
                            VersionSubKey.DeleteValue("Assembly", false);
                            VersionSubKey.DeleteValue("Class", false);
                            VersionSubKey.DeleteValue("RuntimeVersion", false);
                            VersionSubKey.DeleteValue("CodeBase", false);

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

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

                        // If there are sub keys left then there are versions left.
                        if (InProcServerKey.SubKeyCount != 0)
                        {
                            bAllVersionsGone = false;
                        }

                        // If there are no versions left, then delete the threading model and default value.
                        if (bAllVersionsGone)
                        {
                            InProcServerKey.DeleteValue("", false);
                            InProcServerKey.DeleteValue("ThreadingModel", false);
                        }

                        InProcServerKey.DeleteValue("Assembly", false);
                        InProcServerKey.DeleteValue("Class", false);
                        InProcServerKey.DeleteValue("RuntimeVersion", false);
                        InProcServerKey.DeleteValue("CodeBase", false);

                        // If there are no other values or subkeys then we can delete the InProcServerKey.
                        if ((InProcServerKey.SubKeyCount == 0) && (InProcServerKey.ValueCount == 0))
                        {
                            ClsIdKey.DeleteSubKey("InprocServer32");
                        }

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

                    // remove HKEY_CLASS_ROOT\CLSID\<CLSID>\ProgId
                    // and HKEY_CLASS_ROOT\CLSID\<CLSID>\Implemented Category
                    // only when all versions are removed
                    if (bAllVersionsGone)
                    {
                        // Delete the value we created.
                        ClsIdKey.DeleteValue("", false);

                        if (strProgId != String.Empty)
                        {
                            //
                            // Remove the entries in the HKEY_CLASS_ROOT\CLSID\<CLSID>\ProgId key.
                            //

                            RegistryKey ProgIdKey = ClsIdKey.OpenSubKey("ProgId", true);
                            if (ProgIdKey != null)
                            {
                                // Delete the value we created.
                                ProgIdKey.DeleteValue("", false);

                                // If there are no other values or subkeys then we can delete the ProgIdSubKey.
                                if ((ProgIdKey.SubKeyCount == 0) && (ProgIdKey.ValueCount == 0))
                                {
                                    ClsIdKey.DeleteSubKey("ProgId");
                                }

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


                        //
                        // Remove entries in the  HKEY_CLASS_ROOT\CLSID\<CLSID>\Implemented Categories\<Managed Category Guid> key.
                        //

                        RegistryKey CategoryKey = ClsIdKey.OpenSubKey("Implemented Categories", true);
                        if (CategoryKey != null)
                        {
                            RegistryKey ManagedCategoryKey = CategoryKey.OpenSubKey(strManagedCategoryGuid, true);
                            if (ManagedCategoryKey != null)
                            {
                                // If there are no other values or subkeys then we can delete the ManagedCategoryKey.
                                if ((ManagedCategoryKey.SubKeyCount == 0) && (ManagedCategoryKey.ValueCount == 0))
                                {
                                    CategoryKey.DeleteSubKey(strManagedCategoryGuid);
                                }

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

                            // If there are no other values or subkeys then we can delete the CategoryKey.
                            if ((CategoryKey.SubKeyCount == 0) && (CategoryKey.ValueCount == 0))
                            {
                                ClsIdKey.DeleteSubKey("Implemented Categories");
                            }

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

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

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

                ClsIdRootKey.Close();
            }


            //
            // Remove the entries under HKEY_CLASS_ROOT\<wzProgId> key.
            //

            if (bAllVersionsGone)
            {
                if (strProgId != String.Empty)
                {
                    RegistryKey TypeNameKey = Registry.ClassesRoot.OpenSubKey(strProgId, true);
                    if (TypeNameKey != null)
                    {
                        // Delete the values we created.
                        TypeNameKey.DeleteValue("", false);


                        //
                        // Remove the entries in the HKEY_CLASS_ROOT\<wzProgId>\CLSID key.
                        //

                        RegistryKey ProgIdClsIdKey = TypeNameKey.OpenSubKey("CLSID", true);
                        if (ProgIdClsIdKey != null)
                        {
                            // Delete the values we created.
                            ProgIdClsIdKey.DeleteValue("", false);

                            // If there are no other values or subkeys then we can delete the ProgIdClsIdKey.
                            if ((ProgIdClsIdKey.SubKeyCount == 0) && (ProgIdClsIdKey.ValueCount == 0))
                            {
                                TypeNameKey.DeleteSubKey("CLSID");
                            }

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

                        // If there are no other values or subkeys then we can delete the TypeNameKey.
                        if ((TypeNameKey.SubKeyCount == 0) && (TypeNameKey.ValueCount == 0))
                        {
                            Registry.ClassesRoot.DeleteSubKey(strProgId);
                        }

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

            return(bAllVersionsGone);
        }
Пример #17
0
        private void CallUserDefinedRegistrationMethod(Type type, bool bRegister)
        {
            bool flag = false;
            Type typeFromHandle;

            if (bRegister)
            {
                typeFromHandle = typeof(ComRegisterFunctionAttribute);
            }
            else
            {
                typeFromHandle = typeof(ComUnregisterFunctionAttribute);
            }
            Type type2 = type;

            while (!flag && type2 != null)
            {
                MethodInfo[] methods = type2.GetMethods(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                int          num     = methods.Length;
                for (int i = 0; i < num; i++)
                {
                    MethodInfo methodInfo = methods[i];
                    if (methodInfo.GetCustomAttributes(typeFromHandle, true).Length != 0)
                    {
                        if (!methodInfo.IsStatic)
                        {
                            if (bRegister)
                            {
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComRegFunction", new object[]
                                {
                                    methodInfo.Name,
                                    type2.Name
                                }));
                            }
                            throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_NonStaticComUnRegFunction", new object[]
                            {
                                methodInfo.Name,
                                type2.Name
                            }));
                        }
                        else
                        {
                            ParameterInfo[] parameters = methodInfo.GetParameters();
                            if (methodInfo.ReturnType != typeof(void) || parameters == null || parameters.Length != 1 || (parameters[0].ParameterType != typeof(string) && parameters[0].ParameterType != typeof(Type)))
                            {
                                if (bRegister)
                                {
                                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComRegFunctionSig", new object[]
                                    {
                                        methodInfo.Name,
                                        type2.Name
                                    }));
                                }
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_InvalidComUnRegFunctionSig", new object[]
                                {
                                    methodInfo.Name,
                                    type2.Name
                                }));
                            }
                            else if (flag)
                            {
                                if (bRegister)
                                {
                                    throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComRegFunctions", new object[]
                                    {
                                        type2.Name
                                    }));
                                }
                                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_MultipleComUnRegFunctions", new object[]
                                {
                                    type2.Name
                                }));
                            }
                            else
                            {
                                object[] array = new object[1];
                                if (parameters[0].ParameterType == typeof(string))
                                {
                                    array[0] = "HKEY_CLASSES_ROOT\\CLSID\\{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
                                }
                                else
                                {
                                    array[0] = type;
                                }
                                methodInfo.Invoke(null, array);
                                flag = true;
                            }
                        }
                    }
                }
                type2 = type2.BaseType;
            }
        }
        private void RegisterManagedType(Type type, String strAsmName, String strAsmVersion, String strAsmCodeBase, String strRuntimeVersion)
        {
            //
            // Retrieve some information that will be used during the registration process.
            //

            String strDocString = strDocStringPrefix + type.FullName;
            String strClsId     = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            String strProgId    = GetProgIdForType(type);


            //
            // Write the actual type information in the registry.
            //

            if (strProgId != String.Empty)
            {
                // Create the HKEY_CLASS_ROOT\<wzProgId> key.
                RegistryKey TypeNameKey = Registry.ClassesRoot.CreateSubKey(strProgId);
                TypeNameKey.SetValue("", strDocString);

                // Create the HKEY_CLASS_ROOT\<wzProgId>\CLSID key.
                RegistryKey ProgIdClsIdKey = TypeNameKey.CreateSubKey("CLSID");
                ProgIdClsIdKey.SetValue("", strClsId);
                ProgIdClsIdKey.Close();

                // Close HKEY_CLASS_ROOT\<wzProgId> key.
                TypeNameKey.Close();
            }

            // Create the HKEY_CLASS_ROOT\CLSID\<CLSID> key.
            RegistryKey ClsIdRootKey = Registry.ClassesRoot.CreateSubKey("CLSID");
            RegistryKey ClsIdKey     = ClsIdRootKey.CreateSubKey(strClsId);

            ClsIdKey.SetValue("", strDocString);

            // Create the HKEY_CLASS_ROOT\CLSID\<CLSID>\InprocServer32 key.
            RegistryKey InProcServerKey = ClsIdKey.CreateSubKey("InprocServer32");

            InProcServerKey.SetValue("", strMsCorEEFileName);
            InProcServerKey.SetValue("ThreadingModel", strManagedTypeThreadingModel);
            InProcServerKey.SetValue("Class", type.FullName);
            InProcServerKey.SetValue("Assembly", strAsmName);
            InProcServerKey.SetValue("RuntimeVersion", strRuntimeVersion);
            if (strAsmCodeBase != null)
            {
                InProcServerKey.SetValue("CodeBase", strAsmCodeBase);
            }

            // Create the HKEY_CLASS_ROOT\CLSID\<CLSID>\InprocServer32\<Version> subkey
            RegistryKey VersionSubKey = InProcServerKey.CreateSubKey(strAsmVersion);

            VersionSubKey.SetValue("Class", type.FullName);
            VersionSubKey.SetValue("Assembly", strAsmName);
            VersionSubKey.SetValue("RuntimeVersion", strRuntimeVersion);
            if (strAsmCodeBase != null)
            {
                VersionSubKey.SetValue("CodeBase", strAsmCodeBase);
            }

            if (strProgId != String.Empty)
            {
                // Create the HKEY_CLASS_ROOT\CLSID\<CLSID>\ProdId key.
                RegistryKey ProgIdKey = ClsIdKey.CreateSubKey("ProgId");
                ProgIdKey.SetValue("", strProgId);
                ProgIdKey.Close();
            }

            // Create the HKEY_CLASS_ROOT\CLSID\<CLSID>\Implemented Categories\<Managed Category Guid> key.
            RegistryKey CategoryKey        = ClsIdKey.CreateSubKey("Implemented Categories");
            RegistryKey ManagedCategoryKey = CategoryKey.CreateSubKey(strManagedCategoryGuid);

            ManagedCategoryKey.Close();
            CategoryKey.Close();


            //
            // Ensure that the managed category exists.
            //

            EnsureManagedCategoryExists();


            //
            // Close the opened registry keys.
            //

            VersionSubKey.Close();
            InProcServerKey.Close();
            ClsIdKey.Close();
            ClsIdRootKey.Close();
        }
Пример #19
0
 public static void GenerateGuidForType()
 {
     Assert.Equal(typeof(int).GUID, Marshal.GenerateGuidForType(typeof(int)));
     Assert.Equal(typeof(string).GUID, Marshal.GenerateGuidForType(typeof(string)));
 }
        private bool UnregisterValueType(Type type, String strAsmVersion)
        {
            // Try to open the HKEY_CLASS_ROOT\Record key.
            String strRecordId      = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper(CultureInfo.InvariantCulture) + "}";
            bool   bAllVersionsGone = true;

            RegistryKey RecordRootKey = Registry.ClassesRoot.OpenSubKey(strRecordRootName, true);

            if (RecordRootKey != null)
            {
                // Open the HKEY_CLASS_ROOT\Record\{RecordId} key.
                RegistryKey RecordKey = RecordRootKey.OpenSubKey(strRecordId, true);
                if (RecordKey != null)
                {
                    // Delete the values we created.
                    RecordKey.DeleteValue("Assembly", false);
                    RecordKey.DeleteValue("Class", false);
                    RecordKey.DeleteValue("CodeBase", false);

                    RegistryKey VersionSubKey = RecordKey.OpenSubKey(strAsmVersion, true);
                    if (VersionSubKey != null)
                    {
                        // Delete the values we created.
                        VersionSubKey.DeleteValue("Assembly", false);
                        VersionSubKey.DeleteValue("Class", false);
                        VersionSubKey.DeleteValue("CodeBase", false);

                        // delete the version sub key if no value or subkeys under it
                        if ((VersionSubKey.SubKeyCount == 0) && (VersionSubKey.ValueCount == 0))
                        {
                            RecordKey.DeleteSubKey(strAsmVersion);
                        }

                        // close the key
                        VersionSubKey.Close();
                    }

                    // If there are sub keys left then there are versions left.
                    if (RecordKey.SubKeyCount != 0)
                    {
                        bAllVersionsGone = false;
                    }

                    // If there are no other values or subkeys then we can delete the HKEY_CLASS_ROOT\Record\{RecordId}.
                    if ((RecordKey.SubKeyCount == 0) && (RecordKey.ValueCount == 0))
                    {
                        RecordRootKey.DeleteSubKey(strRecordId);
                    }

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

                // If there are no other values or subkeys then we can delete the HKEY_CLASS_ROOT\Record.
                if ((RecordRootKey.SubKeyCount == 0) && (RecordRootKey.ValueCount == 0))
                {
                    Registry.ClassesRoot.DeleteSubKey(strRecordRootName);
                }

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

            return(bAllVersionsGone);
        }