Debugging DLL Projects in Visual Studio 2005
page 4 of 6
by Abhishek Kumar Singh
Feedback
Average Rating: 
Views (Total / Last 10 Days): 36834/ 70

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"))
        Console.Read()
    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.


View Entire Article

User Comments

Title: Nice One   
Name: Marc
Date: 2011-06-23 7:27:27 AM
Comment:
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
Comment:
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=1.0.0.0, 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
Comment:
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
Comment:
Thank you so much
Title: Saving DLL other than the Output Directory   
Name: Ali Asim
Date: 2010-03-19 1:40:37 PM
Comment:
\
\S
\
Title: call DLL from exe   
Name: Arun
Date: 2009-07-10 2:16:16 AM
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
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
Comment:
Hi Abishek

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

Product Spotlight
Product Spotlight 





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


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 8:16:59 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search