How to: Manipulate Projects by Using the Visual C++ Project Model
The following procedures demonstrate how to utilize some of the objects available through Microsoft.VisualStudio.ProjectEngine.dll to programmatically examine information that is specific to Visual C++ projects.
The Visual C++ Project Model, which is contained in Microsoft.VisualStudio.VCProjectEngine.dll, programmatically exposes the functionality of the compiler, linker, and other build tools, as well as to the functionality of the Property Pages dialog box for a Visual C++ project.
Note
The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. These procedures were developed with the General Development Settings active. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings.
To use VCProjectEngine to manipulate Visual C++ projects
Create a Visual Studio add-in project in Visual C#.
On the Project menu, click Add Reference, click the .NET tab, select all EnvDTE references, Microsoft.VisualStudio.VCCodeModel and System.Windows.Forms, and then click OK.
Add the following using statements to the top of the Connect.vs file:
using EnvDTE; using EnvDTE80; using EnvDTE90; using EnvDTE100; using Microsoft.VisualStudio.VCCodeModel;using System.Windows.Forms;
In Visual Basic add the following Imports statements before the OnConnection method:
Imports EnvDTE Imports EnvDTE80 Imports EnvDTE90 Imports EnvDTE100 Imports Microsoft.VisualStudio.VCCodeModel Imports System.Windows.Forms
Replace the code in the OnConnection method with the following code:
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. VCProjectEngineExample((DTE2)_applicationObject); } public void VCProjectEngineExample(DTE2 dte) { VCProject prj; VCAssemblyReference vcar; int idx; IVCCollection mycollection, mycollection2; VCConfiguration cfg; String sTest = "Configuration Information:" + "\n"; VCFile file; try { prj = (VCProject)dte.Solution.Projects.Item(1).Object; if (prj.CanAddAssemblyReference ("C:\\Program Files\\Microsoft Visual Studio 8\ \Common7\\IDE\\PublicAssemblies\\VSLangProj.dll")) { // Make sure the path to the assembly // is correct. The installation root in the // example is "C:", but it might be different // on your computer. vcar= (VCAssemblyReference)prj.AddAssemblyReference ("C:\\Program Files\\Microsoft Visual Studio 8\ \Common7\\IDE\\PublicAssemblies\\VSLangProj.dll"); MessageBox.Show("The assembly named" + vcar.AssemblyName + " was added." + "\n" + "Its culture and description are: " + "\n" + vcar.Culture + vcar.Description); } mycollection = (IVCCollection)prj.Configurations; MessageBox.Show ("The number of configurations in the project is: " + mycollection.Count); for(idx = 1;idx <= mycollection.Count;idx++) { cfg = (VCConfiguration)mycollection.Item(idx); sTest = sTest + "Configuration name: " + cfg.Name + "\n" + "Its platform: " + "\n" + cfg.Platform.ToString() + "\n" + "Program database: " + "\n" + cfg.ProgramDatabase + "\n"; } MessageBox.Show(sTest); mycollection2 = (IVCCollection)prj.Files; MessageBox.Show("Number of files in the project is: " + mycollection2.Count); for(idx = 1;idx <= mycollection2.Count;idx++) { file = (VCFile)mycollection2.Item(idx); MessageBox.Show("The file extension of item # " + idx.ToString() + " is: " + file.Extension); } } catch (SystemException ex) { MessageBox.Show("ERROR: " + ex.ToString()); } }
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.
The add-in code adds a reference to VSLangProj.dll and displays information about it by using the VCAssemblyReference object. It also enumerates and displays all the configuration names by using the VCConfiguration object. VCFile is used to display the file extensions that are present in the Visual C++ project. The VCProject and IVCCollection objects are used to declare the project as a Visual C++ project and to enumerate the configuration and file collections.
Example
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.
VCProjectEngineExample((DTE2)_applicationObject);
}
public void VCProjectEngineExample(DTE2 dte)
{
VCProject prj;
VCAssemblyReference vcar;
int idx;
IVCCollection mycollection, mycollection2;
VCConfiguration cfg;
String sTest = "Configuration Information:" + "\n";
VCFile file;
try
{
prj = (VCProject)dte.Solution.Projects.Item(1).Object;
// Make sure the path to the assembly
// is correct. The installation root in the
// example is "C:", but it might be different
// on your computer.
if (prj.CanAddAssemblyReference
("C:\\Program Files\\Microsoft Visual Studio 8\
\Common7\\IDE\\PublicAssemblies\\VSLangProj.dll"))
{
vcar= (VCAssemblyReference)prj.AddAssemblyReference
("C:\\Program Files\\Microsoft Visual Studio 8
\\Common7\\IDE\\PublicAssemblies\\VSLangProj.dll");
MessageBox.Show("The assembly named" + vcar.AssemblyName
+ " was added." + "\n" + "Its culture and description are: " + "\n"
+ vcar.Culture + vcar.Description);
}
mycollection = (IVCCollection)prj.Configurations;
MessageBox.Show
("The number of configurations in the project is: "
+ mycollection.Count);
for(idx = 1;idx <= mycollection.Count;idx++)
{
cfg = (VCConfiguration)mycollection.Item(idx);
sTest = sTest + "Configuration name: " +cfg.Name + "\n"
+ "Its plattform: " + "\n" +
cfg.Platform.ToString() + "\n" + "Program database: "
+ "\n" + cfg.ProgramDatabase + "\n";
}
MessageBox.Show(sTest);
mycollection2 = (IVCCollection)prj.Files;
MessageBox.Show("Number of files in the project is: "
+ mycollection2.Count);
for(idx = 1;idx <= mycollection2.Count;idx++)
{
file = (VCFile)mycollection2.Item(idx);
MessageBox.Show("The file extension of item # "
+ idx.ToString() + " is: " + file.Extension);
}
}
catch (SystemException ex)
{
MessageBox.Show("ERROR: " + ex.ToString());
}
}
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)
VCProjectEngineExample(_applicationObject)
End Sub
Sub VCProjectEngineExample(ByVal dte As DTE2)
Dim prj As VCProject
Dim vcar As VCAssemblyReference
Dim idx As Integer
Dim mycollection, mycollection2 As IVCCollection
Dim cfg As VCConfiguration
Dim sTest As String = "Configuration information:" & vbCr
Dim file As VCFile
Try
prj = CType(dte.Solution.Projects.Item(1).Object, VCProject)
' Make sure the path to the assembly
' is correct. The installation root in the
' example is "C:", but it might be different
' on your computer.
If prj.CanAddAssemblyReference _
("C:\Program Files\Microsoft Visual Studio 8\Common7 _
\IDE\ PublicAssemblies\VSLangProj.dll") Then
vcar = CType(prj.AddAssemblyReference _
("C:\Program Files\ _
Microsoft Visual Studio 8\ _
Common7\IDE\PublicAssemblies\VSLangProj.dll"), _
VCAssemblyReference)
MsgBox("The assembly named" & vcar.AssemblyName
& " was added." & vbCr & _
"Its culture and description are: " & vbCr & vcar.Culture _
& vcar.Description)
End If
mycollection = CType(prj.Configurations, IVCCollection)
MsgBox("The number of configurations in the project is: " _
& mycollection.Count)
For idx = 1 To mycollection.Count
cfg = CType(mycollection.Item(idx), VCConfiguration)
sTest = sTest & "Configuration name: " & cfg.Name & vbCr _
& "Its platform: " & vbCr & cfg.Platform.ToString() _
& vbCr & "Program database: " & vbCr & _
cfg.ProgramDatabase & vbCr
Next
MsgBox(sTest)
mycollection2 = CType(prj.Files, IVCCollection)
MsgBox("Number of files in the project: " _
& mycollection2.Count)
For idx = 1 To mycollection2.Count
file = CType(mycollection2.Item(idx), VCFile)
MsgBox("The file extension of item # " & idx.ToString() & _
" is: " & file.Extension)
Next idx
Catch ex As System.Exception
MsgBox("ERROR: " & ex.Message)
End Try
End Sub
Compiling the Code
To compile this code, create a new Visual Studio add-in project and replace the code of the OnConnection method in the Connect class with the code in the example. For information about how to run an add-in, see How to: Control Add-Ins By Using the Add-In Manager.