22

I'd like to be able to automatically attach a debugger, something like: System.Diagnostics.Debugger.Launch(), except rather than the current process to another named process. I've got a process name and PID to identify the other process.

Is this possible?

Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
  • 2
    Take a look http://stackoverflow.com/questions/422768/attaching-to-a-child-process-automatically-in-visual-studio-during-debugging – GSerjo Aug 04 '12 at 20:55
  • @GSerjo is that a Visual Studio macro? What is DTE? – Dave Hillier Aug 04 '12 at 21:00
  • 1
    Yes, it's macro. DTE object represents the Visual Studio integrated development environment (IDE) and is the highest level object in the automation model hierarchy – GSerjo Aug 04 '12 at 21:04

5 Answers5

30

Edit: GSerjo offered the correct solution. I'd like to share a few thoughts on how to improve it (and an explanation). I hope my improved answer will be useful to to others who experience the same problem.


Attaching the VS Debugger to a Process

Manually

  1. Open the Windows Task Manager (Ctrl + Shift + Esc).
  2. Go to the Tab Processes.
  3. Right click the process.
  4. Select Debug.

Or, within Visual Studio, select Debug > Attach to Process....

Results will vary depending on whether you have access to the source code.

Automatically with C#

A note of caution: The following code is brittle in the sense that certain values, such as the Visual Studio Version number, are hard-coded. Keep this in mind going forward if you are planning to distribute your program.

First of all, add a reference to EnvDTE to your project (right click on the references folder in the solution explorer, add reference). In the following code, I'll only show the unusual using directives; the normal ones such as using System are omitted.

Because you are interacting with COM you need to make sure to decorate your Main method (the entry point of your application) with the STAThreadAttribute.

Then, you need to define the IOleMessageFilter Interface that will allow you to interact with the defined COM methods (note the ComImportAttribute). We need to access the message filter so we can retry if the Visual Studio COM component blocks one of our calls.

using System.Runtime.InteropServices;

[ComImport, Guid("00000016-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleMessageFilter
{
    [PreserveSig]
    int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);

    [PreserveSig]
    int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);

    [PreserveSig]
    int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}

Now, we need to implement this interface in order to handle incoming messages:

public class MessageFilter : IOleMessageFilter
{
    private const int Handled = 0, RetryAllowed = 2, Retry = 99, Cancel = -1, WaitAndDispatch = 2;

    int IOleMessageFilter.HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
    {
        return Handled;
    }

    int IOleMessageFilter.RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
    {
        return dwRejectType == RetryAllowed ? Retry : Cancel;
    }

    int IOleMessageFilter.MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
    {
        return WaitAndDispatch;
    }

    public static void Register()
    {
        CoRegisterMessageFilter(new MessageFilter());
    }

    public static void Revoke()
    {
        CoRegisterMessageFilter(null);
    }

    private static void CoRegisterMessageFilter(IOleMessageFilter newFilter)
    {
        IOleMessageFilter oldFilter;
        CoRegisterMessageFilter(newFilter, out oldFilter);
    }

    [DllImport("Ole32.dll")]
    private static extern int CoRegisterMessageFilter(IOleMessageFilter newFilter, out IOleMessageFilter oldFilter);
}

I defined the return values as constants for better readability and refactored the whole thing a bit to get rid of some of the duplication from the MSDN example, so I hope you'll find it self-explanatory. extern int CoRegisterMessageFilter is our connection to the unmanaged message filter code - you can read up on the extern keyword at MSDN.

Now all that's left is some code illustrating the usage:

using System.Runtime.InteropServices;
using EnvDTE;

[STAThread]
public static void Main()
{
    MessageFilter.Register();
    var process = GetProcess(7532);
    if (process != null)
    {
        process.Attach();
        Console.WriteLine("Attached to {0}", process.Name);
    }
    MessageFilter.Revoke();
    Console.ReadLine();
}

private static Process GetProcess(int processID)
{
    var dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.10.0");
    var processes = dte.Debugger.LocalProcesses.OfType<Process>();
    return processes.SingleOrDefault(x => x.ProcessID == processID);
}
Community
  • 1
  • 1
Adam
  • 15,537
  • 2
  • 42
  • 63
  • No, that's not what I need. Like I said, I want an equivalent of Debugger.Launch except for another process. – Dave Hillier Aug 04 '12 at 20:53
  • It seems he wants to do it programmatically however. – Cole Tobin Aug 04 '12 at 20:53
  • @DaveHillier What would be the difference between C# and plain Task Manager? – Cole Tobin Aug 04 '12 at 20:53
  • @ColeJohnson: I've got a number of services to manage. It's useful to me to be able to deploy and then subsequently debug from a management app. I've already got the services to install, stop and start. – Dave Hillier Aug 04 '12 at 20:57
  • The same problem "Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)". COM is very sensitive, so added [STAThread], then Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED) is appeared. Looks like solution is here http://msdn.microsoft.com/en-us/library/ms228772(v=vs.90).aspx – GSerjo Aug 04 '12 at 22:16
  • @DaveHillier I gave it another try, and now it works for me. I added an explanation while I was at it, though you've probably figured it out in the meantime. – Adam Aug 05 '12 at 10:28
  • 1
    Works really nicely - bump the ProgId to 11 for VS2012 – Dave Hillier Aug 05 '12 at 12:46
  • +1 This can be used in conjunction with Process.Start() and Debugger.IsAttached in the spawning process! Great work. – vincent gravitas Jun 08 '13 at 23:42
  • If i debug the phase of debugging attachment it work. But if i try to run till i placed the breakpoint i get RPC_E_CALL_REJECTED 0x80010001 – Skary Dec 08 '15 at 17:50
  • @codesparkle Worked great!! Thanks. +1 for the detailed answer. – vendettamit Jan 28 '16 at 19:45
13

Check this out

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using EnvDTE;
using NUnit.Framework;

namespace UnitTests
{
    [TestFixture]
    public class ForTest
    {
        [STAThread]  
        [Test]
        public void Test()
        {
            var dte = (DTE) Marshal.GetActiveObject("VisualStudio.DTE.10.0");
            MessageFilter.Register();

            IEnumerable<Process> processes = dte.Debugger.LocalProcesses.OfType<Process>();
            var process = processes.SingleOrDefault(x => x.ProcessID == 6152);
            if (process != null)
            {
                process.Attach();
            }
        }
    }

    public class MessageFilter : IOleMessageFilter
    {
        //
        // Class containing the IOleMessageFilter
        // thread error-handling functions.

        // Start the filter.

        //
        // IOleMessageFilter functions.
        // Handle incoming thread requests.

        #region IOleMessageFilter Members

        int IOleMessageFilter.HandleInComingCall(int dwCallType,
                                                 IntPtr hTaskCaller, int dwTickCount, IntPtr
                                                                                          lpInterfaceInfo)
        {
            //Return the flag SERVERCALL_ISHANDLED.
            return 0;
        }

        // Thread call was rejected, so try again.
        int IOleMessageFilter.RetryRejectedCall(IntPtr
                                                    hTaskCallee, int dwTickCount, int dwRejectType)
        {
            if (dwRejectType == 2)
            // flag = SERVERCALL_RETRYLATER.
            {
                // Retry the thread call immediately if return >=0 & 
                // <100.
                return 99;
            }
            // Too busy; cancel call.
            return -1;
        }

        int IOleMessageFilter.MessagePending(IntPtr hTaskCallee,
                                             int dwTickCount, int dwPendingType)
        {
            //Return the flag PENDINGMSG_WAITDEFPROCESS.
            return 2;
        }

        #endregion

        public static void Register()
        {
            IOleMessageFilter newFilter = new MessageFilter();
            IOleMessageFilter oldFilter = null;
            CoRegisterMessageFilter(newFilter, out oldFilter);
        }

        // Done with the filter, close it.
        public static void Revoke()
        {
            IOleMessageFilter oldFilter = null;
            CoRegisterMessageFilter(null, out oldFilter);
        }

        // Implement the IOleMessageFilter interface.
        [DllImport("Ole32.dll")]
        private static extern int
            CoRegisterMessageFilter(IOleMessageFilter newFilter, out
                                                                     IOleMessageFilter oldFilter);
    }

    [ComImport, Guid("00000016-0000-0000-C000-000000000046"),
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface IOleMessageFilter
    {
        [PreserveSig]
        int HandleInComingCall(
            int dwCallType,
            IntPtr hTaskCaller,
            int dwTickCount,
            IntPtr lpInterfaceInfo);

        [PreserveSig]
        int RetryRejectedCall(
            IntPtr hTaskCallee,
            int dwTickCount,
            int dwRejectType);

        [PreserveSig]
        int MessagePending(
            IntPtr hTaskCallee,
            int dwTickCount,
            int dwPendingType);
    }
}
GSerjo
  • 4,725
  • 1
  • 36
  • 55
  • 2
    The [`STAThread Attribute`](http://msdn.microsoft.com/en-us/library/system.stathreadattribute.aspx#remarksToggle) is *useless* on all methods except the entry point (probably `Main`). – Adam Aug 05 '12 at 07:24
  • Duuuuude. Where were you :) – Emad Jan 04 '18 at 10:33
  • @Emad, always here, even when I'm there, I'm still here :) – GSerjo Jan 04 '18 at 17:55
9

Simpler way of doing it.

public static void Attach(DTE2 dte)
        {
            var processes = dte.Debugger.LocalProcesses;
            foreach (var proc in processes.Cast<EnvDTE.Process>().Where(proc => proc.Name.IndexOf("YourProcess.exe") != -1))
                proc.Attach();
        }

        internal static DTE2 GetCurrent()
        {
            var dte2 = (DTE2)Marshal.GetActiveObject("VisualStudio.DTE.12.0"); // For VisualStudio 2013

            return dte2;
        }

Usage:

Attach(GetCurrent());
Alpesh
  • 606
  • 6
  • 15
  • Where do you get the DTE2 and EnvDTE definitions from? – user626528 Dec 30 '17 at 22:40
  • You need to add reference to EnvDTE80 (References > Add references > Assemblies > Framework) and then use it in your class `using EnvDTE80;` – Alpesh Jan 02 '18 at 09:27
  • Apparently, it requires both EnvDTE80 and EnvDTE and these are located in References > Add references > Assemblies > Extensions (at least in VS2017). Not sure what version number to use, as "VisualStudio.DTE.14.0" fails with a `Invalid class string` exception. – user626528 Jan 02 '18 at 21:42
  • 1
    I am using VS2013 so can't help on that but this [post](https://www.helixoft.com/blog/creating-envdte-dte-for-vs-2017-from-outside-of-the-devenv-exe.html) may help. Good luck. – Alpesh Jan 04 '18 at 09:07
  • So it was 15.0, not 14.0. Worked like a charm. – user626528 Jan 04 '18 at 17:06
  • One problem I've stumbled upon so far: `Marshal.GetActiveObject` returns a reference to the first opened VS instance rather than the _active_ one. – user626528 Jan 25 '18 at 21:14
4

An option is to run; vsjitdebugger.exe -p ProcessId

It may be possible to use Process.Start to do this within a c# app.

Dave Hillier
  • 18,105
  • 9
  • 43
  • 87
1

If you have troubles with attaching debugger to process that is too quick to attach manually, don't forget you can sometimes add Console.ReadKey(); to a first line of your code and then you have all the time you need to attach it manually. Surprisingly it took me a while to figure that one out :D