Click here to Skip to main content
15,390,185 members
Articles / Programming Languages / C#
Posted 23 Apr 2007


24 bookmarked

Help To Use EnvDTE - A Little Simple Library

Rate me:
Please Sign up or sign in to vote.
3.60/5 (36 votes)
23 Apr 2007CPOL1 min read
A piece of library that can help to use Visual Studio Editor to search, write, change code directly in the code
Screenshot - screenshot.jpg


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 Connect:

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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Elia Conchione
Software Developer (Senior)
Italy Italy
Elia Conchione is a Banking software engineer from 1986, living in Cittadella, Padova, Italy. Current programming interests include C, C++, C#, Linux, Unix, Mac OS X, Safe Code, encryption applications.

Comments and Discussions

GeneralSmall change suggested Pin
Russ Suter10-May-08 10:45
MemberRuss Suter10-May-08 10:45 
Very nicely done but I found one minor issue. If you have multiple instances if the Visual Studio IDE running it will return the first instance and not necessarily the instance you're currently in. I was able to resolve this by adding a single line of code to the GetCurrentDTE() method. Change the following code...

// found?<br />
if (Name.IndexOf(dteName) != -1)<br />
{<br />
    object temp;<br />
    rot.GetObject(aMons[i], out temp);<br />
    dte = (EnvDTE.DTE)temp;<br />
    break;<br />

to this:

// found?<br />
if (Name.IndexOf(dteName) != -1)<br />
{<br />
    object temp;<br />
    rot.GetObject(aMons[i], out temp);<br />
    dte = (EnvDTE.DTE)temp;<br />
    if (dte.MainWindow.Caption == currentProcess.MainWindowTitle)<br />
        break;<br />

This will add a simple check that will determine if the main window caption of the DTE matches that of the current process. That way you can be assured that the DTE refers to the calling process and not another instance that may be running. Unless you happen to have 2 windows open with the same caption which is unlikely but possible. I'm looking into another way to make the comparison that may be more reliable but until then this fix did the trick for me.

Kudos to the author though for putting in so much effort. It really helped me in an issue that I've been struggling with for a week.
GeneralRe: Small change suggested Pin
dfjklsdfjkljklm8-Jan-09 21:28
Memberdfjklsdfjkljklm8-Jan-09 21:28 
QuestionLicense?? Pin
Coolite9-May-08 14:06
MemberCoolite9-May-08 14:06 
Generalvery good Pin
laleh.rajabi1-Mar-08 1:14
Memberlaleh.rajabi1-Mar-08 1:14 
GeneralGreate !!! Pin
Abhijit Jana5-Dec-07 20:44
professionalAbhijit Jana5-Dec-07 20:44 
GeneralGood! Pin
montanacarlo25-Apr-07 22:30
Membermontanacarlo25-Apr-07 22:30 
GeneralExamples Pin
Priyank Bolia24-Apr-07 6:52
MemberPriyank Bolia24-Apr-07 6:52 
GeneralRe: Examples Pin
Elia Conchione25-Apr-07 20:50
MemberElia Conchione25-Apr-07 20:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.