Creating And Using DLLs by (09 January 1999) |
Return to The Archives |
Introduction
|
So you're interested in making an oh-so-fashionable DLL? Well look no further. Creating a DLL is a lot easier than you probably think. DLLs (Dynamic Link Libraries) are very useful when you're writing windows programs. A good example use would be if you create a number of programs that all use common functions. In such a case, you can create a library that they all call functions from. Another interesting use would be to create plug-ins for your application. For example, a particular program I was working on in the past had plug-in renderers that were compiled as DLLs and loaded when the actual program ran. This document will attempt to show you how to create a basic DLL and how to use it (with import library linking and without). This document will not get into things such as sharing data, thread precautions, or resource libraries. We're just sticking to basic dll uses -- the rest you can experiment with on your own. Again, MSVC6 and the ide. Hopefully you shouldn't have too many problems w/conversions. Let's roll. |
Creating The DLL
|
Creating a dll is nothing too far out. Basically, you write your functions then compile
them almost like you would any other library. If you're using MSVC, create a new project and set the
target as a Win32 Dynamic-Link Library. That way when you compile your code, it should produce
a dll, an import library (.lib), and an export library (.exp). Here's some sample code that shows
how the sample DLL included with this tutorial is put together: Header File (dlltest.h):
Source File (dlltest.cpp):
Basically, there's nothing too special about the code above. The sample application is a console app, so I just wrote up two simple functions that display some text and tell you where they're being called from. The extern "C" __declspec(dllexport) means that we want to allow these functions to be used by our actual program. When you compile this thing, it should create the libraries. Now lets see how we actually use them. |
Using the DLL (With an Import Library)
|
First lets look at how to use our DLL using the import library (dlltest.lib) that
was one of the resulting files from compiling the DLL above. This method is the easiest because
all you really have to do is include the header file, then include the import library when you're
linking your objects. That's it. Here's an example: DLL Test Source File (dllrun01.cpp) - Win32 Console Application:
The code above will work fine if you have your dlltest.h header file in your compiler's header path, and the dlltest.lib import library in the lib path. You also need to link your import library with your other modules when linking. Be warned though, when you run the resulting executable, the DLL must be in the path or in the same directory as the executable otherwise it will have an error. That's the whole point. But if you had 10 programs that use the same DLL, you can put one copy of the DLL in a common directory. dllrun01.exe Output:
|
Using the DLL (Without an Import Library)
|
Now we'll look at how to load the DLL on the fly. This is useful for things
such as
if you don't know what the dll is called before-hand (for example the plug-in system I mentioned
earlier). Here's the example code: DLL Test Source File (dllrun02.cpp) - Win32 Console Application:
The code should load our library (assuming its in the path), then get the addresses of the two functions that we want to call. I'd recommend that you be careful when calling functions from dlls without import libraries. There's a lot more code to write (loading each function/library), and a lot more room for errors if your dll isn't correct. So if you don't need to do it this way, I wouldn't. That's up to you. dllrun02.exe Output:
|
Closing
|
Hopefully you learned how to get started on working with dynamic-link libraries. Please let me know what you think of this document. There are many other uses and many more advanced topics in DLL management that you can explore on your own. Also, although the sample program is a Win32 console application, it works just the same for 'normal' Windows programs. |
Downloads
|
|
|