Dynamic Link Libraries Inside-Out - Dynamic Link Library Redirection
(Page 7 of 9 )
Starting with Windows 2000, you can ensure that your application uses the correct version of a DLL by creating a redirection file. The redirection file must be named as “appname.local”. For example; an application c:myappmyapp.exe calls LoadLibrary using the path c:program filescommon filessystemmydll.dll. If c:myappmyapp.exe.local and c:myappmydll.dll exist, LoadLibrary will load c:myappmydll.dll.
Otherwise, LoadLibrary will load c:program filescommon filessystemmydll.dll.
Writing your own DLLs
I’ll explain briefly how to write your own DLLs and use them in your applications using VC++ 6.0 and Visual Basic 6.0. The source code files for the examples can be downloaded from the last page of this article. So let’s get started!
There are 3 steps: Creating Source Files, Exporting Symbols, and Creating an Import Library.
1. Creating Source Files
• Fire up Microsoft Visual C++ IDE and create a Blank Workspace with the name DLLsTraining.
• Add a new “Win32 Dynamic-Link Library” project with name “LoadTimeDLL” to the workspace “DLLsTraining”.
• Choose an empty DLL project
• Add a new file named “LoadTimeDLL.cpp” and “LoadTimeDLL.h” to LoadTimeDLL project.
• Add code shown below to “LoadTimeDLL.cpp” and “LoadTimeDLL.h”
Read the comments so as to find out why are you writing this.
LoadTimeDLL.cpp
#include <windows.h>
// Define the symbol LOAD_TIME_DLL_EXPORTS before including
// "LoadTimeDll.h". This makes the __declspec(dllexport) visible to the
// implementing .cpp file. When using this header in other modules, don’t define
// this symbol. Then those modules will see __declspec(dllimport). This makes
// those modules to import the function form the DLLs.
#define LOAD_TIME_DLL_EXPORTS
#include "LoadTimeDLL.h"
void SayLoadTimeDLL()
{
MessageBox(NULL, TEXT(“Information"), TEXT("I am a load time DLL"),MB_OK);
}
LoadTimeDLL.h
//Add the include guards to protect from cyclic an redundant inclusions
#ifndef _LOAD_TIME_DLL_H
#define _LOAD_TIME_DLL_H
#ifdef LOAD_TIME_DLL_EXPORTS
// This is syntax that has to be followed to export a function that can be used
// from other module, which loaded this dll. “__declspec(dllexport)” tells the
// compiler to export the definition of this function.
extern __declspec(dllexport) void SayLoadTimeDLL() ;
#else
// This is syntax that has to be visible for the module that uses this function.
// “__declspec(dllimport)” tells the compiler to bring the definition of this
// function from a DLL.
extern __declspec(dllimport) void SayLoadTimeDLL() ;
#endif
#endif
Now Compile the project LoadTimeDLL. The out put should look like:
-------------Configuration: LoadTimeDLL - Win32 Debug------------
Compiling...
LoadTimeDLL.cpp
Linking...
Creating library Debug/LoadTimeDLL.lib and object Debug/LoadTimeDLL.exp
LoadTimeDLL.dll - 0 error(s), 0 warning(s)
If the generation of “LoadTimeDLL.lib” is absent, it means no functions were exported and DLL cannot be loaded or used.
(To test, change the name of the SayLoadtimeDLL to SayLoadtimeDll. Observe case.)
What you’ve done is creating a library that you can implicitly link to.
Now perform the following steps to create a library that you’ll link to explicitly:
• Add a new “Win32 Dynamic-Link Library” project with name “RunTimeDLL” to the workspace “DLLsTraining”.
• Choose an empty DLL project
• Add a new file named “RunTimeDLL.cpp”, “RunTimeDLL.h” and “RunTimeDLL.def” to RunTimeDLL project.
• Add code shown below to “RunTimeDLL.cpp” and “RunTimeDLL.h”
RunTimeDLL.cpp
#include <windows.h>
#include "RunTimeDLL.h"
// Definition of the function which will be exported to other modules.
void SayRunTimeDLL()
{
MessageBox( NULL,
TEXT("I am a run time DLL"),
TEXT(“Information"),
MB_OK );
}
RunTimeDLL.h
// Add the include guards to protect from cyclic an redundant inclusions
#ifndef _RUN_TIME_DLL_H
#define _RUN_TIME_DLL_H
// Here the function will be exported using .def file. So no need to export using the
// declspec. This is the way to export the function from an dll that can be loaded at
// runtime and asked for the addresses of the function.
void SayRunTimeDLL() ;
#endif
RunTimeDLL.def
LIBRARY "RunTimeDLL"
EXPORTS
SayRuntimeDLL
Now Compile the project RunTimeDLL. The output should look like:
-------------Configuration: RunTimeDLL - Win32 Debug--------------
Compiling...
RunTimeDLL.cpp
Linking...
Creating library Debug/RunTimeDLL.lib and object Debug/RunTimeDLL.exp
RunTimeDLL.dll - 0 error(s), 0 warning(s)
If the generation of “RunTimeDLL.lib” is absent, it means no functions were exported and DLL cannot be loaded used.
(To test change the name of the SayRunTimeDLL to SayRunTimeDll in .def file. Observe case.)
2. Exporting Symbols
As I explained earlier that by using the __declspec (dllexport) keyword you made that dll export the particular symbol. Alternatively as in the RunTimeDLL when you create a .def file you state the function names to be exported.
3. Creating an Import Library
When you build the projects in the IDE.The .lib file produced with the DLL is the import library that your applications may link to if they need to use the function that you wrote in the DLL.
Next: Points of Interest >>
More Code Examples Articles
More By Digvijay Chauhan