http://blogs.clariusconsulting.net/pga

Pablo Galiano's Blog

Go Back to
pga′s Latest post

How do I intercept a Visual Studio command execution

My third How do I is up.

Scenario

Let’s say that we want to intercept the “Clean Solution” command. For a particular kind of project, before executing the command we need to clean other resources and instead of doing it with msbuild we want to do it by intercepting the command at the VS level. The focus of this How do I tackles this scenario.

Interfaces and classes needed

 

Code snippet

    public class VSCommandInterceptor : IDisposable

    {

        private IServiceProvider serviceProvider;

        private Guid guid;

        private int id;

        private bool isDisposed;

 

        public VSCommandInterceptor(IServiceProvider serviceProvider, Guid commandGuid, int commandId)

        {

            this.serviceProvider = serviceProvider;

            this.guid = commandGuid;

            this.id = commandId;

 

            if(CommandEvents != null)

            {

                CommandEvents.AfterExecute += new _dispCommandEvents_AfterExecuteEventHandler(OnAfterExecute);

                CommandEvents.BeforeExecute += new _dispCommandEvents_BeforeExecuteEventHandler(OnBeforeExecute);

            }

        }

 

        public event EventHandler<EventArgs> AfterExecute;

        public event EventHandler<EventArgs> BeforeExecute;

 

        private CommandEvents commandEvents;

        protected CommandEvents CommandEvents

        {

            get

            {

                if(commandEvents == null)

                {

                    DTE dte = this.serviceProvider.GetService(typeof(DTE)) as DTE;

 

                    if(dte != null)

                    {

                        commandEvents = dte.Events.get_CommandEvents(guid.ToString(“B”), id) as CommandEvents;

                    }

                }

 

                return commandEvents;

            }

        }

 

        public void Dispose()

        {

            this.Dispose(true);

            GC.SuppressFinalize(this);

        }

 

        private void Dispose(bool disposing)

        {

            if(!this.isDisposed && disposing)

            {

                if(CommandEvents != null)

                {

                    CommandEvents.AfterExecute -= OnAfterExecute;

                    CommandEvents.BeforeExecute -= OnBeforeExecute;

                }

 

                this.isDisposed = true;

            }

        }

 

        private void OnAfterExecute(string Guid, int ID, object CustomIn, object CustomOut)

        {

            if(AfterExecute != null)

            {

                AfterExecute(this, new EventArgs());

            }

        }

 

        private void OnBeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)

        {

            if(BeforeExecute != null)

            {

                BeforeExecute(this, new EventArgs());

            }

        }

    }

 

Usage

    VSCommandInterceptor interceptor =

        new VSCommandInterceptor(

            serviceProvider,

            typeof(Microsoft.VisualStudio.VSConstants.VSStd97CmdID).GUID,

            (int)Microsoft.VisualStudio.VSConstants.VSStd97CmdID.CleanSln);

 

    interceptor.BeforeExecute += new EventHandler<EventArgs>(BeforeExecute);

 

    private void BeforeExecute(object sender, EventArgs e)

    {

        //TODO: Provide logic

    }

 

Assemblies needed

  • EnvDTE

 

Stay tuned,

Pablo

Comments

1 Comment

  1. Hello Pablo,

    I came across this example which does exactly what I need to do. However, it makes use of classes that Microsofot document as ‘infrastructure’ and that are intended for their own internal use.
    Is there no other way to intercept or react to commands?