Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug class library that called from external app?

There is an external workflow that executes C# scripts and is able to work with DLL files(my class library).

Is it possible to attach debug to my class library project so breakpoint will hit once this WF will call it?

Thanks

like image 830
Sergejs Avatar asked Dec 02 '12 19:12

Sergejs


People also ask

How do you debug a class library from another solution?

Right click on solution -> Add Existing Project -> then select the . csproj file of your external library (project B). Again in visual studio right click on (project A) -> Add -> Project Refference... -> and then add checkmark on your external library which you want to debug (project B).

How do I debug an external EXE in Visual Studio?

Just use File/Open Project/Solution, select EXE file and Open it. Then select Debug/Start debugging. The other option is to run the EXE first and then Select Debug/Attach to process.


1 Answers

Yes, you can do this with Visual Studio. You have two options:

Configure your project to start the external program

  1. Open your DLL project.

  2. On the properties for the project, go to the Debug tab.

  3. Choose Start external program and give the path of the external program that will call your DLL, along with any command-line arguments you may need to supply, and the working directory if that's relevant.

  4. Save the project.

  5. Set breakpoints in your code where you want them.

  6. Press F5 to start debugging. (At this point, your breakpoints will say that they won't be hit because the symbols aren't loaded. Don't worry about that for now.)

  7. Do whatever you do to make the external application load your library and run your code.

Visual Studio will detect the module load, load the symbols, and stop on the breakpoint.

Attach to an existing process

If you can't start the process but instead have to attach to a process that's already running, you can do that too:

(Side note: If you're using the "Express" edition of Visual Studio, I don't think it has this feature, but I'm not certain about that. It's easy enough to tell: You'll either have the menu item mentioned on Step 4 below or not.)

  1. Make sure the process is running.

  2. Open your DLL project.

  3. Set your breakpoints, etc.

  4. From the Debug menu, choose Attach to process...

  5. In the resulting dialog box, find the process in the list, highlight it, and click Attach.

  6. Visual Studio will go into debug mode. (At this point, your breakpoints will say that they won't be hit because the symbols aren't loaded. Don't worry about that for now.)

  7. Do whatever you do to make the external process load and run your code.

Visual Studio will detect the module load in the external process, load your symbols, and stop on your breakpoint.


N.B. In both cases, if the external process loads your DLL from somewhere other than the bin/Debug folder of your project, you must make sure you copy the DLL to that other location every time you build it (you can set that up to happen automatically in the project options). Otherwise, Visual Studio won't be able to detect that the DLL being loaded is the one you're trying to debug.

like image 144
T.J. Crowder Avatar answered Sep 29 '22 02:09

T.J. Crowder