Debugging DLL Projects in Visual Studio 2005
Published: 12 Jul 2007
This article will walk you through the techniques used to debug the DLL projects in Visual Studio 2005 with the help of a sample application.
by Abhishek Kumar Singh
Average Rating: 
Views (Total / Last 10 Days): 44502/ 90


Dynamic Link Library (DLL) is a file (also called library) which contains executable functions or data that can be used by other applications. A DLL file has the extension "DLL" in its name. A DLL cannot run directly. DLL needs to be called by some other application or EXE to run. In fact, a DLL contains various functionalities to be used by various EXE (or other DLL).

·         If DLL's are placed in some central repository then multiple programs can use the functions of DLL's at the same time. Thus saving the storage space and duplication effort. In addition, if new features are added in the new version of DLL, older version can be replaced by newer and all executables would benefit. DLL's are called dynamic links as they are loaded in the memory (RAM) when it is called by the executables at run time. They run in the program's own memory space. Though a DLL is loaded just once and whenever any other program requests to use it then operating system just maps it to the program's address space. The idea behind using DLL's is "to have a library of commonly used functions which can be obtained by everyone as they needed."

·         Microsoft has written a number of DLL's for Windows to have standard functionalities and appearances for its Operating Systems and most of the programs. As multiple applications use the DLL's, replacing older versions with newer versions of DLL's should not cause stopping running applications. System may crash or behave strangely or a program might not function, if a certain DLL is missed or the wrong version of DLL is installed in the computer.

Building a DLL Project (DLL) in debug mode

Open Visual Studio 2005 IDE and create a Class Library project as mentioned below.

File >> New >> Project

·         Project types: Visual Basic

·         Visual Studio installed templates: Class Library

·         Project Name: DLLProject

Default class Class1.vb will be added in the project automatically. Rename it to CLibrary.vb.

Open CLibrary.vb in the code window and implement a function GetWelcomeMessage as given below:

Listing 1 – Implement function GetWelcomeMessage in CLibrary.vb 

''' <summary>
    ''' Get the welcome message on the basis of company name.
    ''' </summary>
    ''' <param name="Name">Name of Employee</param>
    ''' <param name="Company">Company of Employee</param>
    ''' <returns>Welcome Message</returns>
    ''' <remarks></remarks>
    Public Function GetWelcomeMessage(ByVal Name As String, 
      ByVal Company As StringAs String
        If Company = "mindfire" Then
            Return "Hello"
            Return "Hello " & Name & "! Thank you for viewing this article"
            Return "Hello Guest! Thank you for viewing this article"
        End If
    End Function

Save the project and build it by pressing Ctrl+Shift+B. If the build is successful then the sample DLLProject is ready. A DLLProject.dll file would have been created in …\DLLProject\bin\Debug folder. Now it is time to debug it. We can debug DLL projects in VS Studio 2005 in either of following two common ways.

I. Debug DLL project using Immediate Window.

II. Debug DLL project using external calling application.

A DLL programmer must know both ways of debugging. Why? You will know within a few moments. I am going to describe both ways in this article.

Debug DLL project using Immediate Window

To debug the library functions using immediate window, we can follow any of the following ways:

·         By implementing procedure Main() which calls the library function.

·         By making the library function Shared.

Debug by implementing Main() in module to call the library function.

In the DLLProject press CTRL+Shift+A to open Add New Item window. Choose Module and let the name be Module1.vb. Add the code given in Listing 2 in the Module1.vb.

Listing 2 – Implement Main() in Module1.vb to call the library function

Module Module1
    Sub Main()
        Dim oCLibrary As New CLibrary
    End Sub
End Module

The Main() function is implemented to call the library function GetWelcomeMessage by passing required parameters. Now we can set breakpoint at any line in the definition of Main() or GetWelcomeMessage and use immediate windows to step into the code as we do in any other windows or console applications.

Say I put the breakpoint at function GetWelcomeMessage as given in the figure below.

Figure 1 – Setting breakpoint in the library function code

Now open the immediate window by pressing CTRL+ALT+I or using Debug >> Window >> Immediate. Write Main() or ? Main() in the window and press the Enter key. You will see that breakpoint is hit by the execution pointer and now you can debug the function.

Figure 2 – Executing Main() from immediate window to step into the library function code

Debug using Shared access-specifier to the library function.

You can directly debug the functions implemented in class library application just by using Shared access-specifier for the functions. In this way you do not need the module file containing Main() procedure. In the immediate window call the shared function by using the class name. For example, to call the function GetWelcomeMessage in the class CLibrary of the sample DLLProject, write CLibrary.GetWelcomeMessage("MyName","MyCompany") in the immediate window and press Enter. The execution pointer will hit the breakpoint and thereafter you can debug the function.

Figure 3 – Executing Shared function from immediate window to step into the library function code

Debug DLL project using an external calling application

We can create any external application which produces exe output. This application needs to be implemented such that it should reference the DLL file and call the library function. For example, I am going to create a console application.

Open Visual Studio 2005 IDE and create a new console application project as mentioned below:

File >> New >> Project

Project types: Visual Basic

Visual Studio installed templates: Console Application

Project Name: CallDLL

Open the solution explorer. You will see a default module Module1.vb exists there. Open Module1.vb and add code in the procedure Main() as given below:

Listing 3 – Procedure Main in Module1.vb of CallDLL application

Module Module1
    Sub Main()
        Dim oCLibrary As New DLLProject.CLibrary
        Console.WriteLine(oCLibrary.GetWelcomeMessage("Abhishek", "mindfire"))
    End Sub
End Module

Add reference of DLLProjects.dll which exists in …\DLLProject\bin\Debug created in the DLLProject before. Now press Ctrl+Shift+B to build the solution containing CallDLL project. After a successful build, CallDLL.exe file of the project should be created in …\CallDLL\bin\Debug folder. Note the full path of the exe. Close the CallDLL project.

Configure the library project (DLLProject) to use caller application (CallDLL)

Open the previously created DLLProject in Visual Studio 2005 IDE. Delete Module1.vb from the project as we do not need it now. Open the Property Pages of the project from the "Project" menu.

Figure 4 –Open property page of the DLLProject

In the Property Page screen choose tab option Debug. Under the Start Action, choose the option Start External Program. In the text box specify the full path of caller exe (CallDLL.exe) file. You can also use the browse button to locate the exe file.

Figure 5 – Setting start action in property page of DLLProjecttion

Close the Property Page screen and save the DLLProject.

Test the DLL project execution

Build DLLProject and press F5 to run it. After running it you will see a console screen as the one below.

Figure 6 – Output console screen of the CallDLL.exe run from DLLProject

Note that after running the DLLProject we are actually getting the same results as if we had run the caller application (CallDLL). Wait! We have not done the debugging of the DLLProject yet by stepping into the code which is our main purpose. We need to step into the code as we cannot always determine the correctness of logic implemented in the DLL by just looking the output screen. In the real world scenario we used to have very complex logic inside DLL and multiple applications may share the DLL. Hence, a developer needs to DEBUG CODE LINE BY LINE before releasing a DLL in production.

To set BREAKPOINT in the DLLProject code

To step into the code of DLLProject for debugging, set the breakpoint in the implemented function GetWelcomeMessage of DLLProject which is being invoked by the calling application (CallDLL).

Figure 7 – Set breakpoint in the function definition of DLLProject

Press F5 to run DLLProject. Now the execution pointer must stop at the line which is set with breakpoint as shown below.

Figure 8 – Execution pointer hits the breakpoint and steps into the code

Execution pointer steps into the code when breakpoint is hit. Now you have the control of the execution for DLLProject. Now you can debug the action of each line before deploying a release version of DLL on production machine. Please remember that external application EXE should always build with the updated version of the DLL to step into the DLLProject code.



In this article I have described two ways to debug the DLL project (class library application). One technique is to use immediate window which helps in quick debugging of library functions. However, in practical situations library functions accept complex parameter values (generated by some other exe applications) to process, which we cannot normally pass through the Main() or immediate window. Here we need the second technique of debugging DLL project- by setting an external start program.

Suggestions and feedbacks for this article are appreciated in advance.

Abhishek Kumar Singh

Mindfire Solutions

User Comments

Title: Nice One   
Name: Marc
Date: 2011-06-23 7:27:27 AM
Great article thanks so much! Nice and easy to understand
Title: FileNotFoundException error on debugging dll   
Name: Binkey
Date: 2011-02-08 4:55:58 PM
When I do the above steps and press F5, I get an exception error "FileNotFoundException was unhandled box" stating "Could not load file or assembly 'AddDll, Version=, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified."
AddDll is the dll name. How could it not find the file. I'm running it?
Title: call dll from an exe question not answered   
Name: jeanie
Date: 2011-02-08 4:31:50 PM
Nobody answered the question titled "call DLL from exe". How do you get the debugger to step into the dll when running the exe? Please advise if your still there.
Title: debug DLL   
Name: Robat7
Date: 2010-04-13 10:09:03 AM
Thank you so much
Title: Saving DLL other than the Output Directory   
Name: Ali Asim
Date: 2010-03-19 1:40:37 PM
Title: call DLL from exe   
Name: Arun
Date: 2009-07-10 2:16:16 AM
Hi, nice article. But I'm stuck at a point and think you'l be able to help me. I've created a dll class library project and attached it to a process. I've placed both .dll and .pdb files in the bin folder of the exe application. I've put the breakpoints in my dll project and now start the exe. The process runs and when the dll is called, the control does not enter the class created in the dll(I've put breakpoints at relevant positions). The process completes, but the control does not enter class library.
What I am doing wrong?
Any suggestions would be welcome. Thanks in advance.
Title: Re: Would it be same in 2005   
Name: Abhishek (Author)
Date: 2007-11-21 2:16:32 AM
you should find the DLL file in bin folder as follows:
project folder >> bin >> debug/realease >> dll file
if you missed to locate your project folder, try this:
(Be sure you have created a library project)
1. In the VS 2005 IDE, open solution explorer.
2. in the tree structure there should a "bin" node, exapand that node by clicking on +
3. then you should see Debug or Release, open +
4. now you can see you dll file in the list.
5. right click on the dll and click on properties and see the path of the dll in the properties, and collect the dll file then.

Hope it helps you. Thanks.
Title: Would it be same in 2005   
Name: Ram
Date: 2007-10-19 12:42:23 PM
Hi, you have described very well; but will it be happen in VS 2005.I could not see any bin folder to see the DLL file once you build the application.Do we need to create manually bin folder
Title: Reason fo not keeping both projects in same solution   
Name: Abhishek Kumar Singh
Date: 2007-07-22 4:43:41 PM
darren, thanx for your suggestion.. but intensionally i didn't keep both projects in same soluction. I wanted to show that each porjects could be at different location .. may be in same computer or different computer over LAN. Also in case of large projects client and server projects use to be developed separately by different developers/team.
Title: Add both projects to same solution   
Name: darren
Date: 2007-07-22 4:06:33 PM
You missed the best solution.
Add both projects to the same solution. Set a ref in the EXE to the DLL project. Set startup project as the EXE.
Title: Great Article   
Name: Dhanabalan.R
Date: 2007-07-15 3:15:55 AM
Hi Abishek

Nice and Useful articles for developers
Title: Nice article   
Name: Macho
Date: 2007-07-12 2:07:02 PM
Very good article for DLL masters and real life programming.

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-03-05 3:08:35 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search