次の方法で共有


方法 : Visual C++ コード モデルを使用したコード操作 (Visual Basic)

更新 : 2007 年 11 月

Visual Studio コード モデルでは、オートメーション クライアントから、プロジェクト内のコード定義を検索したり、見つかったコード要素に変更を加えたりできます。Visual C++ には、Visual C++ 固有のコードを対象とした、コア コード モデルの拡張機能が用意されています。

たとえば、Language プロパティで、特定のコード要素が Visual C++ コード モデル オブジェクトとして指定され、さらに、Kind = vsCMElementClass の場合、Visual Studio コード モデルの CodeClass2 を使用するか、Visual C++ コード モデルの VCCodeClass を使用するかを選択できます。

Visual C++ に固有のコード モデルを使用して、コードをチェックしたり生成したりする方法を次の手順に示します。

プロジェクトの最初のファイルにコメントを追加するには

  1. Visual Basic で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックして Microsoft.VisualStudio.VCCodeModel を選択し、[OK] をクリックします。

  3. Connect.vb ファイルの先頭に Imports Microsoft.VisualStudio.VCCodeModel を追加します。

  4. OnConnection メソッドのコードを次のコードで置き換えます。

    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. アドインをビルドするには、[ビルド] メニューの [ソリューションのビルド] をクリックします。

  6. Visual Studio 統合開発環境 (IDE: Integrated Development Environment) で、Visual C++ プロジェクトを開きます。

  7. [ツール] メニューの [アドイン マネージャ] をクリックし、[アドイン マネージャ] ダイアログ ボックスからアドインを選択します。[OK] をクリックしてアドインを実行します。

  8. プロジェクトの 1 つ目のファイルで、プログラムによって追加されたコメントをチェックします。

Visual C++ プロジェクトに新しいファイルを追加するには

  1. Visual Basic で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックして Microsoft.VisualStudio.VCCodeModel を選択し、[OK] をクリックします。

  3. Connect.vb ファイルの先頭に Imports Microsoft.VisualStudio.VCCodeModel を追加します。

  4. OnConnection メソッドのコードを次のコードで置き換えます。

    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. アドインをビルドするには、[ビルド] メニューの [ソリューションのビルド] をクリックします。

  6. Visual Studio IDE で Visual C++ プロジェクトを開きます。

  7. [ツール] メニューの [アドイン マネージャ] をクリックし、[アドイン マネージャ] ダイアログ ボックスからアドインを選択します。[OK] をクリックしてアドインを実行します。

    ms228757.alert_note(ja-jp,VS.90).gifメモ :

    MyClass2.h が既に存在する場合はエラーになります。

関数を file.h に追加するには

  1. Visual Basic で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックして Microsoft.VisualStudio.VCCodeModel を選択し、[OK] をクリックします。

  3. Connect.vb ファイルの先頭に Imports Microsoft.VisualStudio.VCCodeModel を追加します。

  4. OnConnection メソッドのコードを次のコードで置き換えます。

    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. アドインをビルドするには、[ビルド] メニューの [ソリューションのビルド] をクリックします。

  6. Visual Studio IDE で Visual C++ プロジェクトを開き、file.h を追加します。

  7. [ツール] メニューの [アドイン マネージャ] をクリックし、[アドイン マネージャ] ダイアログ ボックスからアドインを選択します。[OK] をクリックしてアドインを実行します。

  8. file.h で挿入されたコードをチェックします。

最上位のコード要素を含むファイルを表示するには

  1. Visual Basic で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックして Microsoft.VisualStudio.VCCodeModel を選択し、[OK] をクリックします。

  3. Connect.vb ファイルの先頭に Imports Microsoft.VisualStudio.VCCodeModel を追加します。

  4. OnConnection メソッドのコードを次のコードで置き換えます。

    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. アドインをビルドするには、[ビルド] メニューの [ソリューションのビルド] をクリックします。

  6. Visual Studio IDE で Visual C++ プロジェクトを開きます。

  7. [ツール] メニューの [アドイン マネージャ] をクリックし、[アドイン マネージャ] ダイアログ ボックスからアドインを選択します。[OK] をクリックしてアドインを実行します。

    最上位のコード要素を含むファイル名がメッセージ ボックスに表示されます。

最上位のコード要素項目をすべて表示するには

  1. Visual Basic で Visual Studio のアドイン プロジェクトを作成します。

  2. [プロジェクト] メニューの [参照の追加] をクリックします。次に、[.NET] タブをクリックして Microsoft.VisualStudio.VCCodeModel を選択し、[OK] をクリックします。

  3. Connect.vb ファイルの先頭に Imports Microsoft.VisualStudio.VCCodeModel を追加します。

  4. OnConnection メソッドのコードを次のコードで置き換えます。

    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. アドインをビルドするには、[ビルド] メニューの [ソリューションのビルド] をクリックします。

  6. Visual Studio IDE で Visual C++ プロジェクトを開きます。

  7. [ツール] メニューの [アドイン マネージャ] をクリックし、[アドイン マネージャ] ダイアログ ボックスからアドインを選択します。[OK] をクリックしてアドインを実行します。

    最上位のコード要素名がメッセージ ボックスに表示されます。

参照

概念

Visual C++ コード モデル

コード モデルを使用したコードの調査 (Visual Basic)

コード モデルを使用したコードの調査 (Visual C#)