Ejemplo n.º 1
0
        /// <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();
        }
Ejemplo n.º 2
0
        public void AttachToProcess(int processId)
        {
            var r = Package.GetGlobalService(typeof(SDTE)) as DTE;

            _attachedProcess = r.Debugger.LocalProcesses.Cast <EnvDTE.Process>().FirstOrDefault(process => process.ProcessID == processId);
            _attachedProcess.Attach();
        }
Ejemplo n.º 3
0
        public static void AttachVisualStudioToProcess(_DTE instanceSolution, string ProcessName)
        {
            Process[] prss = Process.GetProcessesByName(ProcessName);

            if (prss.Length <= 0)
            {
                MessageBox.Show("Failed to find w3wp.exe process! Make sure you opened the page first!", "Quick Attach", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            DTEProcess processToAttachTo = instanceSolution.Debugger.LocalProcesses.Cast <DTEProcess>().FirstOrDefault(process => process.ProcessID == prss[0].Id);

            try
            {
                if (processToAttachTo != null)
                {
                    processToAttachTo.Attach();
                }
                else
                {
                    MessageBox.Show("Failed to attach process to solution!", "Quick Attach", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            catch (SecurityException)
            {
                MessageBox.Show("Admin Permissions are required to attach the process", "Quick Attach", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// The method to use to attach visual studio to a specified process.
        /// </summary>
        /// <param name="visualStudioProcess">
        /// The visual studio process to attach to.
        /// </param>
        /// <param name="applicationProcess">
        /// The application process that needs to be debugged.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// Thrown when the application process is null.
        /// </exception>
        public static void AttachVisualStudioToProcess(Process visualStudioProcess, Process applicationProcess)
        {
            _DTE visualStudioInstance;

            if (TryGetVsInstance(visualStudioProcess.Id, out visualStudioInstance))
            {
                // Find the process you want the VS instance to attach to...
                DTEProcess processToAttachTo =
                    visualStudioInstance.Debugger.LocalProcesses.Cast <DTEProcess>()
                    .FirstOrDefault(process => 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 + "'");
                }
            }
        }
Ejemplo n.º 5
0
            internal static void AttachVisualStudioToProcess(Process visualStudioProcess, _DTE visualStudioInstance, Process applicationProcess)
            {
                DTEProcess processToAttachTo = visualStudioInstance.Debugger.LocalProcesses.Cast <DTEProcess>().FirstOrDefault(process => process.ProcessID == applicationProcess.Id);

                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 + "'");
                }
            }
Ejemplo n.º 6
0
 private bool TryAttachToProcess()
 {
     try
     {
         var r         = Package.GetGlobalService(typeof(SDTE)) as DTE;
         var processId = GetActiveServiceProcessId();
         _attachedProcess = r.Debugger.LocalProcesses.Cast <EnvDTE.Process>().FirstOrDefault(process => process.ProcessID == processId);
         _attachedProcess.Attach();
         return(true);
     }
     catch (Exception ex)
     {
         Message(string.Format("Can't attach to service process, exception message - {0}", ex.Message));
         return(false);
     }
 }
        public static void AttachVSToProcess(Process vsp, Process applicationProcess)
        {
            var vsi = getVSInstance(vsp.Id);

            if (vsi == null)
            {
                return;
            }
            //Find the process you want the VS instance to attach to...
            DTEProcess tp = vsi.Debugger.LocalProcesses.Cast <DTEProcess>().FirstOrDefault(process => process.ProcessID == applicationProcess.Id);

            //Attach to the process.
            if (tp != null)
            {
                tp.Attach();
                ShowWindow((int)vsp.MainWindowHandle, 3);
                SetForegroundWindow(vsp.MainWindowHandle);
            }
            else
            {
                throw new InvalidOperationException("Visual Studio process cannot find specified application '" + applicationProcess.Id + "'");
            }
        }
Ejemplo n.º 8
0
        public void AttachDebugger(int processId)
        {
            EnvDTE.Process process = GetDteProcess(processId);

            try
            {
                process.Attach();
            }
            catch (Exception e)
            {
                Debug.Fail("Exception: " + e.ToString());
                throw;
            }

            if (!m_attachedProcesses.ContainsKey(processId))
            {
                m_attachedProcesses.Add(processId, process);
            }
            else
            {
                Debug.Fail("AttachDebugger: was already attached to process with pid=" + processId.ToString(CultureInfo.InvariantCulture));
            }
        }