示例#1
0
        public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            try
            {
                _vbe          = RootComWrapperFactory.GetVbeWrapper(Application);
                _addin        = RootComWrapperFactory.GetAddInWrapper(AddInInst);
                _addin.Object = this;

                VBENativeServices.HookEvents(_vbe);

#if DEBUG
                // FOR DEBUGGING/DEVELOPMENT PURPOSES, ALLOW ACCESS TO SOME VBETypeLibsAPI FEATURES FROM VBA
                _addin.Object = new VBEditor.ComManagement.TypeLibsAPI.VBETypeLibsAPI_Object(_vbe);
#endif

                switch (ConnectMode)
                {
                case ext_ConnectMode.ext_cm_Startup:
                    // normal execution path - don't initialize just yet, wait for OnStartupComplete to be called by the host.
                    break;

                case ext_ConnectMode.ext_cm_AfterStartup:
                    _isBeginShutdownExecuted = false;       //When we reconnect after having been unloaded, the variable might no longer have its initial value.
                    InitializeAddIn();
                    break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#2
0
        private void ShutdownAddIn()
        {
            Debug.WriteLine("Extension unhooking VBENativeServices events.");
            VBENativeServices.UnhookEvents();

            var currentDomain = AppDomain.CurrentDomain;

            currentDomain.AssemblyResolve -= LoadFromSameFolder;
            Debug.WriteLine("Extension broadcasting shutdown.");
            User32.EnumChildWindows(_ide.MainWindow.Handle(), EnumCallback, new IntPtr(0));

            Debug.WriteLine("Extension calling ReleaseDockableHosts.");
            VBEditor.SafeComWrappers.VBA.Windows.ReleaseDockableHosts();

            if (_app != null)
            {
                Debug.WriteLine("Extension calling App.Shutdown.");
                _app.Shutdown();
                _app = null;
            }

            if (_kernel != null)
            {
                Debug.WriteLine("Extension calling Kernel.Dispose.");
                _kernel.Dispose();
                _kernel = null;
            }

            _isInitialized = false;
        }
        private static void AssociateCodePane(object sender, EventArgs eventArgs)
        {
            var subclass = (CodePaneSubclass)sender;

            subclass.VbeObject = VBENativeServices.GetCodePaneFromHwnd(subclass.Hwnd);
            SubclassLogger.Trace($"CodePane subclass for hWnd 0x{subclass.Hwnd.ToInt64():X8} associated itself with its VBE object.");
        }
示例#4
0
        protected virtual void DispatchFocusEvent(FocusType type)
        {
            var window = VBENativeServices.GetWindowInfoFromHwnd(Hwnd);

            if (!window.HasValue)
            {
                return;
            }
            OnFocusChange(new WindowChangedEventArgs(Hwnd, window.Value.Window, null, type));
        }
示例#5
0
        protected override void DispatchFocusEvent(FocusType type)
        {
            var window = VBENativeServices.GetWindowInfoFromHwnd(Hwnd);

            if (!window.HasValue)
            {
                return;
            }
            OnFocusChange(new WindowChangedEventArgs(window.Value.Hwnd, window.Value.Window, CodePane, type));
        }
示例#6
0
        private void ShutdownAddIn()
        {
            var currentDomain = AppDomain.CurrentDomain;

            try
            {
                _logger.Log(LogLevel.Info, "Rubberduck is shutting down.");
                _logger.Log(LogLevel.Trace, "Unhooking VBENativeServices events...");
                VBENativeServices.UnhookEvents();

                _logger.Log(LogLevel.Trace, "Broadcasting shutdown...");
                User32.EnumChildWindows(_ide.MainWindow.Handle(), EnumCallback, new IntPtr(0));

                _logger.Log(LogLevel.Trace, "Releasing dockable hosts...");
                Windows.ReleaseDockableHosts();

                if (_app != null)
                {
                    _logger.Log(LogLevel.Trace, "Initiating App.Shutdown...");
                    _app.Shutdown();
                    _app = null;
                }

                if (_kernel != null)
                {
                    _logger.Log(LogLevel.Trace, "Disposing IoC container...");
                    _kernel.Dispose();
                    _kernel = null;
                }

                _isInitialized = false;
                _logger.Log(LogLevel.Info, "No exceptions were thrown.");
            }
            catch (Exception e)
            {
                _logger.Error(e);
                _logger.Log(LogLevel.Warn, "Exception is swallowed.");
                //throw; // <<~ uncomment to crash the process
            }
            finally
            {
                _logger.Log(LogLevel.Trace, "Unregistering AppDomain handlers....");
                currentDomain.AssemblyResolve    -= LoadFromSameFolder;
                currentDomain.UnhandledException -= HandlAppDomainException;
                _logger.Log(LogLevel.Trace, "Done. Initiating garbage collection...");
                GC.Collect();
                _logger.Log(LogLevel.Trace, "Done. Waiting for pending finalizers...");
                GC.WaitForPendingFinalizers();
                _logger.Log(LogLevel.Trace, "Done. Shutdown completed. Quack!");
                _isInitialized = false;
            }
        }
示例#7
0
        public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            try
            {
                if (Application is Microsoft.Vbe.Interop.VBE vbe1)
                {
                    _ide = new VBEditor.SafeComWrappers.VBA.VBE(vbe1);
                    VBENativeServices.HookEvents(_ide);

                    var addin = (AddIn)AddInInst;
                    _addin = new VBEditor.SafeComWrappers.VBA.AddIn(addin)
                    {
                        Object = this
                    };

#if DEBUG
                    // FOR DEBUGGING/DEVELOPMENT PURPOSES, ALLOW ACCESS TO SOME VBETypeLibsAPI FEATURES FROM VBA
                    _addin.Object = new Rubberduck.VBEditor.ComManagement.TypeLibsAPI.VBETypeLibsAPI_Object(_ide);
#endif
                }
                else if (Application is Microsoft.VB6.Interop.VBIDE.VBE vbe2)
                {
                    _ide = new VBEditor.SafeComWrappers.VB6.VBE(vbe2);

                    var addin = (Microsoft.VB6.Interop.VBIDE.AddIn)AddInInst;
                    _addin = new VBEditor.SafeComWrappers.VB6.AddIn(addin);
                }


                switch (ConnectMode)
                {
                case ext_ConnectMode.ext_cm_Startup:
                    // normal execution path - don't initialize just yet, wait for OnStartupComplete to be called by the host.
                    break;

                case ext_ConnectMode.ext_cm_AfterStartup:
                    _isBeginShutdownExecuted = false;       //When we reconnect after having been unloaded, the variable might no longer have its initial value.
                    InitializeAddIn();
                    break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
示例#8
0
        public void OnConnection(object Application, ext_ConnectMode ConnectMode, object AddInInst, ref Array custom)
        {
            try
            {
                if (Application is VBE)
                {
                    var vbe = (VBE)Application;
                    _ide = new VBEditor.SafeComWrappers.VBA.VBE(vbe);
                    VBENativeServices.HookEvents(_ide);

                    var addin = (AddIn)AddInInst;
                    _addin = new VBEditor.SafeComWrappers.VBA.AddIn(addin)
                    {
                        Object = this
                    };
                }
                else if (Application is Microsoft.VB6.Interop.VBIDE.VBE)
                {
                    var vbe = Application as Microsoft.VB6.Interop.VBIDE.VBE;
                    _ide = new VBEditor.SafeComWrappers.VB6.VBE(vbe);

                    var addin = (Microsoft.VB6.Interop.VBIDE.AddIn)AddInInst;
                    _addin = new VBEditor.SafeComWrappers.VB6.AddIn(addin);
                }


                switch (ConnectMode)
                {
                case ext_ConnectMode.ext_cm_Startup:
                    // normal execution path - don't initialize just yet, wait for OnStartupComplete to be called by the host.
                    break;

                case ext_ConnectMode.ext_cm_AfterStartup:
                    InitializeAddIn();
                    break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
        private void OnVbeFocusChanged(object sender, WindowChangedEventArgs e)
        {
            if (e.EventType == FocusType.GotFocus)
            {
                switch (e.Hwnd.ToWindowType())
                {
                case WindowType.DesignerWindow:
                    Task.Run(() =>
                    {
                        using (var component = _vbe.SelectedVBComponent)
                        {
                            DispatchSelectedDesignerDeclaration(component);
                        }
                    });
                    break;

                case WindowType.CodePane:
                    //Caret changed in a code pane.
                    Task.Run(() =>
                    {
                        using (var pane = VBENativeServices.GetCodePaneFromHwnd(e.Hwnd))
                        {
                            DispatchSelectedDeclaration(
                                new DeclarationChangedEventArgs(_vbe, _parser.State.FindSelectedDeclaration(pane)));
                        }
                    });
                    break;
                }
            }
            else if (e.EventType == FocusType.ChildFocus)
            {
                //Treeview selection changed in project window.
                Task.Run(() =>
                {
                    using (var component = _vbe.SelectedVBComponent)
                    {
                        DispatchSelectedProjectNodeDeclaration(component);
                    }
                });
            }
        }
示例#10
0
        private void ShutdownAddIn()
        {
            var currentDomain = AppDomain.CurrentDomain;

            try
            {
                _logger.Log(LogLevel.Info, "Rubberduck is shutting down.");
                _logger.Log(LogLevel.Trace, "Unhooking VBENativeServices events...");
                VBENativeServices.UnhookEvents();

                _logger.Log(LogLevel.Trace, "Releasing dockable hosts...");
                Windows.ReleaseDockableHosts();

                if (_app != null)
                {
                    _logger.Log(LogLevel.Trace, "Initiating App.Shutdown...");
                    _app.Shutdown();
                    _app = null;
                }

                if (_container != null)
                {
                    _logger.Log(LogLevel.Trace, "Disposing IoC container...");
                    _container.Dispose();
                    _container = null;
                }
            }
            catch (Exception e)
            {
                _logger.Error(e);
                _logger.Log(LogLevel.Warn, "Exception is swallowed.");
                //throw; // <<~ uncomment to crash the process
            }
            finally
            {
                try
                {
                    _logger.Log(LogLevel.Trace, "Disposing COM safe...");
                    ComSafeManager.DisposeAndResetComSafe();
                    _addin = null;
                    _ide   = null;

                    _isInitialized = false;
                    _logger.Log(LogLevel.Info, "No exceptions were thrown.");
                }
                catch (Exception e)
                {
                    _logger.Error(e);
                    _logger.Log(LogLevel.Warn, "Exception disposing the ComSafe has been swallowed.");
                    //throw; // <<~ uncomment to crash the process
                }
                finally
                {
                    _logger.Log(LogLevel.Trace, "Unregistering AppDomain handlers....");
                    currentDomain.AssemblyResolve    -= LoadFromSameFolder;
                    currentDomain.UnhandledException -= HandlAppDomainException;
                    _logger.Log(LogLevel.Trace, "Done. Main Shutdown completed. Toolwindows follow. Quack!");
                    _isInitialized = false;
                }
            }
        }