Attaching to Specific Instances of the IDE

Switch View :
ScriptFree
Visual Studio 2010
Attaching to Specific Instances of the IDE

There are two ways to attach to a specific instance of the Visual Studio integrated development environment (IDE) when multiple instances are running.

Both of these methods use monikers. A moniker is a name that is bound to an object — in this case, the IDE — that can be used to locate, activate, and access the bound object. You do not need specific information about the location of the actual object. In this regard, it works like a shortcut does with a program in Windows.

There are two ways to attach to a specific instance of the IDE:

  • Use the solution file path moniker.

  • Use the item moniker.

Solution Path Moniker

If the instance of the Visual Studio IDE to which you want to attach has an open solution, then you can attach to it by using the solution file path moniker. That file moniker is registered in the running object table (ROT) with the solution object for that file. Use Solution.DTE to get to that object.

Item Moniker

Visual Studio also registers a ProgID as an item moniker in the ROT. The ProgID is comprised of the name and process ID of the DTE process. So, for example, the object's ROT entry might be "!VisualStudio.DTE.10.0:1234," where 1234 is the process ID.

See Also

Tasks

Other Resources

Community Content

iwantedue
Using the Item Moniker

I tried passing the Item Moniker "!VisualStudio.DTE.10.0:1234" to Marshal.GetActiveObject but it wouldn't work. Here is the code to enumerate the ROT (Running Object Table) and find the right entry.

[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);
public static DTE2 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 (EnvDTE80.DTE2)comObject;
        }
    }
    return null;
}