Using the Automation Model
After you have connected your VSPackage to automation, you can obtain the properties and methods by calling the GetObject method on the _DTE object, passing a string representing the object you wish to retrieve.
Obtaining Project Objects
The following are two code examples that show how an automation consumer obtains the initial project automation objects. The first example uses a Visual Studio macro; the second uses C++ code.
Sub DoAutomation()
Dim MyProjects As Projects
MyProjects = DTE.GetObject("AcmeProject")
End Sub
void DoAutomation(void)
{
CComQIPtr<Projects> pMyPkg; // Use an IDispatch-derived object type.
pMyPkg = pDTE->GetObject("AcmeProjects");
// The '=' performs a Query Interface.
// Assumes pDTE is already available as a global.
// Use pMyPkg to access your projects object's properties and methods.
}
At this point, the consumer can use the standard project objects that are part of a specific VSPackage to move down the hierarchy model. If you extend the Visual Basic code in the previous example, you can see how the consumer might obtain the automation object that contains any of your specialized methods and properties.
The following code example displays a Visual Studio macro for a project model:
Dim MyPrj As Project
Dim MyPrjItem As ProjectItem
Dim objMyObject as MyExtendedObject
MyPrj = MyProjects.Item(1) 'use the Projects collection to get a project
objMyObject = MyPrj.Object 'You call .Object to get to special Project
'implementation
objMyObject.MySpecialMethodOrProperty
The following code example displays a Visual Studio macro for Options pages on the Tools menu. This code lists the names of all of the properties in the Visual Studio environment General option on the Tools menu:
dim objDTE
dim objEnv
set objDTE = CreateObject("VisualStudio.DTE")
set objEnv = objDTE.Properties("Environment", "General")
for each obj in ObjEnv
MsgBox obj.Name
Next