5
votes

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?

1
It doesn't help solve your problem in the long term, but Mashal.GetActiveObject doesn't exist in .NET Core, because it is a .NET Framework, Xamarin and Mono specific API - presumably because it makes Windows specific API calls under the hood. - Jamie Taylor
Yeh I figured as much - just wondered if there is a different way or maybe a nuget package I can bring in. Already checked the Windows Compatibility Pack - Tom
If you need to use .NET Framework APIs, change the application so that it uses .NET Framework. - Jamie Taylor

1 Answers

5
votes

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);