How to: Manipulate Code by Using the Visual C++ Code Model (Visual C#)
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 C#.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.
Add using Microsoft.VisualStudio.VCCodeModel; to the top of the Connect.cs file.
Replace the code in the OnConnection method with the following code:
// Add-in code. using Microsoft.VisualStudio.VCCodeModel; public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { _applicationObject = (DTE2)application; )addInInstance = (AddIn)addInInst; // Pass the applicationObject member variable to the code example. test((DTE2)_applicationObject); } public void test( DTE2 dte ) { VCCodeModel vcCM = null; VCCodeElement vcCodeElement = null; vcCM = ( ( VCCodeModel )( dte.Solution.Item( 1 ).CodeModel ) ); vcCodeElement = ( ( VCCodeElement ) ( vcCM.CodeElements.Item(1))); AddCommentAtStart( vcCodeElement ); AddCommentAtEnd( vcCodeElement ); } public void AddCommentAtStart( Microsoft.VisualStudio.VCCodeModel.VCCodeElement vcCodeElement ) { TextPoint textPoint = null; textPoint = vcCodeElement.get_StartPointOf( vsCMPart.vsCMPartWhole, 0 ); textPoint.CreateEditPoint().Insert("/*This is a Start Comment*/"); } public void AddCommentAtEnd( Microsoft.VisualStudio.VCCodeModel.VCCodeElement vcCodeElement ) { TextPoint textPoint = null; textPoint = vcCodeElement.get_EndPointOf( vsCMPart.vsCMPartWhole, 0 ); textPoint.CreateEditPoint().Insert( "/*End Comment*/" ); }
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 then 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 C#.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.
Add using Microsoft.VisualStudio.VCCodeModel; to the top of the Connect.cs file.
Replace the code in the OnConnection method with the following code:
//Add-in code. using Microsoft.VisualStudio.VCCodeModel; public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; // Pass the applicationObject member variable to the code example. GetVCCodeElement((DTE2)_applicationObject); } // Shows how to get a VCCodeElement. public void GetVCCodeElement( DTE2 dte ) { VCCodeModel vcCM = null; VCCodeElement vcCodeElement = null; vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )( dte.Solution.Item( 1 ).CodeModel ) ); vcCodeElement = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeElement )( vcCM.AddClass( "MyClass2", "MyClass2.h",0,null, null, EnvDTE.vsCMAccess.vsCMAccessDefault ) ) ); }
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 C#.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel and System.Windows.Forms, and then click OK.
Add the following using statements to the top of the Connect.cs file:
using System.Windows.Forms; using Microsoft.VisualStudio.VCCodeModel;
Replace the code in the OnConnection method with the following code:
// Add-in code. using Microsoft.VisualStudio.VCCodeModel; using System.Windows.Forms; public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; // Pass the applicationObject member variable to the code example. DisplayName((DTE2)_applicationObject); } // DisplayName // Shows the DisplayName of a function which includes the parameter // names. public void DisplayName( DTE2 dte ) { VCCodeModel vcCM = null; VCCodeElement vcCodeElement = null; vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )( dte.Solution.Item( 1 ).CodeModel ) ); vcCodeElement = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeElement ) ( vcCM.AddFunction( "MyFunction", "File.h", vsCMFunction.vsCMFunctionFunction, "void", null, EnvDTE.vsCMAccess.vsCMAccessDefault ) ) ); MessageBox.Show( vcCodeElement.DisplayName); }
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 C#.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel and System.Windows.Forms, and then click OK.
Add the following using statements to the top of the Connect.cs file:
using System.Windows.Forms; using Microsoft.VisualStudio.VCCodeModel;
Replace the code in the OnConnection method with the following code:
// Add-in code. using Microsoft.VisualStudio.VCCodeModel; using System.Windows.Forms; public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; // Pass the applicationObject member variable to the code example. DisplayLocation((DTE2)_applicationObject); } public void DisplayLocation( DTE2 dte ) { VCCodeModel vcCM = null; VCCodeElement vcCodeElement = null; vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )( dte.Solution.Item( 1 ).CodeModel ) ); foreach ( Microsoft.VisualStudio.VCCodeModel.VCCodeElement temp in vcCM.CodeElements ) { vcCodeElement = temp; MessageBox.Show( vcCodeElement.Name + " is declared in " + vcCodeElement.get_Location(vsCMWhere.vsCMWhereDefault)); } }
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 the top level code element items
Create a Visual Studio add-in project in Visual C#.
On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel and System.Windows.Forms, and then click OK.
Add the following using statements to the top of the Connect.cs file:
using System.Windows.Forms; using Microsoft.VisualStudio.VCCodeModel;
Replace the code in the OnConnection method with the following code:
// Add-in code. using Microsoft.VisualStudio.VCCodeModel; using System.Windows.Forms; public void OnConnection(object application, Extensibility.ext_ConnectMode connectMode, object addInInst, ref System.Array custom) { _applicationObject = (DTE2)application; _addInInstance = (AddIn)addInInst; // Pass the applicationObject member variable to the code example. FindItem((DTE2)_applicationObject); } public void FindItem( DTE2 dte ) { VCCodeModel vcCM = null; VCCodeElements vcCodeElements = null; vcCM = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeModel )( dte.Solution.Item( 1 ).CodeModel ) ); vcCodeElements = ( ( Microsoft.VisualStudio.VCCodeModel.VCCodeElements ) ( vcCM.CodeElements ) ); int i = 0; for ( i=1; i<=vcCodeElements.Count; i++ ) { MessageBox.Show( vcCodeElements.Item( i ).Name); } }
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.
See Also
Tasks
How to: Manipulate Code by Using the Visual C++ Code Model (Visual Basic)