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()); }
private void WriteTypes(Stream s, Module m, Assembly asm, string strRuntimeVersion, RegistrationServices regServices, bool bGenerateTypeLib, int offset) { string fullName = null; string str2 = "{" + Marshal.GetTypeLibGuidForAssembly(asm).ToString().ToUpper() + "}"; foreach (Type type in m.GetTypes()) { if (regServices.TypeRequiresRegistration(type)) { string str3 = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper() + "}"; fullName = type.FullName; if (regServices.TypeRepresentsComType(type) || type.IsValueType) { this.WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); this.WriteUTFChars(s, " clsid=\"" + str3 + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " name=\"" + fullName + "\">" + Environment.NewLine, offset); this.WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } else { string str4 = Marshal.GenerateProgIdForType(type); this.WriteUTFChars(s, "<clrClass" + Environment.NewLine, offset); this.WriteUTFChars(s, " clsid=\"" + str3 + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " progid=\"" + str4 + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " threadingModel=\"Both\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " name=\"" + fullName + "\"" + Environment.NewLine, offset); this.WriteUTFChars(s, " runtimeVersion=\"" + strRuntimeVersion + "\">" + Environment.NewLine, offset); this.WriteUTFChars(s, "</clrClass>" + Environment.NewLine, offset); } } else if ((bGenerateTypeLib && type.IsInterface) && (type.IsPublic && !type.IsImport)) { string str5 = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpper() + "}"; this.WriteUTFChars(s, "<comInterfaceExternalProxyStub" + Environment.NewLine, offset); this.WriteUTFChars(s, "iid=\"" + str5 + "\"" + Environment.NewLine, offset + 4); this.WriteUTFChars(s, "name=\"" + type.Name + "\"" + Environment.NewLine, offset + 4); this.WriteUTFChars(s, string.Concat(new object[] { "numMethods=\"", type.GetMethods().Length, "\"", Environment.NewLine }), offset + 4); this.WriteUTFChars(s, "proxyStubClsid32=\"{00020424-0000-0000-C000-000000000046}\"" + Environment.NewLine, offset + 4); this.WriteUTFChars(s, "tlbid=\"" + str2 + "\" />" + Environment.NewLine, offset + 4); } } }
static void WriteTypes(Stream s, Type[] aTypes, int offset) { RegistrationServices regServices = new RegistrationServices(); String name = null; Assembly asm = Assembly.GetExecutingAssembly(); string asmver = asm.ImageRuntimeVersion; foreach (Type t in aTypes) { // only registrable managed types will show up in the manifest file if (!regServices.TypeRequiresRegistration(t)) { throw Fx.AssertAndThrow("User defined types must be registrable"); } String strClsId = "{" + Marshal.GenerateGuidForType(t).ToString().ToUpperInvariant() + "}"; name = t.FullName; // this type is a com imported type or Record if (regServices.TypeRepresentsComType(t) || t.IsValueType) { WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); // attribute clsid WriteUTFChars(s, " clsid=\"" + strClsId + "\"" + Environment.NewLine, offset); // attribute class WriteUTFChars(s, " name=\"" + name + "\"" + Environment.NewLine, offset); // clr version WriteUTFChars(s, " runtimeVersion=\"" + asmver + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } } }
private static void WriteTypes(Stream s, Type[] aTypes, int offset) { RegistrationServices services = new RegistrationServices(); string fullName = null; string imageRuntimeVersion = Assembly.GetExecutingAssembly().ImageRuntimeVersion; foreach (Type type in aTypes) { if (!services.TypeRequiresRegistration(type)) { throw Fx.AssertAndThrow("User defined types must be registrable"); } string str3 = "{" + Marshal.GenerateGuidForType(type).ToString().ToUpperInvariant() + "}"; fullName = type.FullName; if (services.TypeRepresentsComType(type) || type.IsValueType) { WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); WriteUTFChars(s, " clsid=\"" + str3 + "\"" + Environment.NewLine, offset); WriteUTFChars(s, " name=\"" + fullName + "\"" + Environment.NewLine, offset); WriteUTFChars(s, " runtimeVersion=\"" + imageRuntimeVersion + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } } }
// write all the types need to register in module m private static void WriteTypes(Stream s, Module m, Assembly asm, String strRuntimeVersion, RegistrationServices regServices, bool bGenerateTypeLib, int offset) { String name = null; AssemblyName asmName = asm.GetName(); String strTlbId = "{" + Marshal.GetTypeLibGuidForAssembly(asm).ToString().ToUpper() + "}"; // element comClass or interface Type[] aTypes = m.GetTypes(); foreach (Type t in aTypes) { // only registrable managed types will show up in the manifest file if (regServices.TypeRequiresRegistration(t)) { String strClsId = "{" + Marshal.GenerateGuidForType(t).ToString().ToUpper() + "}"; name = t.FullName; // this type is a com imported type or Record if (regServices.TypeRepresentsComType(t) || t.IsValueType) { WriteUTFChars(s, "<clrSurrogate" + Environment.NewLine, offset); // attribute clsid WriteUTFChars(s, " clsid=\"" + strClsId + "\"" + Environment.NewLine, offset); // attribute class WriteUTFChars(s, " name=\"" + name + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrSurrogate>" + Environment.NewLine, offset); } else // this is a managed type { String strProgId = Marshal.GenerateProgIdForType(t); WriteUTFChars(s, "<clrClass" + Environment.NewLine, offset); // attribute clsid WriteUTFChars(s, " clsid=\"" + strClsId + "\"" + Environment.NewLine, offset); // attribute progid WriteUTFChars(s, " progid=\"" + strProgId + "\"" + Environment.NewLine, offset); // attribute threadingModel WriteUTFChars(s, " threadingModel=\"Both\"" + Environment.NewLine, offset); // attribute class WriteUTFChars(s, " name=\"" + name + "\"" + Environment.NewLine, offset); // attribute runtimeVersion WriteUTFChars(s, " runtimeVersion=\"" + strRuntimeVersion + "\">" + Environment.NewLine, offset); WriteUTFChars(s, "</clrClass>" + Environment.NewLine, offset); } } else { // public, non-imported from COM's interface need to be in the manifest if (bGenerateTypeLib && t.IsInterface && t.IsPublic && !t.IsImport) { String strIID = "{" + Marshal.GenerateGuidForType(t).ToString().ToUpper() + "}"; WriteUTFChars(s, "<comInterfaceExternalProxyStub" + Environment.NewLine, offset); WriteUTFChars(s, "iid=\"" + strIID + "\"" + Environment.NewLine, offset + 4); // Should t.FullName be used here? // Seems t.Name matches what regasm does. WriteUTFChars(s, "name=\"" + t.Name + "\"" + Environment.NewLine, offset + 4); WriteUTFChars(s, "numMethods=\"" + t.GetMethods().Length + "\"" + Environment.NewLine, offset + 4); // Seems regasm puts this guid for every interface. // Correct it if this is not true WriteUTFChars(s, "proxyStubClsid32=\"{00020424-0000-0000-C000-000000000046}\"" + Environment.NewLine, offset + 4); WriteUTFChars(s, "tlbid=\"" + strTlbId + "\" />" + Environment.NewLine, offset + 4); } } } }
/// <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); }