0

when I do:

// Get an instance of the currently running Visual Studio IDE.
EnvDTE80.DTE2 dte2;
dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
GetActiveObject("VisualStudio.DTE.10.0");

var solution = dte2.Solution; // get solution

Console.WriteLine(solution.FullName);  // prints the name of the solution where this code is written

I am able to get an instance of the current ide

I will like to get a reference to dte2 of a different visual studio instance though. This link states that it is possible to do that. As a result I have tried something like:

    Process p = new Process();
    ProcessStartInfo ps = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe");
    p.StartInfo = ps;
    p.Start(); // start a new instance of visual studio

    var ROT = "!VisualStudio.DTE.10.0:" + p.Id;

    // Get an instance of the NEW instance of Visual Studio IDE.
    EnvDTE80.DTE2 dte2;
    dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.
    GetActiveObject(ROT); 

If I try that I get the exception:

Invalid class string (Exception from HRESULT: 0x800401F3 (CO_E_CLASSSTRING))

There are more links that show how to do what am I looking for but for some reason I cannot make it work. Here are some of the links:

http://msdn.microsoft.com/en-us/library/6cefss65.aspx

http://msdn.microsoft.com/en-us/library/ms228755.aspx

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • Has anyone else noticed how 'GetActiveObject' does not always return the current instance of VS but rather the instance, out of the current set of open instances, that was opened first. – Russell Horwood Jan 13 '15 at 12:03

1 Answers1

3

One thing that has worked for me consistently is

var dte = GetDTE();
var debugger = dte.Debugger;
var processes = debugger.LocalProcesses;
int processIdToAttach; //your process id of second visual studio
foreach (var p in processes)
{
    EnvDTE90.Process3 process3 = (EnvDTE90.Process3)p;
    if (process3.ProcessID == processIdToAttach)
    {
        if (!process3.IsBeingDebugged)
        {
            if (doMixedModeDebugging)
            {
                string[] arr = new string[] { "Managed", "Native" };
                process3.Attach2(arr);
            }
            else
            {
                process3.Attach();
            }
        }
        break;
    }
}
Eduard Malakhov
  • 1,095
  • 4
  • 13
  • 25
parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85