Sample - DDLs in C++: Explicit Dynamic Linking with DEF-File Notes Code
Simple DLL that Prints a Text:
#include "iostream.h" //cout
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
return TRUE;
}
void WriteString()
{
cout << "This string printed from DLL ..." << endl;
};
DEF-file for DLL:
LIBRARY "DLL.DLL"
EXPORTS
WriteString @1
Calling Program in Console Application:
#include <windows.h> //for HINSTANCE
#include "iostream.h" //for cout
void main()
{
typedef void (* PFUNCTION)(void);
PFUNCTION pFunction;
HINSTANCE hLibrary = LoadLibrary("dll.dll"); // Load the DLL now
if (hLibrary != NULL)
{
/***********************************************************************
* This code is no differerent from snippet not using DEF-file, but *
* importantly GetProcAddress uses original function name. *
***********************************************************************/
pFunction = (PFUNCTION)GetProcAddress(hLibrary, "WriteString");
/***********************************************************************
* Oprionally, when DEF-file is used for explicit dynamic linking, we *
* could use function ordinal number from DEF-file to call the function.*
***********************************************************************/
//pFunction = (PFUNCTION)GetProcAddress(hLibrary, MAKEINTRESOURCE(1));
if(pFunction==NULL)
{
cout << "Function entry point not found ..." << endl;
}
else
{
(*pFunction)();
};
FreeLibrary(hLibrary);
}
else
{
cout << "Library not found ..." << endl;
};
}
Console Output:
This string printed from DLL ...
2002, Netston Consulting