C# 如何调试从外部应用程序调用的类库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13672751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to debug class library that called from external app?
提问by Sergejs
There is an external workflow that executes C# scripts and is able to work with DLL files(my class library).
有一个外部工作流可以执行 C# 脚本并且能够处理 DLL 文件(我的类库)。
Is it possible to attach debug to my class library project so breakpoint will hit once this WF will call it?
是否可以将调试附加到我的类库项目中,以便一旦此 WF 调用它就会命中断点?
Thanks
谢谢
采纳答案by T.J. Crowder
Yes, you can do this with Visual Studio. You have two options:
是的,您可以使用 Visual Studio 执行此操作。您有两个选择:
Configure your project to start the external program
配置您的项目以启动外部程序
Open your DLL project.
On the properties for the project, go to the Debugtab.
Choose Start external programand 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.
Save the project.
Set breakpoints in your code where you want them.
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.)
Do whatever you do to make the external application load your library and run your code.
打开您的 DLL 项目。
在项目的属性上,转到“调试”选项卡。
选择启动外部程序并提供将调用您的 DLL 的外部程序的路径,以及您可能需要提供的任何命令行参数,以及工作目录(如果相关)。
保存项目。
在代码中需要的地方设置断点。
按 F5 开始调试。(此时,您的断点会说它们不会被击中,因为符号未加载。现在不要担心。)
尽一切努力使外部应用程序加载您的库并运行您的代码。
Visual Studio will detect the module load, load the symbols, and stop on the breakpoint.
Visual Studio 将检测模块加载、加载符号并在断点处停止。
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 thinkit 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.)
(旁注:如果你使用的是 Visual Studio 的“Express”版本,我认为它没有这个功能,但我不确定。很容易说:你要么有菜单是否在下面的第 4 步中提到的项目。)
Make sure the process is running.
Open your DLL project.
Set your breakpoints, etc.
From the Debugmenu, choose Attach to process...
In the resulting dialog box, find the process in the list, highlight it, and click Attach.
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.)
Do whatever you do to make the external process load and run your code.
确保进程正在运行。
打开您的 DLL 项目。
设置断点等。
从调试菜单中,选择附加到处理...
在出现的对话框中,在列表中找到进程,突出显示它,然后单击附加。
Visual Studio 将进入调试模式。(此时,您的断点会说它们不会被击中,因为符号未加载。现在不要担心。)
尽一切努力使外部进程加载并运行您的代码。
Visual Studio will detect the module load in the external process, load your symbols, and stop on your breakpoint.
Visual Studio 将检测外部进程中的模块加载,加载您的符号,并在您的断点处停止。
N.B.In both cases, if the external process loads your DLL from somewhere other than the bin/Debugfolder 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.
注意在这两种情况下,如果外部进程从bin/Debug项目文件夹以外的某个地方加载您的 DLL ,您必须确保每次构建它时都将 DLL 复制到其他位置(您可以将其设置为在项目选项)。否则,Visual Studio 将无法检测到正在加载的 DLL 是您尝试调试的那个。
回答by Manas Jog
You can use the Attach to processfrom the Debugmenu for debugging your DLL project. You may be required to use mixed mode debugging if debugging does not happen with native code. This can be done by selecting Managed and Nativecode type from the window that appears when you click on Selectbutton inside the Attach to processwindow.
您可以使用“调试”菜单中的“附加到处理”来调试您的 DLL 项目。如果本机代码没有进行调试,您可能需要使用混合模式调试。这可以通过从单击“附加到进程”窗口内的“选择”按钮时出现的窗口中选择托管和本机代码类型来完成。
If the edition of Visual Studio that you are using supports macros, then you can create a new macro with the following code to automate all this:
如果您使用的 Visual Studio 版本支持宏,那么您可以使用以下代码创建一个新的宏来自动化所有这些:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module AttachToProcess
Public Sub DebugMyDLL()
DTE.ExecuteCommand("Build.BuildSelection")
Dim ApplicationExePath As String = "C:\Program Files (x86)\foo\bar.exe"
Shell(ApplicationExePath)
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(2) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("Managed (v4.0)")
dbgeng(1) = trans.Engines.Item("Native")
Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "<QualifierName>").Item("bar.exe")
proc2.Attach2(dbgeng)
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub
End Module
The above macro tries to build your project, launches the external application and then attaches your DLL to that program automatically. You can get the QualifierNamefor your system from the Attach to processwindow. Also, the version of managed code("Managed (v4.0)" in this case) depends on the version of .NET framework that you use.
上面的宏尝试构建您的项目,启动外部应用程序,然后自动将您的 DLL 附加到该程序。您可以从“附加到进程”窗口中获取系统的QualifierName。此外,托管代码的版本(在本例中为“托管 (v4.0)”)取决于您使用的 .NET 框架版本。
回答by rock_walker
I think nowadays is more actual to create a Unit test projectfor executing your library code. Thus you will kill two birds with one stone: will able to debug your project in the same solution and by the way start to cover your code by tests.
我认为现在创建一个用于执行库代码的单元测试项目更实际。因此,您将一石激起千层浪:将能够在同一个解决方案中调试您的项目,并且顺便开始通过测试覆盖您的代码。
回答by alexkovelsky
If you don't want/can't use external app - you can call the class library directly from Visual Studio: Ctrl+Alt+Ito show "Immediate"widow, then you can call any method from your class library from there (use breakpoints). You'll have to type fully-qualified names (i.e. namespaces).
如果您不想/不能使用外部应用程序 - 您可以直接从 Visual Studio 调用类库:Ctrl+Alt+I以显示"Immediate"寡妇,然后您可以从那里调用类库中的任何方法(使用断点)。您必须键入完全限定的名称(即命名空间)。
回答by Alex_P
A useful link from Microsoft: Specify a calling app in a managed DLL project
来自 Microsoft 的有用链接:在托管 DLL 项目中指定调用应用程序
- Open your class library project
- Go to [Project]
- At the bottom select project's [Properties...]
- Go to [Debug]
- In the 'Launch' row, select "Executable"
- Select the path to the .exe file
- Set your breakpoints
- Run the project.
- 打开您的类库项目
- 前往【项目】
- 在底部选择项目的[属性...]
- 转到[调试]
- 在“启动”行中,选择“可执行文件”
- 选择 .exe 文件的路径
- 设置断点
- 运行项目。
The link from Microsoft has various other useful debugging scenarios.
来自 Microsoft 的链接有各种其他有用的调试场景。
I am using Visual Studio 16.4.5.
我正在使用 Visual Studio 16.4.5。

