1

I am trying to get Visual Studio 2013 to programmatically attach the Debugger to a child process, which I start using the Process.Start method.

Process.Start(ProcessStartupInformation startupInfo)

var subProcess = Process.Start(startInfo);

// TODO: how to find the right VS instance?
var dte2 = (EnvDTE80.DTE2)Marshal.GetActiveObject("VisualStudio.DTE.12.0");
var debugger2 = (EnvDTE80.Debugger2)dte2.Debugger;
Process2 processToAttachTo = null;
if (debugger2.LocalProcesses != null)
{
    foreach (Process2 proc in debugger2.LocalProcesses)
    {
        if (proc.ProcessID == subProcess.Id)
        {
            processToAttachTo = proc;
        }
    }
 }

 if (processToAttachTo != null)
 {
      var trans = debugger2.Transports.Item("Default");
      var engine1 = trans.Engines.Item("Managed (v4.5, v4.0)");
      var engine2 = trans.Engines.Item("Native");
      var engines = new[] { engine1, engine2 };

      processToAttachTo.Attach2(engines);
  }

Unfortunately, this seems to crash the Debugger. However, if I use only one of the engines

      processToAttachTo.Attach2(engine1);

OR

      processToAttachTo.Attach2(engine2);

everything works fine.

In that scenario, I can only debug either my C++ child processes or my C# child processes. But not both.

If I try to get processToAttachTo.Transport.Engines and use those like so processToAttachTo.Attach2(processToAttachTo.Transport.Engines) the Debugger crashes as well and I get at Call was rejected by callee. (Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)) exception.

QUESTIONS:

  1. How do I use multiple engines to attach the Debugger?
  2. If it is not possible to attach multiple engines, how do I determine, which engine I should Attach?

Related Questions:

Further info:

http://msdn.microsoft.com/en-us/library/ms228772(v=vs.120).aspx

Community
  • 1
  • 1
froeschli
  • 2,692
  • 2
  • 28
  • 55
  • If all you actually want is to debug two processes simultaneously, can't you simply use multiple startup projects? http://stackoverflow.com/questions/3850019/running-two-projects-at-once-in-visual-studio/3850044 – stijn Jan 15 '14 at 14:49
  • What we are trying to achieve is having a launcher application, which can start other applications. In that way, we authenticate the user once in the launcher and all subsequent application calls will use the identified user... In other words, no I can't use multiple startup projects. – froeschli Jan 15 '14 at 14:55
  • aha. But in the end you just want to debug those launched applications, right? If so as a workaround (since I've no idea how to fix the above problem) you could e.g. pass an extra argument to launched application like 'debug' and issue a DebugBreak in the app when it sees that argument. Then you'll have the chance to attach the debugger.. Manually though.. – stijn Jan 15 '14 at 15:02
  • You are a bit quick to call an error return value a "crash". Getting "call rejected" is pretty normal, that happens when you slam VS too quickly with commands. Using CoRegisterMessageFilter() is a heavy-handed solution for that. Or just sleep for a bit and try again. What kind of "crash" you got the first time is unguessable. – Hans Passant Jan 15 '14 at 17:23
  • I simplified the code a little bit. I am looping this code up to 100 times or until I succeed in attaching the Debugger. In every cycle I sleep for 10ms. Is that too short? – froeschli Jan 16 '14 at 06:33

1 Answers1

2

A string[] works for me (VS 2017):

processToAttachTo.Attach2(new string[] {"Managed (v4.5, v4.0)", "Native"});
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569