How to debug class library that called from external app?

瘦欲@ 提交于 2019-12-28 09:55:05

问题


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


回答1:


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.




回答2:


You can use the Attach to process from the Debug menu 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 Native code type from the window that appears when you click on Select button inside the Attach to process window.

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:

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 QualifierName for your system from the Attach to process window. Also, the version of managed code("Managed (v4.0)" in this case) depends on the version of .NET framework that you use.




回答3:


If you don't want/can't use external app - you can call the class library directly from Visual Studio: Ctrl+Alt+I to 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).




回答4:


I think nowadays is more actual to create a Unit test project for 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.



来源:https://stackoverflow.com/questions/13672751/how-to-debug-class-library-that-called-from-external-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!