Question

I'm trying to programmatically attach to an IISExpress process for debugging. When using the Visual Studio attach to process window I have to select Managed Code as the type to attach to. If auto detect is selected instead, VS tries to debug IISExpress as an x86/native application which does not work.

The following code (from this answer) using DTE attaches the debugger but does not appear to attach it as managed code.

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 + "'");
        }
    }
}

Perhaps the DTE library is incapable of suggesting that the debugger should attach to managed code?

Was it helpful?

Solution

You need to use the Process2.Attach2() method instead, available since VS2005. It takes a debugger engine argument that specifies the kind of debugger you want to use.

Sample code is here.

OTHER TIPS

Working code taken and modified from Hans' answer:

    public static void AttachVisualStudioToProcess(Process visualStudioProcess, Process applicationProcess)
    {
        _DTE visualStudioInstance;

        if (TryGetVsInstance(visualStudioProcess.Id, out visualStudioInstance))
        {

            EnvDTE100.Debugger5 dbg5 = (EnvDTE100.Debugger5)visualStudioInstance.Debugger;
            EnvDTE80.Transport trans = dbg5.Transports.Item("Default");
            EnvDTE80.Engine dbgeng;
            dbgeng = trans.Engines.Item("Managed (v4.5, v4.0)");
            var proc2 = (EnvDTE80.Process2)dbg5.GetProcesses(trans, "WIN-86CEJEGQCPD").Item("iisexpress.exe");
            proc2.Attach2(dbgeng);


        }
    }

I added references to EnvDTE100, EnvDTE90, EnvDTE90a and EnvDTE80 but I suspect if you just stick to debugger2 rather than debugger5 you would only need EnvDTE80.

You can do this in your code.

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());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top