Attach from a Classlibrary to an External application (Visual Studio)

Asked

Viewed 25 times

0

I have a project that is a Classlibrary and this DLL runs inside a Console Application, I have a requirement here where the developers of this Classlibrary need to debug this DLL with the application, in the properties of the project I have configured the path of the EXE, runs normally but breakpoints do not work.

  • Visual Studio running as administrator
  • Updated dlls in executable folder

enter image description here

However, if I run the application manually (in the same path I set in the image above) and go through the Attach to Process option the breakpoints work

I also tried to copy the DLL and PDB after Build to ensure when I have changes the program will have the recent version (otherwise the attach does not work properly). What it seems to me here is that I need a kind of "delay" to make the attach.

enter image description here

1 answer

0


After some researches I decided to create a Helper for this

What I did was create a new application called Debuggerhelper, this application runs my main process and automatically attaches to it.

So I added this Helper to the Debug property of my Classlibrary Project and added the file as part of the project. Now I run my DLL with F5 and the debug works perfectly.

More information and details below:

enter image description here

private static void AttachProcess()
{
    var localByName = System.Diagnostics.Process.GetProcessesByName(_appName);

    MessageFilter.Register();
    var process = GetProcess(localByName[0].Id);
    if (process != null)
    {
        process.Attach();
        Console.WriteLine("Attached to {0}", process.Name);
    }
    MessageFilter.Revoke();
}

private static void StartProcess()
{
    System.Diagnostics.Process.Start("start.bat");

    Console.WriteLine("Waiting to load the process...");
    System.Threading.Thread.Sleep(3000);
}

private static Process GetProcess(int processId)
{
    // Visual Studio 2017 (15.0)
    var dte = (DTE)Marshal.GetActiveObject("VisualStudio.DTE.15.0");
    var processes = dte.Debugger.LocalProcesses.OfType<Process>();
    return processes.SingleOrDefault(x => x.ProcessID == processId);
}

Github Solution: https://github.com/thiagoloureiro/DebuggerHelper

Browser other questions tagged

You are not signed in. Login or sign up in order to post.