Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A coworker ended up with a solution using DTE, and I posted the code on PasteBin.</p> <p>The methods of interest are <code>AttachVisualStudioToProcess</code> and <code>TryGetVsInstance</code></p> <p><a href="http://pastebin.com/KKyBpWQW" rel="nofollow noreferrer">Source Code</a></p> <pre><code>public static void AttachVisualStudioToProcess(Process visualStudioProcess, Process applicationProcess) { _DTE visualStudioInstance; if (TryGetVsInstance(visualStudioProcess.Id, out visualStudioInstance)) { //Find the process you want the Visual Studio instance to attach to... DTEProcess processToAttachTo = visualStudioInstance.Debugger.LocalProcesses.Cast&lt;DTEProcess&gt;().FirstOrDefault(process =&gt; process.ProcessID == applicationProcess.Id); // Attach to the process. if (processToAttachTo != null) { processToAttachTo.Attach(); ShowWindow((int)visualStudioProcess.MainWindowHandle, 3); SetForegroundWindow(visualStudioProcess.MainWindowHandle); } else { throw new InvalidOperationException("Visual Studio process cannot find specified application '" + applicationProcess.Id + "'"); } } } private static bool TryGetVsInstance(int processId, out _DTE instance) { IntPtr numFetched = IntPtr.Zero; IRunningObjectTable runningObjectTable; IEnumMoniker monikerEnumerator; IMoniker[] monikers = new IMoniker[1]; GetRunningObjectTable(0, out runningObjectTable); runningObjectTable.EnumRunning(out monikerEnumerator); monikerEnumerator.Reset(); while (monikerEnumerator.Next(1, monikers, numFetched) == 0) { IBindCtx ctx; CreateBindCtx(0, out ctx); string runningObjectName; monikers[0].GetDisplayName(ctx, null, out runningObjectName); object runningObjectVal; runningObjectTable.GetObject(monikers[0], out runningObjectVal); if (runningObjectVal is _DTE &amp;&amp; runningObjectName.StartsWith("!VisualStudio")) { int currentProcessId = int.Parse(runningObjectName.Split(':')[1]); if (currentProcessId == processId) { instance = (_DTE)runningObjectVal; return true; } } } instance = null; return false; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload