You:
1) call LoadLibrary to load the dll.
2) call GetProcAddress to obtain the address of the function to call. This will be returned as a FARPROC.
3) Use reinterpret_cast<> () to type cast the FARPROC to a function pointer having the correct arguments and return type
4) call the function using the function pointer
5) call FreeLibrary when you are finished with the dll
Note:
A dll is a relic from the C age. That means no functtion overloading and that means no name mangling on the C++ side. The C++ functions must be extern "C" or the dll must have been built using a DEF file containing the export names of the mangled C++ function names.
Example:
-
-
int main()
-
{
-
cout << "Calling functions from a dll" << endl;
-
-
//First, load the dll into memory
-
HMODULE theDll = LoadLibrary "C:\\Scratch\\ClassDemos\\ADll\\Debug\\ADll.dll");
-
if (!theDll)
-
{
-
cout << "The dll failed to load" << endl;
-
return 1;
-
}
-
-
//Second, get the address of the desried function from the dll
-
FARPROC addr = GetProcAddress(theDll, "DisplayFromDll");
-
if (!addr)
-
{
-
//Look up the error in the system errors list
-
unsigned int what = GetLastError();
-
if (what == ERROR_PROC_NOT_FOUND)
-
{
-
cout << "Function not found in the dll" << endl;
-
}
-
else
-
{
-
cout << "Error: " << what << endl;
-
}
-
return 2;
-
}
-
cout << "The function has been located in the dll" << endl;
-
//Declare a function pointer that can accept the address of the function.
-
//You will need to know the function prototype to do this.
-
//Dll function prototypes should be provided by the vendor of the dll
-
void (__stdcall *DisplayFromDll)();
-
//Type-cast the address returned from GetProcAddress to the function pointer type
-
DisplayFromDll = reinterpret_cast<void (__stdcall *)()> (addr);
-
//Now use the function pointer to call the function:
-
DisplayFromDll();
-
-
//If you don't use a .def file in the dll, you must use the mangled name
-
//Second, get the address of the desried function from the dll
-
addr = GetProcAddress(theDll, "AreaOfSquare");
-
if (!addr)
-
{
-
//Look up the error in the system errors list
-
unsigned int what = GetLastError();
-
if (what == ERROR_PROC_NOT_FOUND)
-
{
-
cout << "Function not found in the dll" << endl;
-
}
-
else
-
{
-
cout << "Error: " << what << endl;
-
}
-
return 2;
-
}
-
-
cout << "The function has been located in the dll" << endl;
-
//Declare a function pointer that can accept the address of the function.
-
//You will need to know the function prototype to do this.
-
//Dll function prototypes should be provided by the vendor of the dll
-
int (__stdcall *AreaOfSquare)(int, int);
-
//Type-cast the address returned from GetProcAddress to the function pointer type
-
AreaOfSquare = reinterpret_cast<int (__stdcall*)(int,int)> (addr);
-
//Now use the function pointer to call the function:
-
cout << "The area of a 6x8 square is " << AreaOfSquare(6,8) << endl;;
-
-
//Finally, unload the dll from memory
-
FreeLibrary(theDll);
-
-
return 0;
-
}
-
The dll code:
-
-
#include <iostream>
-
using namespace std;
-
-
void __stdcall DisplayFromDll()
-
{
-
cout << "This function was called from ADLL.dll" << endl;
-
}
-
-
int __stdcall AreaOfSquare (int len, int wid)
-
{
-
return len * wid;
-
}
-
and the dll DEF file:
-
;BEGIN ADLL.DEF FILE
-
;This DEF file is required becuase the argument to GetProcAddress()
-
;for the function is a C-string and it will never be equal to the
-
;C++ mangled name for the function
-
;This DEF file maps the mangled name to a name that can be used with GetProcAddress()
-
;Note also: Change project settings in Visual Studio to send the LINK this def file.
-
;Visual Studio.NET: Project Properties/Linker/Input/Module Definition File/...Path to the def file\Adll.def
-
LIBRARY ADll
-
EXPORTS
-
;Exported Name C++ Mangled Name
-
AreaOfSquare = ?AreaOfSquare@@YGHHH@Z
-
DisplayFromDll = ?DisplayFromDll@@YGXXZ
-
;END DEF FILE
-