Win32 DLL |
|
Introduction |
A Win32 DLL is a library that can be made available to programs that run on a Microsoft Windows computer. As a normal library, it is made of functions and/or other resources grouped in a file. The DLL abbreviation stands for Dynamic Link Library. This means that, as opposed to a static library, a DLL allows the programmer to decide on when and how other applications will be linked to this type of library. For example, a DLL allows difference applications to use its library as they see fit and as necessary. In fact, applications created on different programming environments can use functions or resources stored in one particular DLL. For this reason, an application dynamically links to the library. You create a DLL like any other application, as a project. This project must contain at least one file. From your experience, you
probably know that each C++ program contains the main() function and each Win32 application contains a
WinMain() function. These are referred to as entry-points because the compiler always looks for these functions as the starting point of an application. In the same way, a Win32 DLL must contain an entry point function. The most regularly used function as the entry point is called
DllMain. Unlike a C++ program that uses main() and unlike a Win32 application that uses
WinMain(), a DLL can have a different function as the entry-point but it must have an entry point. When building your DLL, you will need to communicate your entry-point to the compiler.
We have mentioned that a DLL is created as a project that contains at least one source file and this source file should present an entry-point. After creating the DLL, you will build and distribute it so other programs can use it. When building it, you must create a library file that will accompany the DLL. This library file will be used by other programs to import what is available in the DLL: When the import library is created, it contains information about where each available function is included in the DLL and can locate it. When an application needs to use a function contained in the DLL, it presents its request to the import library. The import library checks the DLL for that function. If the function exists, the client program can use it. If it doesn't, the library communicates this to the application and the application presents an error. Normally, the import library is created when building the DLL but you must provide a mechanism for the compiler to initiate it. There are two main ways you do this. You can use a calling convention that allows the compiler to detect what function will be accessible to the clients of the DLL. In this case, each one of these functions will start with the _declspec modifier. When creating the DLL, the _declspec modifier must take as argument the dllexport keyword. The functions do not have to be included in the main file that contains the entry-point. It may simply be convenient to do it that way.
|
|
Win32 DLL Test |
Naturally you should test your DLL to make sure it behaves as expected, before distributing it to other applications. For an application to be able to use the DLL, it must be able to locate its dll and its lib files. The simplest way to do this is to simply copy and paste these files in the client project. Some other times, the DLL and its library will need to reside in the system directory. To make the DLL available to an application, you must import the DLL into the application. Although there are various ways to do this, the simplest consists of adding it to the project by using the main menu where you would click Project -> Add Existing Item…, and selecting the lib file. We mentioned that, when creating the DLL, you should use the _declspec(dllexport) modifier for each function that will be accessed outside of the DLL. In the project that is using the DLL, each function that will be accessed must be declared using the _declspec(dllimport) modifier. |
Practical Learning: Testing a DLL |
|
|
||
Previous | Copyright © 2004-2006 FunctionX, Inc. | Next |
|