SetProgId() private method

private SetProgId ( string progId ) : void
progId string
return void
Ejemplo n.º 1
0
        public static void LoadComAddIn(ExcelComAddIn addIn)
        {
            // If we are called without the addIn's DnaLibrary being set, default to the current library
            if (addIn.DnaLibrary == null)
            {
                addIn.DnaLibrary = DnaLibrary.CurrentLibrary;
            }

            Guid   clsId;
            string progId;

            // If we have both an explicit Guid and an explicit ProgId, then use those, else create synthetic ProgId.
            object[] progIdAttrs = addIn.GetType().GetCustomAttributes(typeof(ProgIdAttribute), false);
            object[] guidAttrs   = addIn.GetType().GetCustomAttributes(typeof(GuidAttribute), false);
            if (progIdAttrs.Length >= 1 && guidAttrs.Length >= 1)
            {
                // Use the attributes
                ProgIdAttribute progIdAtt = (ProgIdAttribute)progIdAttrs[0];
                progId = progIdAtt.Value;

                GuidAttribute guidAtt = (GuidAttribute)guidAttrs[0];
                clsId = new Guid(guidAtt.Value);
            }
            else
            {
                // Use a stable Guid derived from the Xll Path (since Excel stores load-times and other info for every COM add-in loaded in the registry)
                clsId = ExcelDnaUtil.XllGuid;
                // and make the ProgId from this Guid - max 39 chars....
                progId = "Dna." + clsId.ToString("N") + "." + loadedComAddIns.Count;
            }
            addIn.SetProgId(progId);

            // Put together some nicer descriptions for the Add-ins dialog.
            string friendlyName;

            if (addIn is ExcelRibbon)
            {
                friendlyName = addIn.DnaLibrary.Name; // + " (Ribbon Helper)"; (No more - it is displayed in the Ribbon tooltip!)
            }
            else if (addIn is ExcelCustomTaskPaneAddIn)
            {
                friendlyName = addIn.DnaLibrary.Name + " (Custom Task Pane Helper)";
            }
            else
            {
                friendlyName = addIn.DnaLibrary.Name + " (COM Add-in Helper)";
            }
            string description = string.Format("Dynamically created COM Add-in to load custom UI for the Excel Add-in {0}, located at {1}.", addIn.DnaLibrary.Name, DnaLibrary.XllPath);


            Logger.ComAddIn.Verbose("Getting Application object");
            object app     = ExcelDnaUtil.Application;
            Type   appType = app.GetType();

            Logger.ComAddIn.Verbose("Got Application object: " + app.GetType().ToString());

            CultureInfo ci = new CultureInfo(1033);
            object      excelComAddIns;
            object      comAddIn;

            try
            {
                Logger.ComAddIn.Verbose("Loading Ribbon/COM Add-In {0} ({1}) {2} / {3}",
                                        addIn.GetType().FullName, friendlyName, progId, clsId);

                using (new SingletonClassFactoryRegistration(addIn, clsId))
                    using (new ProgIdRegistration(progId, clsId))
                        using (new ClsIdRegistration(clsId, progId))
                            using (new ComAddInRegistration(progId, friendlyName, description))
                                using (new AutomationSecurityOverride(app))
                                {
                                    excelComAddIns = appType.InvokeMember("COMAddIns", BindingFlags.GetProperty, null, app, null, ci);
                                    //                            Debug.Print("Got COMAddins object: " + excelComAddIns.GetType().ToString());
                                    appType.InvokeMember("Update", BindingFlags.InvokeMethod, null, excelComAddIns, null, ci);
                                    //                            Debug.Print("Updated COMAddins object with AddIn registered");
                                    comAddIn = excelComAddIns.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, excelComAddIns, new object[] { progId }, ci);
                                    //                            Debug.Print("Got the COMAddin object: " + comAddIn.GetType().ToString());

                                    // At this point Excel knows how to load our add-in by CLSID, so we could clean up the
                                    // registry aggressively, before the actual (dangerous?) loading starts.
                                    // But this seems to lead to some distress - Excel has some assertion checked when
                                    // it updates the LoadBehavior after a successful load....
                                    comAddIn.GetType().InvokeMember("Connect", BindingFlags.SetProperty, null, comAddIn, new object[] { true }, ci);
                                    //                            Debug.Print("COMAddin is loaded.");
                                    loadedComAddIns.Add(comAddIn);

                                    Logger.ComAddIn.Verbose("Completed Loading Ribbon/COM Add-In");
                                }
            }
            catch (UnauthorizedAccessException secex)
            {
                Logger.ComAddIn.Error(secex, "The Ribbon/COM Add-In helper required by add-in {0} could not be registered.\r\nThis may be due to restricted permissions on the HKCU\\Software\\Classes key", DnaLibrary.CurrentLibrary.Name);
            }
            catch (Exception ex)
            {
                // If Excel is running with the /K switch it seems to indicate we're running
                // in a COM-unfriendly mode where (sometimes) the COM add-in for the ribbon won't load.
                // We skip the log display in this case.
                // CONSIDER: How would an add-in know that its COM AddIn load failed in this case?
                if (Environment.CommandLine.Contains(" /K"))
                {
                    Logger.ComAddIn.Info("Load Ribbon/COM Add-In exception with /K in CommandLine \r\n{0}", ex.ToString());
                }
                else
                {
                    Logger.ComAddIn.Error(ex, "The Ribbon/COM add-in helper required by add-in {0} could not be registered.\r\nThis may be due to the helper add-in being disabled by Excel.\r\nTo repair, open Disabled items in the Options->Add-Ins page and re-enable target add-in, then restart Excel.\r\n\r\nError details: {1}", DnaLibrary.CurrentLibrary.Name, ex.ToString());
                }
            }
        }
Ejemplo n.º 2
0
        public static void LoadComAddIn(ExcelComAddIn addIn)
        {
            // If we are called without the addIn's DnaLibrary being set, default to the current library
            if (addIn.DnaLibrary == null)
            {
                addIn.DnaLibrary = DnaLibrary.CurrentLibrary;
            }

            Guid   clsId;
            string progId;

            // If we have both an explicit Guid and an explicit ProgId, then use those, else create synthetic ProgId.
            object[] progIdAttrs = addIn.GetType().GetCustomAttributes(typeof(ProgIdAttribute), false);
            object[] guidAttrs   = addIn.GetType().GetCustomAttributes(typeof(GuidAttribute), false);
            if (progIdAttrs.Length >= 1 && guidAttrs.Length >= 1)
            {
                // Use the attributes
                ProgIdAttribute progIdAtt = (ProgIdAttribute)progIdAttrs[0];
                progId = progIdAtt.Value;

                GuidAttribute guidAtt = (GuidAttribute)guidAttrs[0];
                clsId = new Guid(guidAtt.Value);
            }
            else
            {
                // Use a stable Guid derived from the Xll Path (since Excel stores load-times and other info for every COM add-in loaded in the registry)
                clsId = ExcelDnaUtil.XllGuid;
                // and make the ProgId from this Guid - max 39 chars....
                progId = "Dna." + clsId.ToString("N") + "." + loadedComAddIns.Count;
            }
            addIn.SetProgId(progId);

            // Put together some nicer descriptions for the Add-ins dialog.
            string friendlyName;

            if (addIn is ExcelRibbon)
            {
                friendlyName = addIn.DnaLibrary.Name; // + " (Ribbon Helper)"; (No more - it is displayed in the Ribbon tooltip!)
            }
            else if (addIn is ExcelCustomTaskPaneAddIn)
            {
                friendlyName = addIn.DnaLibrary.Name + " (Custom Task Pane Helper)";
            }
            else
            {
                friendlyName = addIn.DnaLibrary.Name + " (COM Add-in Helper)";
            }
            string description = string.Format("Dynamically created COM Add-in to load custom UI for the Excel Add-in {0}, located at {1}.", addIn.DnaLibrary.Name, DnaLibrary.XllPath);


            Debug.Print("Getting Application object");
            object app     = ExcelDnaUtil.Application;
            Type   appType = app.GetType();

            Debug.Print("Got Application object: " + app.GetType().ToString());

            CultureInfo ci = new CultureInfo(1033);
            object      excelComAddIns;
            object      comAddIn;

            try
            {
                using (new SingletonClassFactoryRegistration(addIn, clsId))
                    using (new ProgIdRegistration(progId, clsId))
                        using (new ComAddInRegistration(progId, friendlyName, description))
                        {
                            excelComAddIns = appType.InvokeMember("COMAddIns", BindingFlags.GetProperty, null, app, null, ci);
                            //                            Debug.Print("Got COMAddins object: " + excelComAddIns.GetType().ToString());
                            appType.InvokeMember("Update", BindingFlags.InvokeMethod, null, excelComAddIns, null, ci);
                            //                            Debug.Print("Updated COMAddins object with AddIn registered");
                            comAddIn = excelComAddIns.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, excelComAddIns, new object[] { progId }, ci);
                            //                            Debug.Print("Got the COMAddin object: " + comAddIn.GetType().ToString());

                            // At this point Excel knows how to load our add-in by CLSID, so we could clean up the
                            // registry aggressively, before the actual (dangerous?) loading starts.
                            // But this seems to lead to some distress - Excel has some assertion checked when
                            // it updates the LoadBehavior after a successful load....
                            comAddIn.GetType().InvokeMember("Connect", BindingFlags.SetProperty, null, comAddIn, new object[] { true }, ci);
                            //                            Debug.Print("COMAddin is loaded.");
                            loadedComAddIns.Add(comAddIn);
                        }
            }
            catch (UnauthorizedAccessException secex)
            {
                Logging.LogDisplay.WriteLine("The Ribbon/COM Add-in helper required by add-in {0} could not be registered.\r\nThis may be due to restricted permissions on the user's HKCU\\Software\\Classes key.\r\nError message: {1}", DnaLibrary.CurrentLibrary.Name, secex.Message);
                Debug.Print("LoadComAddIn exception: " + secex.ToString());
            }
            catch (Exception ex)
            {
                Logging.LogDisplay.WriteLine("The Ribbon/COM Add-in helper required by add-in {0} could not be registered.\r\nThis is an unexpected error.\r\nError message: {1}", DnaLibrary.CurrentLibrary.Name, ex.Message);
                Debug.Print("LoadComAddIn exception: " + ex.ToString());
            }
        }