Debugging a third-party DLL in Visual Studio?

2019-01-18 21:18发布

I am using a third-party DLL. For some particular cases, a function in the DLL is throwing an exception. Is it possible to debug the DLL in the Visual Studio?

After the answer from Andrew Rollings, I am able to view the code, but is there any easy way to debug through the code in Visual Studio?

8条回答
Bombasti
2楼-- · 2019-01-18 21:52

There are two methods I've come across:

1) Accessing the DLL project from the using project. This involves building the DLL in a separate instance of Visual Studio and then accessing the DLL through a different project in Visual Studio (this assumes you have the source code). There a number of ways to accomplish this:

  • You can add Trace.WriteLine statements in the DLL that will show up in the 'Output' window in Visual Studio.
  • You can add System.Diagnostics.Debugger.Break() statements to the DLL code. When running the calling project in Visual Studio, program execution will stop there. From here you can add access the call stack (including all function calls in DLL itself) and set break points (although the icon for the breakpoint will appear disabled and the hover text for the break point will read "The breakpoint will not currently be hit. No symbols have been loaded for this document").
  • If the DLL is throwing an exception (which you can see from the 'Output' window if the exception is caught and handled by the DLL) you can tell Visual Studio to always break when that type of exception is thrown. Hit Ctrl + Alt + E, find the type of exception being thrown, and click the 'Throw' column for that exception. From here it is exactly as if you had used System.Diagnostics.Debugger.Break() (see above).

2) Attaching a using process to the DLL project. This involved hooking the Visual Studio debugger into a running process.

  • Open the DLL project in Visual Studio.
  • Run an application that uses the DLL (this application can't be run from another instance of Visual Studio since the process will already have a debugger attached to it).
  • From here you can add break points and step through the DLL code loaded in Visual Studio (although the break point will appear disabled the same as in method 1).
查看更多
何必那么认真
3楼-- · 2019-01-18 21:54

One more option we should mention here is dotPeek 1.2 (a free decompiler from creators of ReSharper). Here is a nice post describing how to configure VS symbol server and dotPeek 1.2 to debug decompiled code from VisualStudio: http://blog.jetbrains.com/dotnet/2014/04/09/introducing-dotpeek-1-2-early-access-program

查看更多
登录 后发表回答