internal static AddinClassInformations Create(Assembly addinAssembly, IEnumerable<object> assemblyAttributes, SingletonSettings.UnRegisterMode mode, Type addinClassType, IEnumerable<object> addinClassAttributes) { AssemblyName binaryHeader = addinAssembly.GetName(); string assemblyName = binaryHeader.Name; string assemblyVersion = binaryHeader.Version.ToString(); string assemblyCulture = CultureInfoConversion.ConvertToString(binaryHeader.CultureInfo); string assemblyToken = TokenConversion.ConvertToString(binaryHeader.GetPublicKeyToken()); string runtimeVersion = addinAssembly.ImageRuntimeVersion; string[] classesRoot = null; switch (mode) { case SingletonSettings.UnRegisterMode.Auto: classesRoot = new string[] { "HKEY_CLASSES_ROOT", "HKEY_CURRENT_USER\\Software\\Classes" }; break; case SingletonSettings.UnRegisterMode.System: classesRoot = new string[] { "HKEY_CLASSES_ROOT" }; break; case SingletonSettings.UnRegisterMode.User: classesRoot = new string[] { "HKEY_CURRENT_USER\\Software\\Classes" }; break; default: throw new IndexOutOfRangeException("mode"); } string progid = AttributeReflection.GetAttribute<ProgIdAttribute>(addinClassAttributes).Value; string fullClassName = addinClassType.FullName; string id = AttributeReflection.GetAttribute<GuidAttribute>(addinClassAttributes).Value; string codebase = addinAssembly.CodeBase; AddinClassInformations result = new AddinClassInformations( assemblyName, assemblyVersion, assemblyCulture, assemblyToken, runtimeVersion, classesRoot, progid, fullClassName, id, codebase); return result; }
internal RegFileOperationHostSettings(string assemblyPath, SingletonSettings.RegisterMode mode, bool codebase, string regFilePath, SingletonSettings.AddinRegMode addinRegMode, SingletonSettings.RegExportCall exportCall) { AssemblyPath = assemblyPath; Mode = mode; Codebase = codebase; RegFilePath = regFilePath; AddinRegMode = addinRegMode; ExportCall = exportCall; }
internal UnregisterOperationHostSettings(string assemblyPath, SingletonSettings.UnRegisterMode mode, bool doRegisterCall, SingletonSettings.SignCheckMode signCheck, SingletonSettings.AddinRegMode addinRegMode) { AssemblyPath = assemblyPath; Mode = mode; DoRegisterCall = doRegisterCall; SignCheck = signCheck; AddinRegMode = addinRegMode; }
internal void DeleteComponentKey(SingletonSettings.RegisterMode mode, string key) { RegistryKey rootKey; if (mode == SingletonSettings.RegisterMode.System) rootKey = Microsoft.Win32.Registry.ClassesRoot; else rootKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(_userBypass, true); rootKey.DeleteSubKeyTree(key, false); rootKey.Close(); }
internal void CreateComponentValue(SingletonSettings.RegisterMode mode, string key, string name, object value, RegistryValueKind kind) { RegistryKey componentKey; if (mode == SingletonSettings.RegisterMode.System) componentKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(key, true); else componentKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(_userBypass + key, true); if (null == componentKey) throw new ArgumentOutOfRangeException("Unable to find specified registry key."); CreateComponentValue(componentKey, name, value, kind); }
internal void DeleteComponentKey(SingletonSettings.UnRegisterMode mode, string key) { RegistryKey[] rootKeys = null; switch (mode) { case SingletonSettings.UnRegisterMode.Auto: rootKeys = new RegistryKey[2]; rootKeys[0] = Microsoft.Win32.Registry.ClassesRoot; rootKeys[1] = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(_userBypass, true); break; case SingletonSettings.UnRegisterMode.System: rootKeys = new RegistryKey[] { Microsoft.Win32.Registry.ClassesRoot }; break; case SingletonSettings.UnRegisterMode.User: rootKeys = new RegistryKey[] { Microsoft.Win32.Registry.CurrentUser.OpenSubKey(_userBypass, true) }; break; default: throw new IndexOutOfRangeException("mode"); } foreach (RegistryKey item in rootKeys) { try { item.DeleteSubKeyTree(key, false); item.Close(); } catch (System.Security.SecurityException) { if (mode != SingletonSettings.UnRegisterMode.Auto) throw; } catch (System.UnauthorizedAccessException) { if (mode != SingletonSettings.UnRegisterMode.Auto) throw; } catch (Exception) { throw; } } }
internal RegistryKey CreateComponentKey(SingletonSettings.RegisterMode mode, params string[] key) { RegistryKey rootKey; if (mode == SingletonSettings.RegisterMode.System) rootKey = Microsoft.Win32.Registry.ClassesRoot; else rootKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(_userBypass, true); bool isFirst = true; string keys = String.Empty; foreach (string item in key) { if (!isFirst) keys += "\\"; if (isFirst) isFirst = false; keys += item; } RegistryKey result = rootKey.CreateSubKey(keys); rootKey.Close(); return result; }
private string CreateRegistryFileContent(Assembly addinAssembly, IEnumerable<object> assemblyAttributes, SingletonSettings.RegisterMode mode, Type addinClassType, IEnumerable<object> addinClassAttributes) { AddinClassInformations addinClass = AddinClassInformations.Create( addinAssembly, assemblyAttributes, mode, addinClassType, addinClassAttributes); StringBuilder content = new StringBuilder(); content.AppendLine(String.Format("[{0}\\{1}]", addinClass.ClassesRoot, addinClass.ProgId)); content.AppendLine(String.Format("@=\"{0}\"", addinClass.FullClassName)); content.AppendLine(String.Empty); content.AppendLine(String.Format("[{0}\\{1}\\CLSID]", addinClass.ClassesRoot, addinClass.ProgId)); content.AppendLine("@=\"{" + addinClass.Id.ToString() + "}\""); content.AppendLine(String.Empty); content.AppendLine("[" + addinClass.ClassesRoot + "\\CLSID\\{" + addinClass.Id + "}]"); content.AppendLine(String.Format("@=\"{0}\"", addinClass.FullClassName)); content.AppendLine(String.Empty); content.AppendLine("[" + addinClass.ClassesRoot + "\\CLSID\\{" + addinClass.Id + "}\\InprocServer32]"); content.AppendLine("@=\"mscoree.dll\""); content.AppendLine("\"ThreadingModel\"=\"Both\""); content.AppendLine(String.Format("\"Class\"=\"{0}\"", addinClass.FullClassName)); content.AppendLine(String.Format("\"Assembly\"=\"{0}, Version={1}, Culture={2}, PublicKeyToken={3}\"", addinClass.AssemblyName, addinClass.AssemblyVersion, addinClass.AssemblyCulture, addinClass.AssemblyToken)); content.AppendLine(String.Format("\"RuntimeVersion\"=\"{0}\"", addinClass.RuntimeVersion)); if (Settings.Codebase) content.AppendLine(String.Format("\"Codebase\"=\"{0}\"", addinClass.Codebase)); content.AppendLine(String.Empty); content.AppendLine("[" + addinClass.ClassesRoot + "\\CLSID\\{" + addinClass.Id + "}\\InprocServer32\\" + addinClass.AssemblyVersion + "]"); content.AppendLine(String.Format("\"Class\"=\"{0}\"", addinClass.FullClassName)); content.AppendLine(String.Format("\"Assembly\"=\"{0}, Version={1}, Culture={2}, PublicKeyToken={3}\"", addinClass.AssemblyName, addinClass.AssemblyVersion, addinClass.AssemblyCulture, addinClass.AssemblyToken)); content.AppendLine(String.Format("\"RuntimeVersion\"=\"{0}\"", addinClass.RuntimeVersion)); if (Settings.Codebase) content.AppendLine(String.Format("\"Codebase\"=\"{0}\"", addinAssembly.CodeBase)); content.AppendLine(String.Empty); content.AppendLine("[" + addinClass.ClassesRoot + "\\CLSID\\{" + addinClass.Id + "}\\ProgId]"); content.AppendLine(String.Format("@=\"{0}\"", addinClass.ProgId)); content.AppendLine(String.Empty); content.AppendLine("[" + addinClass.ClassesRoot + "\\CLSID\\{" + addinClass.Id + "}\\Implemented Categories\\{" + addinClass.ComponentCategoryId.ToString() + "}]"); if (Settings.AddinRegMode == SingletonSettings.AddinRegMode.On) { Dictionary<object, Type> attributeTypes = GetAttributeTypes(addinClassType.GetCustomAttributes(true)); bool isMultiAddin = AddinRegAnalyzer.IsMultiAddin(addinClassType); KeyValuePair<object, Type> multi = AddinRegAnalyzer.GetMultiRegisterAttribute(attributeTypes); KeyValuePair<object, Type> comAddin = AddinRegAnalyzer.GetComAddinAttribute(attributeTypes); if (null != comAddin.Key) { string name = (string)comAddin.Value.InvokeMember("Name", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField, null, comAddin.Key, new object[0]); string description = (string)comAddin.Value.InvokeMember("Description", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField, null, comAddin.Key, new object[0]); int loadBehavior = (int)comAddin.Value.InvokeMember("LoadBehavior", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField, null, comAddin.Key, new object[0]); int commandLineSafe = (int)comAddin.Value.InvokeMember("CommandLineSafe", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField, null, comAddin.Key, new object[0]); if (true == isMultiAddin && multi.Key != null) { IEnumerable products = multi.Value.InvokeMember("Products", BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetField, null, multi.Key, new string[0]) as IEnumerable; if (null != products) { foreach (object item in products) { int productIndex = Convert.ToInt32(item); CreateOfficeRegistryKey(content, _multiRegisterIn[productIndex], addinClass.ProgId, name, description, loadBehavior, commandLineSafe, Settings.Mode == SingletonSettings.RegisterMode.System); } } } else if (false == isMultiAddin) { string key = GetKeyName(addinClassType); CreateOfficeRegistryKey(content, key, addinClass.ProgId, name, description, loadBehavior, commandLineSafe, Settings.Mode == SingletonSettings.RegisterMode.System); } } } return content.ToString(); }
private void CreateRegistryEntries(Assembly addinAssembly, IEnumerable<object> assemblyAttributes, SingletonSettings.RegisterMode mode, Type addinClassType, IEnumerable<object> addinClassAttributes) { AddinClassInformations addinClass = AddinClassInformations.Create( addinAssembly, assemblyAttributes, mode, addinClassType, addinClassAttributes); Registry registry = new Registry(); Microsoft.Win32.RegistryKey key = registry.CreateComponentKey(Settings.Mode, addinClass.ProgId); registry.CreateComponentValue(key, "", addinClass.FullClassName, Microsoft.Win32.RegistryValueKind.String); key.Close(); key = registry.CreateComponentKey(Settings.Mode, String.Format("{0}\\CLSID", addinClass.ProgId)); registry.CreateComponentValue(key, "", "{" + addinClass.Id.ToString() + "}", Microsoft.Win32.RegistryValueKind.String); key.Close(); key = registry.CreateComponentKey(Settings.Mode, "CLSID\\{" + addinClass.Id + "}"); registry.CreateComponentValue(key, "", addinClass.FullClassName, Microsoft.Win32.RegistryValueKind.String); key.Close(); key = registry.CreateComponentKey(Settings.Mode, "CLSID\\{" + addinClass.Id + "}\\InprocServer32"); registry.CreateComponentValue(key, "", "mscoree.dll", Microsoft.Win32.RegistryValueKind.String); registry.CreateComponentValue(key, "ThreadingModel", "Both", Microsoft.Win32.RegistryValueKind.String); registry.CreateComponentValue(key, "Class", addinClass.FullClassName, Microsoft.Win32.RegistryValueKind.String); registry.CreateComponentValue(key, "Assembly", String.Format("{0}, Version={1}, Culture={2}, PublicKeyToken={3}", addinClass.AssemblyName, addinClass.AssemblyVersion, addinClass.AssemblyCulture, addinClass.AssemblyToken), Microsoft.Win32.RegistryValueKind.String); registry.CreateComponentValue(key, "RuntimeVersion", addinClass.RuntimeVersion, Microsoft.Win32.RegistryValueKind.String); if (Settings.Codebase) registry.CreateComponentValue(key, "Codebase", addinClass.Codebase, Microsoft.Win32.RegistryValueKind.String); key.Close(); key = registry.CreateComponentKey(Settings.Mode, "CLSID\\{" + addinClass.Id + "}\\InprocServer32\\" + addinClass.AssemblyVersion); registry.CreateComponentValue(key, "Class", addinClass.FullClassName, Microsoft.Win32.RegistryValueKind.String); registry.CreateComponentValue(key, "Assembly", String.Format("{0}, Version={1}, Culture={2}, PublicKeyToken={3}", addinClass.AssemblyName, addinClass.AssemblyVersion, addinClass.AssemblyCulture, addinClass.AssemblyToken), Microsoft.Win32.RegistryValueKind.String); registry.CreateComponentValue(key, "RuntimeVersion", addinClass.RuntimeVersion, Microsoft.Win32.RegistryValueKind.String); if (Settings.Codebase) registry.CreateComponentValue(key, "Codebase", addinClass.Codebase, Microsoft.Win32.RegistryValueKind.String); key.Close(); key = registry.CreateComponentKey(Settings.Mode, String.Format("CLSID\\{0}\\{1}","{" + addinClass.Id + "}", "ProgId")); registry.CreateComponentValue(key, "", addinClass.ProgId, Microsoft.Win32.RegistryValueKind.String); key.Close(); key = registry.CreateComponentKey(Settings.Mode, String.Format("CLSID\\{0}\\Implemented Categories\\{1}", "{" + addinClass.Id + "}", "{" + addinClass.ComponentCategoryId +"}")); key.Close(); }
private void DeleteRegistryEntries(Assembly addinAssembly, IEnumerable<object> assemblyAttributes, SingletonSettings.UnRegisterMode mode, Type addinClassType, IEnumerable<object> addinClassAttributes) { AddinClassInformations addinClass = AddinClassInformations.Create( addinAssembly, assemblyAttributes, mode, addinClassType, addinClassAttributes); Registry registry = new Registry(); registry.DeleteComponentKey(Settings.Mode, addinClass.ProgId); registry.DeleteComponentKey(Settings.Mode, "CLSID\\{" + addinClass.Id + "}"); }