This article shows you how to get control of the currently running Visual Studio IDE instance. You can get a reference to the DTE object - the top-level object in the Visual Studio automation object model. Once you have a reference to this object, you can query for information about a solution, projects in a solution, start builds, etc.
My code is a very simple library, the point is to demonstrate it using the DTE object. For more detailed automation examples, you can look at the MSDN documentation, but this article should help you get started with a reference to the object model.
The DTE Object
DTE stands for "Development Tools Extensibility" and is the object's codeclass. It implements the interface "
_DTE". You can find more information about the DTE object in the Visual Studio documentation under "DTE object", and in the document "Referencing the DTE object".
Using the Code
The file library contains a class
VisualStudioIdea which defines a few of the relevant methods. The first thing we need is to be able to get at the system's Running Object Table. To do this, we'll need access to a couple of functions that live in ole32.dll:
[DllImport("ole32.dll", EntryPoint = "GetRunningObjectTable")]
static extern uint GetRunningObjectTable(uint res, out UCOMIRunningObjectTable ROT);
[DllImport("ole32.dll", EntryPoint = "CreateBindCtx")]
static extern uint CreateBindCtx(uint res, out UCOMIBindCtx ctx);
Now you are able to use
public static bool Connect()
public static Project GetCurrentProject()
public static bool Disconnect()
This is a first raw version of this library born around March 2007.
About Elia Conchione
Elia Conchione is a software engineer living in Lugano, CH. Current programming interests include C, C++, C#, Linux, Unix, Mac OS X, Safe Code, encryption applications.