Varun Gupta
Varun Gupta

Reputation: 11

Visual Studio AddIn

I am looking to create a Visual Studio AddIn which can help me launch my own debugging process. I want to keep the original F5 based debugging intact and hence i do not want to intercept that call and need a separate AddIn.

Any suggestions

Upvotes: 1

Views: 435

Answers (1)

Visual Micro
Visual Micro

Reputation: 1561

The easiest way is to capture the system events/macros using an addin. It is really easy to override what vs does in these events. All the events are automatically fired when using the standard visual studio commands such as F5. This includes all of the standard visual studio shortcut keys, menus and tool bar buttons.

Create a new vs addin project and it will automatically add the code to attach the OnBeforeCommandEvent. In vb the event handler will look like the code below.

Friend Sub OnBeforeCommandEvent(sGuid As String, ID As Integer, CustomIn As Object, CustomOut As Object, ByRef CancelDefault As Boolean)

The event passes you sGuid and ID. You can resolve these two items to a macro string name (sCommandName) as follows:-

 Dim objCommand As EnvDTE.Command

 Try
        objCommand = _applicationObject.Commands.Item(sGuid, ID)
 Catch ex As Exception
        'unknown guids can be ignored
        Exit Sub
 End Try

 If objCommand Is Nothing Then Exit Sub
        Dim sCommandName As String
        sCommandName = objCommand.Name

nb: The _applicationObject is passed to your code when the addin starts. A new addin project will automatically inlude the following code for the OnConnection event, the first argument is the _applicationObject shown above.

OnConnection(ByVal application As Object

Once you have the sCommandName variable it will contain the name of a Visual Studio macro such as Debug.Start.

To override the Debug.Start functions then you would add some of your own code and remember to set CancelDefault to True before you exit the handler.

When you set CancelDefault to true Visual Studio will not run the standard macro which means you can run your own debugger when F5 is pressed.

These are Visual Studio macro names that are used during the build process. You can override as many or as few as you like. I have grouped them into their related functionality but you can handle them in any combination.

Select Case sCommandName

         Case "Debug.Start", _
                    "Debug.StartWithoutDebugging"
                    System.Windows.Forms.MessageBox.Show("You clicked F5, we are overriding the debug process")
                    CancelDefault=true
                    Exit Sub


         Case "ClassViewContextMenus.ClassViewProject.Rebuild", _
                    "ClassViewContextMenus.ClassViewProject.Build", _
                    "Build.RebuildOnlyProject", _
                    "Build.RebuildSelection", _
                    "Build.BuildOnlyProject", _
                    "Build.BuildSelection"

         Case "Build.RebuildSolution", _
                    "Build.BuildSolution"

         Case "ClassViewContextMenus.ClassViewProject.Debug.Startnewinstance", _
                    "ClassViewContextMenus.ClassViewProject.Debug.StepIntonewinstance"

         Case "Build.CleanSelection", _
                    "Build.CleanSolution", _
                    "ClassViewContextMenus.ClassViewProject.Clean"

         Case "Build.SolutionConfigurations"

Upvotes: 5

Related Questions