Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8064577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:24:44+00:00 2026-06-05T11:24:44+00:00

I am coding up some design time code. I want to use this snippet:

  • 0

I am coding up some design time code. I want to use this snippet: (Found here)

var dte = (EnvDTE.DTE) GetService(typeof(EnvDTE.DTE));
if (dte != null)
{
    var solution = dte.Solution;
    if (solution != null)
    {
        string baseDir = Path.GetDirectoryName(solution.FullName);
    }
}

Problem is that this does not compile. (GetService is not a known method call) I tried adding Microsoft.VisualStudio.Shell (and Microsoft.VisualStudio.Shell.10.0) but it did not help.

In looking around on the internet I found that you need a IServiceProvider to call this.

But all the examples that show how to get an IServiceProvider use a EnvDTE.

So, to get the current EnvDTE I need IServiceProvider. But to get an IServiceProvider I need an EnvDTE. (There is a hole in my bucket…)

So, here is my question:

In a normal WPF Application, how can I get the current instance of EnvDTE?

NOTE: I am not looking for any old instance of EnvDTE. I need the one for my current Visual Studio instance (I run 3-4 instances of Visual Studio at a time.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T11:24:46+00:00Added an answer on June 5, 2026 at 11:24 am

    This question has the answer to which you’re looking.

    Get the reference of the DTE2 object in Visual C# 2010

    Specifically

    https://stackoverflow.com/a/4724924/858142

    Here is the code:

    Usings:

    using System;
    using System.Runtime.InteropServices;
    using System.Runtime.InteropServices.ComTypes;
    using EnvDTE;
    using Process = System.Diagnostics.Process;
    

    Method:

    [DllImport("ole32.dll")]
    private static extern void CreateBindCtx(int reserved, out IBindCtx ppbc);
    [DllImport("ole32.dll")]
    private static extern void GetRunningObjectTable(int reserved,
                                                     out IRunningObjectTable prot);
    internal static DTE GetCurrent()
    {
       //rot entry for visual studio running under current process.
       string rotEntry = String.Format("!VisualStudio.DTE.10.0:{0}",
                                        Process.GetCurrentProcess().Id);
       IRunningObjectTable rot;
       GetRunningObjectTable(0, out rot);
       IEnumMoniker enumMoniker;
       rot.EnumRunning(out enumMoniker);
       enumMoniker.Reset();
       IntPtr fetched = IntPtr.Zero;
       IMoniker[] moniker = new IMoniker[1];
       while (enumMoniker.Next(1, moniker, fetched) == 0)
       {
           IBindCtx bindCtx;
           CreateBindCtx(0, out bindCtx);
           string displayName;
           moniker[0].GetDisplayName(bindCtx, null, out displayName);
           if (displayName == rotEntry)
           {
               object comObject;
               rot.GetObject(moniker[0], out comObject);
               return (DTE)comObject;
           }
       }
       return null;
    }
    

    As the other answer indicates, this does not work while debugging.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across some code recently that replaces the use of switches by hard-coding
I am currently coding some chat program and now want to play music via
I've noticed in some coding people use icicle with the onCreate method, and I
I'm trying to save myself some coding time by taking advantage of using namespace
A long shot but here's hoping someone has some experience coding PHP hooks for
some code snippets. The java coding doing the jaxb unmarshaling. pretty straightforward, copied out
When coding and reviewing code, it's easy to spot places where a design pattern
We are currently going through the long process of writing some coding standards for
I'm doing some JAVA coding at home and at work. At home i have
I'm someone that used to do some J2EE coding in the past and I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.