5

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?

Tom
  • 623
  • 8
  • 23
  • 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](https://apisof.net/catalog/System.Runtime.InteropServices.Marshal.GetActiveObject(String)) - presumably because it makes Windows specific API calls under the hood. – Jamie Taylor Feb 06 '18 at 09:46
  • 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](https://www.nuget.org/packages/Microsoft.Windows.Compatibility) – Tom Feb 06 '18 at 10:02
  • If you need to use .NET Framework APIs, change the application so that it uses .NET Framework. – Jamie Taylor Feb 06 '18 at 10:13

1 Answers1

5

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);
Andrew Hanlon
  • 7,271
  • 4
  • 33
  • 53