How to: Manipulate Code by Using the Visual C++ Code Model (Visual Basic)
In Visual Studio 2013, add-ins are deprecated. We recommend that you upgrade your add-ins to VSPackage extensions. For more information about how to upgrade, see FAQ: Converting Add-ins to VSPackage Extensions.
The Visual Studio code model offers automation clients the ability to find code definitions in a project and modify those code elements. Visual C++ provides an extension to the core code model to target code that is specific to Visual C++.
For example, if the Language property indicates that a given code element is a Visual C++ code model object, and Kind = vsCMElementClass, then you can choose to use either the CodeClass2 from the Visual Studio code model or the VCCodeClass from the Visual C++ code model.
The following procedures demonstrate how to examine and generate code by using the code model that is specific to Visual C++.
To add a comment to the first file in the project
Create a Visual Studio add-in project in Visual Basic.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.
Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.
Replace the code in the OnConnection method with the following code:
Imports Microsoft.VisualStudio.VCCodeModel Public Sub OnConnection(ByVal application As Object, ByVal _ connectMode As ext_ConnectMode, ByVal addInInst As Object, _ ByRef custom As Array) Implements IDTExtensibility2.OnConnection _applicationObject = CType(application, DTE2) _addInInstance = CType(addInInst, AddIn) test(_applicationObject) End Sub Sub test(ByVal dte As DTE2) Dim vcCM As VCCodeModel Dim vcCodeElement As VCCodeElement vcCM = CType(dte.Solution.Item(1).CodeModel, VCCodeModel) vcCodeElement = CType(vcCM.CodeElements.Item(1), _ VCCodeElement) AddCommentAtStart(vcCodeElement) AddCommentAtEnd(vcCodeElement) End Sub ' Project Property ' StartPointOf Property ' Adds a comment before the VCCodeElement declaration. Sub AddCommentAtStart(ByVal vcCodeElement As VCCodeElement) Dim textPoint As TextPoint textPoint = vcCodeElement.StartPointOf(vsCMPart.vsCMPartWhole) textPoint.CreateEditPoint().Insert("/*This is a Start_ Comment*/") End Sub Sub AddCommentAtEnd(ByVal vcCodeElement As VCCodeElement) Dim textPoint As TextPoint textPoint = vcCodeElement.EndPointOf(vsCMPart.vsCMPartWhole) textPoint.CreateEditPoint().Insert("/*End Comment*/") End Sub
To build the add-in, click Build Solution on the Build menu.
Open a Visual C++ project in the Visual Studio integrated development environment (IDE).
On the Tools menu, click Add-in Manager, and select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.
Examine the first file in the project for the programmatically added comments.
To add a new file to a Visual C++ project
Create a Visual Studio add-in project in Visual Basic.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.
Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.
Replace the code in the OnConnection method with the following code:
Imports Microsoft.VisualStudio.VCCodeModel Public Sub OnConnection(ByVal application As Object, ByVal _ connectMode As ext_ConnectMode, ByVal addInInst As Object, _ ByRef custom As Array) Implements IDTExtensibility2.OnConnection _applicationObject = CType(application, DTE2) _addInInstance = CType(addInInst, AddIn) GetVCCodeElement(_applicationObject) End Sub ' Shows how to get a VCCodeElement. Sub GetVCCodeElement(ByVal dte As DTE2) Dim vcCM As VCCodeModel Dim vcCodeElement As VCCodeElement vcCM = CType(dte.Solution.Item(1).CodeModel, VCCodeModel) vcCodeElement = CType(vcCM.AddClass("MyClass2", "MyClass2.h"), _ VCCodeElement) End Sub
To build the add-in, click Build Solution on the Build menu.
Open a Visual C++ project in the Visual Studio IDE.
On the Tools menu, click Add-in Manager, and select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.
Note
If MyClass2.h already exists, the code fails.
To add a function to file.h
Create a Visual Studio add-in project in Visual Basic.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.
Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.
Replace the code in the OnConnection method with the following code:
Imports Microsoft.VisualStudio.VCCodeModel Public Sub OnConnection(ByVal application As Object, ByVal _ connectMode As ext_ConnectMode, ByVal addInInst As Object, _ ByRef custom As Array) Implements IDTExtensibility2.OnConnection _applicationObject = CType(application, DTE2) _addInInstance = CType(addInInst, AddIn) DisplayName(_applicationObject) End Sub ' DisplayName ' Shows the DisplayName of a function which includes the parameter ' names. Sub DisplayName(ByVal dte As DTE2) Dim vcCM As VCCodeModel Dim vcCodeElement As VCCodeElement vcCM = CType(dte.Solution.Item(1).CodeModel, VCCodeModel) vcCodeElement = CType(vcCM.AddFunction("MyFunction", "File.h", _ vsCMFunction.vsCMFunctionFunction, "void"), VCCodeElement) MsgBox(vcCodeElement.DisplayName) End Sub
To build the add-in, click Build Solution on the Build menu.
Open a Visual C++ project in the Visual Studio IDE and add a file.h to it.
On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.
Examine the inserted code in file.h.
To display files that include top-level code elements
Create a Visual Studio add-in project in Visual Basic.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.
Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.
Replace the code in the OnConnection method with the following code:
Imports Microsoft.VisualStudio.VCCodeModel Public Sub OnConnection(ByVal application As Object, ByVal _ connectMode As ext_ConnectMode, ByVal addInInst As Object, _ ByRef custom As Array) Implements IDTExtensibility2.OnConnection _applicationObject = CType(application, DTE2) _addInInstance = CType(addInInst, AddIn) DisplayLocation(_applicationObject) End Sub Sub DisplayLocation(ByVal dte As DTE2) Dim vcCM As VCCodeModel Dim vcCodeElement As VCCodeElement vcCM = CType(dte.Solution.Item(1).CodeModel, VCCodeModel) For Each vcCodeElement In vcCM.CodeElements MsgBox(vcCodeElement.Name + " is declared in " _ & vcCodeElement.Location) Next End Sub
To build the add-in, click Build Solution on the Build menu.
Open a Visual C++ project in the Visual Studio IDE.
On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.
Message boxes display the file names that contain the top level code elements.
To display all top-level code element items
Create a Visual Studio add-in project in Visual Basic.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.
Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.
Replace the code in the OnConnection method with the following code:
Imports Microsoft.VisualStudio.VCCodeModel Public Sub OnConnection(ByVal application As Object, ByVal _ connectMode As Extensibility.ext_ConnectMode, ByVal addInInst _ As Object, ByRef custom As System.Array) Implements _ Extensibility.IDTExtensibility2.OnConnection _applicationObject = CType(application, DTE2) _addInInstance = CType(addInInst, AddIn) FindItem(_applicationObject) End Sub Sub FindItem(ByVal dte As DTE2) Dim vcCM As VCCodeModel Dim vcCodeElements As VCCodeElements vcCM = CType(dte.Solution.Item(1).CodeModel, VCCodeModel) vcCodeElements = CType(vcCM.CodeElements, VCCodeElements) Dim i As Integer For i = 1 To vcCodeElements.Count MsgBox(vcCodeElements.Item(i).Name) Next End Sub
To build the add-in, click Build Solution on the Build menu.
Open a Visual C++ project in the Visual Studio IDE.
On the Tools menu, click Add-in Manager, and then select your add-in from the Add-In Manager dialog box. Click OK to run your add-in.
Message boxes display the top level code element names.