So far, in order to use a library, whether static or
DLL, we made sure we new where the library was located. In fact, we kept
copying it in the directory where we created it and pasted in the
directory of the application that needed it. Suppose someone else or a
certain company created the library and distributed it, either with
Microsoft Windows or the DLL was installed with another application. To
use such a library, of course you can find it, copy it, and paste it in
the project where you want to use it. If many other programs are accessing
such a DLL, there would be no need to copy and paste it like that. These
types of DLL are installed in the Windows directory so any program that
needs them can access them from there.
As you can see, Microsoft Windows installed many DLLs
in the Windows or Windows\System32 directory. You too, when creating the
executable of your program, you can install the DLL there so you would
know with certainty where the DLL is located.
Practical Learning: Creating an MFC Static Library |
|
- To start a new application, open the New dialog box (File ->
New...) and, in the Projects property page, click Win32 Dynamic-Link
Libraries. In the Project Name box, type ExplicitDLL1 and press Enter
- In the Win32 Dynamic-Link Library Step 1 of 1, make sure the An
Empty DLL Project radio button is selected.
Click Finish then click OK
- To add a header file, on the main menu, click File -> New…
- In the New dialog box and in the Files property page, click C/C++ Header File
- In the Name edit box, type stdafx and press Enter
- In the empty file, type the following:
#ifndef STDAFX_H_
#define STDAFX_H_
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#endif // STDAFX_H_
|
- To add the accompanying source file, display the New dialog box and select C++
Source File
- In the Name edit box, type stdafx and press Enter
- In the empty file, type the following:
- To add a new source file, on the main menu, click File -> New...
- In the Name edit box, type exo1 and press Enter
- In the empty file, type the following:
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved )
{
return TRUE;
}
|
- To add a header file, on the main menu, click File -> New…
- In the New dialog box and in the Files property page, click C/C++ Header File
- In the Name edit box, type head and press Enter
- In the empty file, type the following:
#pragma once
double GetNumber(double n);
|
- To add the accompanying source file, display the New dialog box and select C++
Source File
- In the Name edit box, type head and press Enter
- In the empty file, type the following:
#include "stdafx.h"
#include "head.h"
double GetNumber(double Nbr)
{
return Nbr * 4.12;
}
|
- To add a definition file, display the New dialog box again
- In the Files property page, click Text File
- In the Name edit box, type "exo1.def" and
press Enter
- In the empty file, type the following:
LIBRARY ExplicitDLL1
EXPORTS
GetNumber
|
- To create the DLL, on the main menu, click Build -> Build
ExplicitDLL1.dll
- Open Windows Explorer or My Computer and locate the folder that contains the
above ExplicitDLL1 project then open its Debug folder
- Select and copy both the ExplicitDLL1.dll and the ExplicitDLL1.lib files
- Paste them in your Windows\System32 folder
The technique of calling a DLL as we have proceeded so
far is referred to as implicit loading because we knew how we created the
DLL and we added it to the directory of the application we were creating.
This allowed us to add the DLL and its library file to the project and
freed us from remembering to call the DLL by its name. Another technique
consists of calling the DLL by its name. This is referred to as explicit
call because you specify the name of the DLL you need and you don't have
to copy and paste it in your directory and you don't have to add it to the
project.
To call a DLL explicitly,
- You first call the LoadLibrary() function. Since/if creating
an MFC application, Microsoft recommends that you use the AfxLoadLibrary()
function
- To use the function you need from the DLL, call the GetProcAddress()
function
- When you have finished using the DLL, you can call the FreeLibrary()
function. This time too, Microsoft recommends using the AfxFreeLibrary()
function instead.
|
|