Who's afraid of a DLL?

By Eric Pearson

When I first started moving from DOS to Windows programming, the term "DLL" was pretty intimidating. But as I learned more about them, and what they could do for my programs, the mystery -- and the fear -- started to fade. Now they have become an absolutely indispensable part of my Windows programming toolbox, and they should be part of yours.

Just saying that DLL stands for "Dynamic Link Library" isn't very helpful. That last word -- Library -- is by far the most important.

Important Point #1: A DLL is simply a file that contains one or more pre-compiled functions. I was familiar with pre-compiling my own PB/DOS functions into Unit files, but DLLs take the "library" concept a couple of steps further. A couple of very powerful steps!

Important Point #2: DLLs are the fastest, most compact, and most efficient form of Windows library file. The Microsoft Windows operating system itself is based almost entirely on DLLs! In fact, Windows ME and XP rely on more "system DLLs" than ever before. If you want raw, high-efficiency computing power, you are probably wasting your time with ActiveX, OLE, COM, OCX and all of those other three-letter acronyms. They all rely on DLL technology "under the hood" so they'll never be more efficient than using DLLs directly.

Important Point #3: Your PowerBASIC PB/CC, PB/Win, and PB/DLL programs can use DLLs that have been created by any almost Windows compiler, just as if they had been compiled by PowerBASIC. That means that thousands and thousands of sophisticated, thoroughly field-tested functions are available right now, for use in your PowerBASIC apps.

Important Point #4: If you use PB/Win or PB/DLL to create a DLL, virtually any computer language can use it. The DLLs that you create with PowerBASIC can be used by Visual Basic, C++, Delphi, and so on. DLLs are the one standard that virtually everybody supports! And many programs can use DLLs too, to provide "extension" functions.

Important Point #5: There's nothing magic -- or difficult -- about using a function in a DLL. You simply use the PowerBASIC DECLARE statement to tell the compiler that a certain function is located in a certain DLL file. Then you can use the function almost as if was part of the PowerBASIC language itself. If you put the DECLARE statements in an #INCLUDE file, it's almost like using a file that contains the source code for the functions!

Important Point #6: DLLs save disk space, reduce load times, and reduce download times. The first two words of "Dynamic Link Library" describe another very powerful aspect of DLLs. Your program "links" to them at runtime, not at compile time. While it is running, an EXE file sees a DLL as if it was part of the EXE, even though it is a separate file. That means that if you have a DLL that contains some multi-purpose functions, many different programs can use one copy of the DLL file. That can result in significantly smaller programs, which is especially important if you want people to download your programs from the internet.

Important Point #7: DLLs are memory-efficient. If two or more programs are using a certain DLL, only one copy of the DLL is actually loaded into memory. Each program will have its own set of DLL variables, and those do take up a small amount of memory, but the DLL itself will be loaded only once, and that can result in a significant reduction in the amount of memory that your programs use. (Don't worry, programs will never conflict with each other because Windows automatically keeps them from "seeing" each other.)

Important Point #8: DLLs can provide powerful new "variable scope" options. You're probably familiar PowerBASIC's GLOBAL, LOCAL, and STATIC variables. GLOBAL variables are visible to every SUB and FUNCTION in your program, while LOCAL and STATIC variables are visible only to individual SUBs and FUNCTIONs. Well, DLLs add an entirely new level of variable scoping to your programs. The variables in a DLL are completely isolated from the rest of your program, so the GLOBAL variables in a DLL are really isolated "module level" variables that can't be seen by any other parts of your program. And a DLL can't see your main program's GLOBALs, either.

Important Point #9: Managing DLL files doesn't have to be a nightmare! We have all heard about "DLL Hell": DLLs being incorrectly replaced by older versions, and programs that fail because the wrong version of a DLL was installed. Virtually 100% of those problems can be eliminated if you simply store your program's DLLs in your program's own directory. If you put them in the "Windows System" directory (as many programs do) you are asking for trouble. But the first place that Windows looks for DLLs is in your program's default directory, so if you put them there, you'll avoid all of those headaches.

Important Point #10 is that there are lots of other important points! Once you start using them, you'll see that DLLs provide many different, subtle advantages. You'll reduce the amount of time it takes to compile large programs because you won't have to recompile the whole thing every time. You can update your program by changing a single DLL instead of replacing a huge, monolithic file. And on and on!

The more you use DLLs the more advantages you'll find. And the more Powerful your PowerBASIC programs will become!


And now a brief word from our sponsor...

Eric Pearson is the President of Perfect Sync, Inc.

Perfect Sync provides DLL-based development tools to PowerBASIC programmers, including SQL Tools, Graphics Tools, and Console Tools. For more information about how DLL-based development tools can add even more Power to your PowerBASIC programs, visit http://perfectsync.com/DevelopmentTools.htm.