Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attach a second process to VS2010 programmatically with .NET

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?

like image 748
kure Avatar asked May 16 '13 02:05

kure


1 Answers

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.

like image 75
Laurent LA RIZZA Avatar answered Sep 28 '22 06:09

Laurent LA RIZZA