ClrMD - AttachToProcess Using AttachFlag.Invasive and AttachFlag.NonInvasive Crashing

0

I'm using the Microsoft.Diagnostics.Runtime nuget package and this is my code as I attempt to get the stacktrace:

var pid = Process.GetCurrentProcess().Id;
// Line of error
using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Invasive))
{
    ClrInfo currentRuntime = dataTarget.ClrVersions[0];
    var runtime = currentRuntime.CreateRuntime();
    foreach (var t in runtime.Threads)
    {
        MessageBox.Show("Got here");
        t.StackTrace
    }
}

Question is similar to Attach to self with ClrMD? HRESULT: 0x80070057 but I go a step further and build the application using Wix. Then, I install the application on my desktop so it is running without Visual Studio and its debugger.

The messagebox doesn't show, as long as I put it after the line using (var dataTarget = DataTarget.AttachToProcess(pid, 5000, AttachFlag.Invasive)). If I put the messagebox before, the messagebox does show.

In the code, I get the error

Microsoft.Diagnostics.Runtime.ClrDiagnosticsException: 'Could not attach to pid 624, HRESULT: 0x80070057'

I think I understand why AttachFlag.Invasive doesn't work when I run the application in Visual Studio because it's being debugged, but I don't understand why that line doesn't work after I build it with Wix and install it on my Desktop.

Again, as with the attached Stackoverflow post, AttachFlag.Invasive and AttachFlag.NonInvasive don't work, but AttachFlag.Passive does work.

c#
wpf
process
clrmd
asked on Stack Overflow Jan 8, 2019 by Thomas • edited Jan 8, 2019 by Thomas

1 Answer

0

Like my answer in: https://stackoverflow.com/a/57790758/7122901

You can use DataTarget.CreateSnapshotAndAttach. This method creates a snapshot of the process and create DataTarget from it. Example:

var processId = Process.GetCurrentProcess().Id;

using (var dataTarget = DataTarget.CreateSnapshotAndAttach(processId))
{
}
answered on Stack Overflow Sep 5, 2019 by itaiy

User contributions licensed under CC BY-SA 3.0