// // Remove all traces of this from the registry. // // **TODO** If the above does AppID/DCOM stuff, this would have // to remove that stuff too. // private static void UnregisterObjects(DriverDiscovery drivers) { if (!IsAdministrator) { ElevateSelf("/unregister"); return; } // // Local server's DCOM/AppID information // Registry.ClassesRoot.DeleteSubKey(string.Format("APPID\\{0}", s_appId), false); Registry.ClassesRoot.DeleteSubKey(string.Format("APPID\\{0}", Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1)), false); // // For each of the driver assemblies // foreach (var type in drivers.DiscoveredTypes) { var clsid = Marshal.GenerateGuidForType(type).ToString("B"); var progid = Marshal.GenerateProgIdForType(type); var deviceType = type.Name; // // Best efforts // // // HKCR\progid // Registry.ClassesRoot.DeleteSubKey(string.Format("{0}\\CLSID", progid), false); Registry.ClassesRoot.DeleteSubKey(progid, false); // // HKCR\CLSID\clsid // Registry.ClassesRoot.DeleteSubKey( string.Format("CLSID\\{0}\\Implemented Categories\\{{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}}", clsid), false); Registry.ClassesRoot.DeleteSubKey(string.Format("CLSID\\{0}\\Implemented Categories", clsid), false); Registry.ClassesRoot.DeleteSubKey(string.Format("CLSID\\{0}\\ProgId", clsid), false); Registry.ClassesRoot.DeleteSubKey(string.Format("CLSID\\{0}\\LocalServer32", clsid), false); Registry.ClassesRoot.DeleteSubKey(string.Format("CLSID\\{0}\\Programmable", clsid), false); Registry.ClassesRoot.DeleteSubKey(string.Format("CLSID\\{0}", clsid), false); try { // // ASCOM // using (var P = new Profile()) { P.DeviceType = deviceType; P.Unregister(progid); } } catch (Exception) { } } }
// // ProcessArguments() will process the command-line arguments // If the return value is true, we carry on and start this application. // If the return value is false, we terminate this application immediately. // private static bool ProcessArguments(DriverDiscovery drivers, string[] args) { var bRet = true; // //**TODO** -Embedding is "ActiveX start". Prohibit non_AX starting? // if (args.Length > 0) { switch (args[0].ToLower()) { case "-embedding": StartedByCOM = true; // Indicate COM started us break; case "-register": case @"/register": case "-regserver": // Emulate VB6 case @"/regserver": RegisterObjects(drivers); // Register each served object bRet = false; break; case "-unregister": case @"/unregister": case "-unregserver": // Emulate VB6 case @"/unregserver": UnregisterObjects(drivers); //Unregister each served object bRet = false; break; default: MessageBox.Show( "Unknown argument: " + args[0] + "\nValid are : -register, -unregister and -embedding", "ASCOM driver for Arduino Power Controller", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); break; } } else { StartedByCOM = false; } return(bRet); }
// // Register the class factories of the COM objects (drivers) // that we serve. This requires the class factory name to be // equal to the served class name + "ClassFactory". // private static IEnumerable <ClassFactory> RegisterClassFactories(DriverDiscovery drivers, ILog log) { var registeredFactories = new List <ClassFactory>(); foreach (var type in drivers.DiscoveredTypes) { var factory = new ClassFactory(type); // Use default context & flags if (factory.RegisterClassObject()) { registeredFactories.Add(factory); } else { // This will terminate the application log.Fatal() .Message("Failed to register class factory for {type}", type.Name) .Write(); } } ClassFactory.ResumeClassObjects(); // Served objects now go live return(registeredFactories); }
/// <summary>Registers the discovered driver types for COM and with the ASCOM Profile Store.</summary> /// <param name="drivers">The discovered drivers.</param> /// <remarks> /// Registers each discovered driver type with COM so that applications can find it and load it by /// ProgID (i.e. via the ASCOM Chooser). Also adds DCOM info for the LocalServer itself, so it can /// be activated via an outbound connection from TheSky. /// </remarks> private static void RegisterObjects(DriverDiscovery drivers) { if (!IsAdministrator) { ElevateSelf("/register"); return; } // // If reached here, we're running elevated // var assy = Assembly.GetExecutingAssembly(); var attr = Attribute.GetCustomAttribute(assy, typeof(AssemblyTitleAttribute)); var assyTitle = ((AssemblyTitleAttribute)attr).Title; attr = Attribute.GetCustomAttribute(assy, typeof(AssemblyDescriptionAttribute)); var assyDescription = ((AssemblyDescriptionAttribute)attr).Description; // // Local server's DCOM/AppID information // try { // // HKCR\APPID\appid // using (var key = Registry.ClassesRoot.CreateSubKey("APPID\\" + s_appId)) { key.SetValue(null, assyDescription); key.SetValue("AppID", s_appId); key.SetValue("AuthenticationLevel", 1, RegistryValueKind.DWord); } // // HKCR\APPID\exename.ext // using (var key = Registry.ClassesRoot.CreateSubKey(string.Format("APPID\\{0}", Application.ExecutablePath.Substring(Application.ExecutablePath.LastIndexOf('\\') + 1)))) { key.SetValue("AppID", s_appId); } } catch (Exception ex) { MessageBox.Show("Error while registering the server:\n" + ex, SharedResources.DomeDriverId, MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } // // For each of the driver assemblies // foreach (var type in drivers.DiscoveredTypes) { var bFail = false; try { // // HKCR\CLSID\clsid // var clsid = Marshal.GenerateGuidForType(type).ToString("B"); var progid = Marshal.GenerateProgIdForType(type); //PWGS Generate device type from the Class name var deviceType = type.Name; using (var key = Registry.ClassesRoot.CreateSubKey(string.Format("CLSID\\{0}", clsid))) { key.SetValue(null, progid); // Could be assyTitle/Desc??, but .NET components show ProgId here key.SetValue("AppId", s_appId); using (var key2 = key.CreateSubKey("Implemented Categories")) { key2.CreateSubKey("{62C8FE65-4EBB-45e7-B440-6E39B2CDBF29}"); } using (var key2 = key.CreateSubKey("ProgId")) { key2.SetValue(null, progid); } key.CreateSubKey("Programmable"); using (var key2 = key.CreateSubKey("LocalServer32")) { key2.SetValue(null, Application.ExecutablePath); } } // // HKCR\progid // using (var key = Registry.ClassesRoot.CreateSubKey(progid)) { key.SetValue(null, assyTitle); using (var key2 = key.CreateSubKey("CLSID")) { key2.SetValue(null, clsid); } } // // ASCOM // assy = type.Assembly; // Pull the display name from the ServedClassName attribute. attr = Attribute.GetCustomAttribute(type, typeof(ServedClassNameAttribute)); //PWGS Changed to search type for attribute rather than assembly var chooserName = ((ServedClassNameAttribute)attr).DisplayName ?? "MultiServer"; using (var P = new Profile()) { P.DeviceType = deviceType; P.Register(progid, chooserName); } } catch (Exception ex) { MessageBox.Show("Error while registering the server:\n" + ex, SharedResources.DomeDriverId, MessageBoxButtons.OK, MessageBoxIcon.Stop); bFail = true; } if (bFail) { break; } } }