【问题标题】:Attaching to Specific Instances of the IDE附加到 IDE 的特定实例
【发布时间】:2012-07-29 14:38:19
【问题描述】:

当我这样做时:

// 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

我能够获取当前 ide 的实例

不过,我希望获得对不同 Visual Studio 实例的 dte2 的引用。 This link 表示可以这样做。结果我尝试了类似的东西:

    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); 

如果我尝试得到异常:

类字符串无效(HRESULT 异常:0x800401F3 (CO_E_CLASSSTRING))

还有更多链接显示如何执行我正在寻找的内容,但由于某种原因我无法使其工作。以下是一些链接:

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

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

【问题讨论】:

  • 有没有其他人注意到“GetActiveObject”并不总是返回当前的 VS 实例,而是返回当前打开的实例集中首先打开的实例。

标签: c# visual-studio-2010 ide macros envdte


【解决方案1】:

一直对我有用的一件事是

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;
    }
}

【讨论】:

  • 不错的解决方案,不需要 p/invoking。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 1970-01-01
相关资源
最近更新 更多