Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dotnet core 2.0 - programmatically attach the VS2017 debugger to a process

In my dotnet core 2.0 application I re-launch the same application in a different process (with some different arguments) at a certain point. I want to be able to programmatically attach the current Visual Studio (2017) debugger to the new process.

Here is an example of how it is done in full framework but for starters the Marshal.GetActiveObject method doesn't appear to exist.

Is there a different way to achieve this in dotnet core 2.0? Or is it just not possible?

like image 263
Tom Avatar asked Feb 06 '18 09:02

Tom


People also ask

How to attach a process to Visual Studio for debugging?

You can attach the Visual Studio debugger to a running process on a local or remote computer. After the process is running, select Debug > Attach to Process or press Ctrl+Alt+p in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.

How do I debug in .NET core?

Set a breakpointTo set the breakpoint, click in the gutter to the left of the doWork function (or select the line of code and press F9). The breakpoint is set to the left of the opening brace ( { ). Now press F5 (or choose Debug > Start Debugging).

How to debug with IIS Express?

To start debugging, select IIS Express (<Browser name>) or Local IIS (<Browser name>) in the toolbar, select Start Debugging from the Debug menu, or press F5. The debugger pauses at the breakpoints. If the debugger can't hit the breakpoints, see Troubleshoot debugging.

How to debug the ASP net Application after deployment?

To debug an ASP.NET application that has been deployed to IIS, install and run the remote tools on the computer where you deployed your app, and then attach to your running app from Visual Studio.


1 Answers

While this is changing, .Net Core was envisioned to be as cross-platform as possible and originally left out many 'windows-only' methods. That said, you can still call into the underlying windows functions using P/Invoke:

[DllImport("oleaut32.dll", PreserveSig = false)]
static extern void GetActiveObject(ref Guid rclsid, IntPtr pvReserved,
    [MarshalAs(UnmanagedType.IUnknown)] out object ppunk);

[DllImport("ole32.dll")]
static extern int CLSIDFromProgID(
    [MarshalAs(UnmanagedType.LPWStr)] string lpszProgID, out Guid pclsid);
....

// Replace XX with the correct version
CLSIDFromProgID($"VisualStudio.DTE.XX.0", out var classId); 
GetActiveObject(ref classId, default, out dte);
like image 80
Andrew Hanlon Avatar answered Sep 20 '22 13:09

Andrew Hanlon