public void GetComponentInfo(string assemblyPath, out string numComponents, out string componentInfo)
        {
            RegistrationServices services = new RegistrationServices();
            Assembly             assembly = this.LoadAssembly(assemblyPath);

            Type[] registrableTypesInAssembly = services.GetRegistrableTypesInAssembly(assembly);
            int    num = 0;
            string str = "";

            foreach (Type type in registrableTypesInAssembly)
            {
                if (type.IsClass && type.IsSubclassOf(typeof(ServicedComponent)))
                {
                    num++;
                    string str2 = Marshal.GenerateGuidForType(type).ToString();
                    string str3 = Marshal.GenerateProgIdForType(type);
                    if ((str2.Length == 0) || (str3.Length == 0))
                    {
                        throw new COMException();
                    }
                    string str4 = str;
                    str = str4 + str3 + ",{" + str2 + "},";
                }
            }
            numComponents = num.ToString(CultureInfo.InvariantCulture);
            componentInfo = str;
        }
Пример #2
0
        /// <summary>
        /// Registers the COM types in the specified assembly.
        /// </summary>
        public static List <System.Type> RegisterComTypes(string filePath)
        {
            // load assmebly.
            Assembly assembly = Assembly.LoadFrom(filePath);

            // check that the correct assembly is being registered.
            VerifyCodebase(assembly, filePath);

            RegistrationServices services = new RegistrationServices();

            // list types to register/unregister.
            List <System.Type> types = new List <System.Type>(services.GetRegistrableTypesInAssembly(assembly));

            // register types.
            if (types.Count > 0)
            {
                // unregister types first.
                if (!services.UnregisterAssembly(assembly))
                {
                    throw new ApplicationException("Unregister COM Types Failed.");
                }

                // register types.
                if (!services.RegisterAssembly(assembly, AssemblyRegistrationFlags.SetCodeBase))
                {
                    throw new ApplicationException("Register COM Types Failed.");
                }
            }

            return(types);
        }
Пример #3
0
        public void DaServerTest()
        {
            DaServer _daServer = new DaServer();

            Assert.IsNotNull(_daServer);
            Assert.IsTrue(File.Exists("CommServer_Main.log"), $"Cannot find the file in {Environment.CurrentDirectory}");
            Assert.IsFalse(Environment.Is64BitProcess);
            RegistrationServices _registrationServices = new RegistrationServices();
            //GetProgIdForType
            string _progId = _registrationServices.GetProgIdForType(typeof(DaServer));

            Assert.AreEqual <string>("CAS.CommServer.DA.Server.NETServer.DaServer", _progId);
            //GetRegistrableTypesInAssembly
            Type[] _typesToRegister = _registrationServices.GetRegistrableTypesInAssembly(typeof(DaServer).Assembly);
            Assert.IsNotNull(_typesToRegister);
            Assert.AreEqual <int>(1, _typesToRegister.Length);
            Assert.AreEqual <string>("CAS.CommServer.DA.Server.NETServer.DaServer", _typesToRegister[0].FullName);
            //TypeRepresentsComType
            Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(DaServer)));
            Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(IOPCCommon)));
            Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(IOPCCommon)));
            Assert.IsFalse(_registrationServices.TypeRepresentsComType(typeof(IOPCWrappedServer)));
            //TypeRequiresRegistration
            Assert.IsTrue(_registrationServices.TypeRequiresRegistration(typeof(DaServer)));
            object[] _attributes = typeof(DaServer).GetCustomAttributes(typeof(GuidAttribute), false);
            Assert.AreEqual <int>(1, _attributes.Length);
            Assert.AreEqual <string>("BE77A3C7-D2B7-44E7-B943-B978C1C87E5A", ((GuidAttribute)_attributes[0]).Value.ToUpper());
        }
        static bool PerformAssemblyRegistration(string strTargetAssemblyFilePath, bool bCodeBase)
        {
            try
            {
                RegistrationServices      registration_services = new RegistrationServices();
                Assembly                  assembly = Assembly.LoadFrom(strTargetAssemblyFilePath);
                AssemblyRegistrationFlags flags;

                bool bRet = false;

                if (bCodeBase == true)
                {
                    flags = AssemblyRegistrationFlags.SetCodeBase;
                }
                else
                {
                    flags = AssemblyRegistrationFlags.None;
                }

                bRet = registration_services.RegisterAssembly(assembly, flags);

                if (bRet)
                {
                    Console.WriteLine(string.Format("Successfully registered assembly [{0:S}].", strTargetAssemblyFilePath));

                    if (m_bVerbose)
                    {
                        Type[] types = registration_services.GetRegistrableTypesInAssembly(assembly);

                        Console.WriteLine(string.Format("Types Registered :"));

                        foreach (Type type in types)
                        {
                            Console.WriteLine(string.Format("GUID : [{0:S}] [{1:S}].", type.GUID.ToString(), type.FullName));
                        }
                    }
                }
                else
                {
                    Console.WriteLine(string.Format("Failed to register assembly [{0:S}].", strTargetAssemblyFilePath));
                }

                return(bRet);
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("An exception occurred. Exception description : [{0:S}].", ex.Message));
                return(false);
            }
        }
Пример #5
0
        /// <include file='doc\ImportUtil.uex' path='docs/doc[@for="ComManagedImportUtil.GetComponentInfo"]/*' />
        public void GetComponentInfo(string assemblyPath, out string numComponents, out string componentInfo)
        {
            RegistrationServices rs  = new RegistrationServices();
            Assembly             asm = LoadAssembly(assemblyPath);

            Type[] asmTypes = rs.GetRegistrableTypesInAssembly(asm);

            int    nComponents = 0;
            string s           = "";

            foreach (Type t in asmTypes)
            {
                if (t.IsClass && t.IsSubclassOf(typeof(ServicedComponent)))
                {
                    nComponents++;
                    string clsid  = Marshal.GenerateGuidForType(t).ToString();;
                    string progId = Marshal.GenerateProgIdForType(t);
                    s += progId + ",{" + clsid + "},";
                }
            }

            numComponents = nComponents.ToString();
            componentInfo = s;
        }
Пример #6
0
        /// <summary>
        /// Generates the registry file (.reg) asked for by the user via the
        /// <c>/regfile</c> command-line option.
        /// </summary>
        /// <returns>Exit code to return from the CLRegAsm tool.</returns>
        /// <exception cref="CoreException">User asked for codebase but provided assembly
        /// has no codebase.</exception>
        private int GenerateRegistryFile()
        {
            // This will be changed if an error occurs
            int exitCode = 0;

            // Get list of registrable types in the assembly.
            Type[] registrableTypes = regServices.GetRegistrableTypesInAssembly(assembly);

            // Check if assembly is a primary interop assembly.
            IList <CustomAttributeData> attribs = CustomAttributeData.GetCustomAttributes(assembly);
            bool assemblyIsPrimaryInterop       = false;

            foreach (CustomAttributeData attrib in attribs)
            {
                if (attrib.Constructor.DeclaringType == typeof(PrimaryInteropAssemblyAttribute))
                {
                    assemblyIsPrimaryInterop = true;
                    break;
                }
            }

            if (registrableTypes.Length != 0 || assemblyIsPrimaryInterop)
            {
                // Get codebase if needed.
                string codebase = null;
                if (prms.setCodeBase)
                {
                    codebase = assembly.CodeBase;
                    if (String.IsNullOrEmpty(codebase))
                    {
                        throw new CoreException(String.Format("Assembly \"{0}\" has no codebase; " +
                                                              "cannot include codebase in registry file.", prms.assemblyName));
                    }
                }

                // Get root key name depending on whether this is per-user or per-machine.
                string rootKeyName;
                if (prms.perUser)
                {
                    rootKeyName = String.Format(@"{0}\Software\Classes", Registry.CurrentUser.Name);
                }
                else
                {
                    rootKeyName = Registry.ClassesRoot.Name;
                }

                // Create regfile writer.
                Debug.Assert(!String.IsNullOrEmpty(prms.registryFile));
                using (RegistryFileWriter writer = new RegistryFileWriter(prms.registryFile)) {
                    // Write each registrable type to the file.
                    foreach (Type type in registrableTypes)
                    {
                        if (type.IsValueType)
                        {
                            // Value type, like struct.
                            WriteValueTypeInRegistryFile(type, codebase, rootKeyName, writer);
                        }
                        else if (regServices.TypeRepresentsComType(type))
                        {
                            // Com-imported type.
                            WriteComImportInRegistryFile(type, codebase, rootKeyName, writer);
                        }
                        else
                        {
                            // Class type.
                            WriteClassInRegistryFile(type, codebase, rootKeyName, writer);
                        }
                    }

                    // Write primary interop assembly info.
                    foreach (CustomAttributeData attrib in attribs)
                    {
                        if (attrib.Constructor.DeclaringType == typeof(PrimaryInteropAssemblyAttribute))
                        {
                            WritePrimaryInteropAssemblyInRegistryFile(attrib, codebase, rootKeyName, writer);
                            break;
                        }
                    }
                }

                // We're done, tell the user.
                console.WriteLine("Registry file \"{0}\" generated successfully.", prms.registryFile);
            }
            else if (!prms.silent)
            {
                // No registrable types and assembly isn't a primary interop assembly; can't continue.
                console.Error.WriteLine("Assembly \"{0}\" has no registrable types and is not a " +
                                        "primary interop assembly: cannot generate registry file.", prms.assemblyName);
                exitCode = 500;
            }

            return(exitCode);
        }