How to debug class library that called from external app?
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.
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).
Yes, you can do this with Visual Studio. You have two options:
Configure your project to start the external program
Open your DLL project.
On the properties for the project, go to the Debug tab.
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.
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.
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.)
Make sure the process is running.
Open your DLL project.
Set your breakpoints, etc.
From the Debug menu, 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.
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.