PDA

View Full Version : Writing DLL's



Crisp_N_Dry
02-06-2004, 10:31 AM
A post by MrWB got me to thinking about DLL's and their usefullness. I'm wondering what the benefit is of using DLL's. Also I am wondering how I go about writing and compiling DLL's. Do they use a different syntax/construct to Exe (As I understand it they are the same) and how I go about linking them to an Exe.

I have been out of programming for about 12 months now but am looking forward to returning the the game dev scene. So hello all.

Alimonster
02-06-2004, 11:04 AM
Hello again!

The benefit of DLLs is that they can be loaded into memory once, but shared among many applications. As an example, MSHTML.dll is Microsoft's HTML parser - it's used in Outlook Express, Internet Explorer, FrontPage (I'd guess) and a few other applications, but it only needs to be loaded once (which is a fairly big memory saving if you think about it).

Another possibility with DLLs is that you can dynamically load them at run-time (rather than including them statically into your exe), which might be useful for maybe plug-ins or similar. The down side here is that the DLL may not be in the right place (think FileExists style of problems) if you're relying on a particular name, or perhaps the DLL won't contain the right sort of functions on which you're relying.

DLLs are basically done the same way as exe files. You create a new DLL and the word "library" replaces the word "program" in the program source. You then declare a bunch of functions (be careful when using memory managed stuff - like strings - though, since you need to include a borland specific thing in the uses clause IIRC. Use PChars instead, maybe).

The DLL is simply a bunch of functions. In your separate exe application, you'd maybe declare the function (prototype it again, look up the "external" keyword in the help files - it's not difficult to follow), which would have the effect of statically linking your exe to the DLL. If the DLL doesn't exist, or that function isn't in the DLL, then your exe won't even start!

Alternatively, you might use LoadLibrary/GetProcAddress/FreeLibrary to dynamically load the DLL at some point when your exe is running. You'd declare a function pointer of the type wanted (matching the prototype), then declare a pointer, then use GetProcAddress.

The online help is really good in this case. I'd give examples but it's time for lunch. Perhaps once I get home.

Mrwb
02-06-2004, 11:07 AM
Using dll's have many advantages. The two most important I can think of, is code re-use, and easy upgrade (you only mofify the dll instead of the exe). It's also fairly fast, easy to implement, and offers a more "clean" structure of your program, since all your code wouldn't be resident in the main exe.

Here's a tutorial on writing dll's and linking 'em with delphi:
http://delphi.about.com/library/weekly/aa041100a.htm

EDIT: Ooh.. You beat me by three min. Alimonster ;)

Crisp_N_Dry
03-06-2004, 12:11 PM
Some excellent responses with links aswell, what more could I ask. Thanks again :)