Attach a ClassLibrary to an External Application (Visual Studio)

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 project properties I configured the EXE path, but usually breakpoints do not work.

  • Visual Studio running as administrator
  • Upgraded DLLs in the executable folder

ButifIruntheapplicationmanually(inthesamepathIconfiguredintheimageabove)andthroughtheAttachtoProcessoptionthebreakpointswork

IalsotriedtocopytheDLLandPDBafterBuildtoensurewhenIhavechangestheprogramwillhavetherecentversion(otherwisetheattachdoesnotworkcorrectly).WhatIthinkhereisthatIneedtotypea"delay" to do the attach.

    
asked by anonymous 28.02.2018 / 10:13

1 answer

0

After some research 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 in 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:

privatestaticvoidAttachProcess(){varlocalByName=System.Diagnostics.Process.GetProcessesByName(_appName);MessageFilter.Register();varprocess=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: link

    
02.03.2018 / 10:30