Share via


How to: Manipulate Code by Using the Visual C++ Code Model (Visual Basic)

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

  1. Create a Visual Studio add-in project in Visual Basic.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.

  4. 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
    
  5. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio integrated development environment (IDE).

  7. 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.

  8. Examine the first file in the project for the programmatically added comments.

To add a new file to a Visual C++ project

  1. Create a Visual Studio add-in project in Visual Basic.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.

  4. 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
    
  5. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE.

  7. 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

  1. Create a Visual Studio add-in project in Visual Basic.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.

  4. 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
    
  5. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE and add a file.h to it.

  7. 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.

  8. Examine the inserted code in file.h.

To display files that include top-level code elements

  1. Create a Visual Studio add-in project in Visual Basic.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.

  4. 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
    
  5. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE.

  7. 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

  1. Create a Visual Studio add-in project in Visual Basic.

  2. On the Project menu, click Add Reference, click the .NET tab, select Microsoft.VisualStudio.VCCodeModel, and then click OK.

  3. Add Imports Microsoft.VisualStudio.VCCodeModel to the top of the Connect.vb file.

  4. 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
    
  5. To build the add-in, click Build Solution on the Build menu.

  6. Open a Visual C++ project in the Visual Studio IDE.

  7. 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

Concepts

Visual C++ Code Model

Discovering Code by Using the Code Model (Visual Basic)

Discovering Code by Using the Code Model (Visual C#)