Microsys
  

Delphi DLL Debugging Help

Help and tutorial guide on how to debug bugs in DLLs using Delphi 4, 5, 6, 7, 2005, 2006, 2007, 2010, XE - XE10 and C++ Builder / Borland Developer Studio (BDS), CodeGear Developer Studio (CDS) and Embarcadero RAD studio
Articles: overview | previous | next

December 21, 2002


BPG files - Avoid When You Want to Debug DLLs

Even if you mark/select the DLL project you want to debug in the list of projects, you will still have troubles if following conditions are present:

  • Your DLL project uses relative search paths. (Set in project options.)

    • An example with relative project search paths and Delphi IDE:
      • Use the open dialog to open a file in the same directory as the project file.
      • You can open project files located in e.g. "..shared" in the code editor using "CTRL + ENTER".
      • Try open a file located in a different directory. You can no longer open files in "..shared".

    • Jean-Claude Servaye proposes following solution:
      • Go to the Control panel - System - Advanced panel.
      • Click on Environment variables.
      • Add the absolute path of the dll to debug in the system paths.
      • Reboot the machine.

  • Your DLL project is in another directory than the other projects (possibly just if different from last project in list).


Debug Info - Always Compile and Build with All Enabled

  • You need to enable all relevant settings in "Project - Options - Compiler".
  • Whenever you try to debug from one project into another, the into project needs to have set Project - Options - Linker - Include remote debug symbols.


How to Debug - Host Application and Attach to Process

  • If you want to debug both your DLL and executable (if just the DLL file, ignore this), you will also need to configure your executable with remote debug symbols as mentioned in above section. In he DLL project choose Run - Parameters - Host Application and fill in the path to the compiled .exe file. When the exe application is running, choose (from the DLL project) Run - Attach to Process and pick your running exe program process.
  • Set a breakpoint in the DLL project where the exe will enter your DLL. Now initiate the required actions in the exe for it to occur.
  • Refer to the help for more usage variations.


Build Project Before Running and Debugging

This lessens the chance of Delphi stating (while e.g. stepping from a breakpoint): The source has changed since last build.


Blue Dots Appear Wrong Places when Building

Although this can be caused by other things (in my experience at least) a primary reason seems to be a CR/LF problem that exists in Delphi: The IDE and compiler disagree on end-of-line. For Windows/Linux/Max this means that text/files written in one OS can cause problems in another. So, if you use Windows, and suffer from the above problem, try use e.g. NotePad to open and save the problematic file with.


Attach to Project and Multi Thread Applications

It can often be useful to attach into a running project to see what the various threads are working at. However, setting a break point at all possible places is tedious. Here is the solution:
  • Select "Run - Attach to process and choose your application.
  • Select "Run - Program pauses
  • Select "View - Debug windows - Call stack and view the different thread call stacks.


COM DLL programming - Paths Are Important

Although this can be caused by other things (in my experience at least) a primary reason seems to be a CR/LF problem that exists in Delphi: The IDE and compiler disagree on end-of-line. For Windows/Linux/Max this means that text/files written in one OS can cause problems in another. So, if you use Windows, and suffer from the above problem, try use e.g. NotePad to open and save the problematic file with.


Remember ".dll" File Extension in Interface Declarations

Many people encounter problems with COM debugging when the file path is not in 8.3 format: No spaces or long file names in path. One such problem is setting breakpoints in code.
Jean-Claude Servaye sent the following solution:
  • Search the registry for the filepath of the COM. This is in 8.3 format.
  • Change it to the long file name format and the breakpoints are available.


Remember Alignment Issues When Passing Data

Even if some Windows versions can handle leaving out the extension, it is safer to include it, e.g. like this: procedure Foo external 'example.dll'


Threads - The Less The Easier to Debug

If possible, create a flag that prevents multiple threads from connecting to your DLL; This makes it easier to debug the logic of the code from within the Delphi IDE.


Access Violations, Errors, Exceptions and Memory leaks

As with everything else it is still possible to make and have pointer errors etc. Here is a list of miscellaneous links and tools to solve common problems and reasons:
  • Access violations, exceptions, memory leaks: You should probably check out memory leak detection tools such as MemCheck (compiled-in source)


Last Solution - Search and Ask in Discussion Groups

This page was never meant to be a full guide about Delphi DLL file debugging. I originally created this page because the thing that helped me, knowing how BPG files are handled, is something no-one ever mentioned in the various discussion groups (probably because my problem was quite rare to encounter). Good places to find answers include:


Epilogue - When I Experienced My "DLL Hell"

The product for which I wanted a multiple DLLs / plugins and for an application was Automation Batch Tools which comes with a DLL plugin SDK for developers. While the term DLL hell usually refers to problems associated with a visual RAD development tool (not Delphi), I dare to say that I have gone through my amount of DLL hell.
This article was written by

As one of the lead developers, his hands have touched most of the code in the software from Microsys.

If you email any questions, chances are that he will be the one answering them.
MicrosysAbout Microsys

Microsys develops desktop software, mobile apps and websites.We also offer various services for other related areas including SEO optimization.
Share this page with friends   LinkedIn   Twitter   Facebook   Pinterest   Google+   YouTube  
 © Copyright 1997-2017 Microsys

 Usage of this website constitutes an accept of our legal, privacy and cookies information.