/// <summary> /// Attaches Visual Studio (2012) to the specified process. /// </summary> /// <param name="process">The process.</param> public static void Attach(this System.Diagnostics.Process process) { // Reference visual studio core DTE dte = (DTE)Marshal.GetActiveObject(@"VisualStudio.DTE.11.0"); // Register the IOleMessageFilter to handle any threading errors. MessageFilter.Register(); // Try loop - visual studio may not respond the first time. int tryCount = 50; while (tryCount-- > 0) { try { EnvDTE.Processes processes = dte.Debugger.LocalProcesses; Process DTEprocess = processes.Cast <Process>().Where( proc => proc.ProcessID == process.Id).First(); DTEprocess.Attach(); break; } catch (COMException) { System.Threading.Thread.Sleep(500); } } // and turn off the IOleMessageFilter. MessageFilter.Revoke(); }
public override void Execute() { DTE service = (DTE)this.GetService(typeof(DTE)); try { EnvDTE.Processes processes = service.Debugger.LocalProcesses; foreach (EnvDTE.Process proc in processes) { if (proc.ProcessID == ProcessID) { Helpers.LogMessage(service, this, "Attaching to process " + proc.Name + " (" + ProcessID.ToString() + ")"); proc.Attach(); } } } catch (Exception ex) { MessageBox.Show("Process not found. Maybe the process is not started"); Helpers.LogMessage(service, this, ex.ToString()); } }