Showing posts with label DLL. Show all posts
Showing posts with label DLL. Show all posts

July 29, 2013

Breakpoint not honored while debugging a DLL

Using Delphi, it is very easy to debug a DLL having it called from a host application. This host application can be written using Delphi or any other language.

To debug your DLL in the context for a host application, just add the host application into the corresponding field in Delphi / Menu / Run / Parameters. Then run your project as usual. Delphi will not load the DLL but the host application and intercept the DLL when it is loaded by the host application. It will then honor any breakpoint you've set.

Easy? Yes, it is easy but sometimes, there is a glitch. For some reasons, Delphi won't honor your breakpoints. Among the reasons is that you forgot to use the debug build, or you forgot to enable debugging in the DLL project options.

Sometimes, there is another reason. It made me lose a lot of time playing with all options. Sometimes, I thought that I had found the right option. But later the breakpoints were still not honored.

Finally I found the correct solution. Why it works, I don't know. But so far it worked for me. The solution is: Set option "Include remote debug symbols" to true (Delphi compiler / Linking).

This was not obvious since I didn't used the remote debugger. Anyway, it works!

When option "Include remote debug symbols" is used, Delphi creates one more file with rsm extension in the same directory as the executable. It looks like the debugger always find the symbols there.


Follow me on Twitter
Follow me on LinkedIn
Follow me on Google+
Visit my website: http://www.overbyte.be

June 29, 2013

Delayed loading of a DLL

The delayed directive can be used when declaring a function exported from a DLL so that the DLL is not loaded before the function is called.

This is very useful because you may first check if the conditions are met to call the DLL before actually loading the DLL, for example check if the DLL is located where it should.

If you statically load a DLL (the default), then your application won’t start if the DLL is not present. You could as well dynamically load the DLL (LoadLibrary) to avoid this issue, but it is a little bit more work for you to declare function pointer and the get each function address (GetProcAddress) by code. Using delayed directive is much easier. Just add delayed keyword in each function declaration and suddenly a static DLL is loaded the first time one of his functions is called. If you never call a DLL’s function, then the DLL is not loaded at all.

A declaration looks like this:
    function GetSomething: Integer; external 'somelibrary.dll' delayed;

This directive exists since Delphi 2010. There is valuable documentation on Embarcadero DocWiki at this URL: http://docwiki.embarcadero.com/RADStudio/XE4/en/Libraries_and_Packages#Delayed_Loading And you can find some code sample here: http://docwiki.embarcadero.com/CodeExamples/XE4/en/DelayedLoading_(Delphi)

Follow me on Twitter
Follow me on LinkedIn
Follow me on Google+
Visit my website: http://www.overbyte.be