Attach a second process to VS2010 programmatically with .NET

7

I have a solution in visual studio 2010 that contains 2 projects, one is a C# console application, which I will refer to as Foo, and the other is a CLR C++ console application, which I will refer to as Bar. Bar is an exe not a dll. When the Debug session starts is starts by running Foo. Foo starts Bar’s Process by using the code System.Diagnostics.Process procBar = System.Diagnostics.Process.Start(pathToBarEXE) I want to then attach that Bar.exe to the currently running debugger programmatically. I have a function that is supposed to do it (seen it everywhere on the internet)

public void AttachToProcess(int processId)
    {
        foreach (EnvDTE.Process process in DTE.Debugger.LocalProcesses)
        {
            if (process.ProcessID == processId)
            {
                process.Attach();
                DTE.Debugger.CurrentProcess = process;
            }
        }
    }

but the function does not compile, as DTE is an interface. I have modified the function thusly

public void AttachToProcess(int processId)
    {
        EnvDTE80.DTE2 dte2;
        dte2 = (EnvDTE80.DTE2)System.Runtime.InteropServices.Marshal.GetActiveObject("VisualStudio.DTE.10.0");
        foreach (EnvDTE.Process process in dte2.Debugger.LocalProcesses)
        {
            if (process.ProcessID == processId)
            {
                process.Attach();
            }
        }
    }

and it compiles and runs, but when it finds Bar’s process, the if statement throws the exception The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)) What am I doing wrong here?

c#
c++
.net
visual-studio-2010
debugging
asked on Stack Overflow May 16, 2013 by kure

1 Answer

1

They say here:

http://msdn.microsoft.com/en-us/library/ms228772.aspx

that it is because COM has some problems dealing with multithreaded applications. They are presenting a sample code, inviting you to use an IOleMessageFilter to retry the call when you get rejected like this.

answered on Stack Overflow Oct 2, 2013 by Laurent LA RIZZA

User contributions licensed under CC BY-SA 3.0