1. Computing

DLLs Made Simpler

This article is aimed at removing the fear of writing DLLs for beginners.

By

Article submitted by Richard Le Mesurier for the Delphi Programming Quickies Contest.

Easier, Simpler

This document is aimed at removing the fear of writing DLLs (Dynamic Link Libraries) for beginners. A lot of beginners think DLLs are really complex, but in actual fact they are pretty simple.

Hopefully this will provide a template you can use to develop your own. The actual code doesn't do very much.

I have also included an idea I found in the Secure32.DLL (NT security, SSPI validation). This has a routine that returns the addresses of all routines in the DLL. I think this is a "good" way of coding, especially if you have a DLL with many routines and you don't want to have to keep calling GetProcAddress. Again, it's just the idea, and some explanatory notes on how _I_ did it.

File names are rather long. I did it this way for explanatory purposes only. I would expect "real" code to use more sensible naming conventions...

I have not attempted to explain all the Delphi functions used - use the F1 help.

I am a beginner. If I made a mistake, please email me back and tell me. This pack started off as notes to me so I could remember the correct way to do all these methods.

Things to remember when writing DLLs

  1. If you use long strings, then you have to use BORLANDMM. This is mentioned in the blank DLL library template that Delphi makes for you.
  2. I think threads could be a problem
  3. Some 3rd party controls are not DLL safe - this could cause strange AV (access violation) errors
  4. Make sure you tell your user what calling convention to use - I use StdCall (but so far we can't get that to work from C++, so this might be wrong)
  5. I would suggest making your own copies of parameters sent to the DLL as I have done. My logic is that the DLL is a "black box", you give it some info, and it gives you some info back out. I would rather separate my DLL as much as possible from the calling program, hence this method.
  6. ... more DLL related articles
First, download the full DLL project source code!

Final words

I was teaching myself the very basics of DLL programming the other day, and part of that involved me writing up a tutorial for my friends. Everything I have learnt about Delphi has either been self-taught, or through some Internet help - I love the strength of our community so would like to contribute. What goes around comes around...

Basically, this is very informal document with source code. The DLL itself is pretty useless, but I have tried to explain the things that initially cared me about this topic. "DLL" is an intimidating word for a lot of us and I have tried to unmask this monster and show people that it really is easy.

©2014 About.com. All rights reserved.