Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
243 views
in Technique[技术] by (71.8m points)

c# - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...