Windows DLL Demo 2 - Simple DLL Project

What is a DLL? Many Windows applications, and Windows itself, are built built as a collection of callable DLLs (Dynamically Linked Libraries) rather than static executable binaries. For examples of this, check out the size of MS-Internet Explorer (IEXPLORE.EXE) which is only 89K, or the size of MS-Outlook Express (MSIMN.EXE) which is only 56K, yet both programs call the same "HTML rendering engine" which is implemented in the system DLLs. When you've got Outlook Express running at the same time as three instances of IE, there is only one set of DLLs loaded which definitely saves memory.

Notes:

  1. While IE is based upon hundreds of DLLs, other browsers like Firefox are static binary apps. This is why you can have multiple versions of Firefox on your system but only one version of IE
  2. Programmers coming from mini-computer or main-frame backgrounds may be more familiar with the term RTL (Run Time Library)

DllDemoSimple2.dll
build using Visual-C++ (version 6)

click file->new->projects->"Win32 Dynamic-Link Library"
enter "DllDemoSimple2" in the project name text box
click ok
select an simple DLL project
click finish
click file->new->files->"c++ source file"
enter "DllDemoSimple2.cpp" in the file name text box
click ok
paste the contents of DllDemoSimple2.h (below) into this window
click file->new->files->"c/c++ header file"
enter "DllDemoSimple2.h" in the file name text box
click ok
paste the contents of DllDemoSimple2.cpp (below) into this window
press key "F7" to build

//============================================================
// title  : DllDemoSimple2.h
// purpose: required for DllDemoSimple2.DLL
// author : Neil Rieck
// notes  : https://neilrieck.net
//============================================================
#ifndef __DllDemoSimple2_H				//
#define __DllDemoSimple2_H				//
#ifndef __DllDemoSimple2__				// define when calling from DLL
#define __DllDemoSimple2LIB__ __declspec(dllimport)	// used when called from app
#else							// 
#define __DllDemoSimple2LIB__ __declspec(dllexport)	// used when called from DLL 
#endif							// ifndef else __DllDemoSimple2__
//
// prototype(s)
//
// these functions will be tagged for either import or export
//
__DllDemoSimple2LIB__ unsigned long SimpleDouble( unsigned long whatever);
__DllDemoSimple2LIB__ unsigned long SimpleTriple( unsigned long whatever);
#endif							// ifndef __DllDemoSimple2_H

//============================================================
// title  : DllDemoSimple2.cpp
// purpose: this program builds into DllDemoSimple2.DLL
// author : Neil Rieck
// notes  : https://neilrieck.net
//============================================================
#include "stdafx.h"		//
#define __DllDemoSimple2__	// force export mode
#include "DllDemoSimple2.h"	// get exported prototypes
//
//	see MDSN info on how to expand DllMain()
//
BOOL APIENTRY DllMain(	HANDLE hModule, 
			DWORD ul_reason_for_call, 
			LPVOID lpReserved
			)
{	// Perform actions based on the reason for calling.
	switch( fdwReason ) 
	{ 
		case DLL_PROCESS_ATTACH:
		// Initialize once for each new process.
		// Return FALSE to fail DLL load.
		break;

		case DLL_THREAD_ATTACH:
		// Do thread-specific initialization.
		break;

		case DLL_THREAD_DETACH:
		// Do thread-specific cleanup.
		break;

		case DLL_PROCESS_DETACH:
		// Perform any necessary cleanup.
		break;
	}
	return TRUE;	// Successful DLL_PROCESS_ATTACH.
}
//------------------------------------------------------------
// this function doubles whatever it receives
// (it has been marked for export in "DllDemoSimple2.h")
//------------------------------------------------------------
unsigned long SimpleDouble( unsigned long whatever)
{	return (2 * whatever);
}
//------------------------------------------------------------
// this function triples whatever it receives
// (it has been marked for export in "DllDemoSimple2.h")
//------------------------------------------------------------
unsigned long SimpleTriple( unsigned long whatever)
{	return (3 * whatever);
}

DllDemoSimple2Test.exe
build using Visual-C++ (version 6)

click file->new->projects->"Console Application"
enter "DllDemoSimple2Test" in the project name text box
click ok
select a "Hello World" project
click finish
edit "DllDemoSimple2Test.cpp" replace that code with the code below
copy files "DllDemoSimple2.h", "DllDemoSimple2.lib", "DllDemoSimple2.dll" to this directory
click project->settings->link then add DllDemoSimple2.lib to the end of the "Object/Library Modules" text box
(just click inside the box then press the "end" key)
press key "F7" to build

//============================================================
// title  : DllDemoSimple2Test.cpp
// purpose: tests DllDemoSimple2.DLL
// author : Neil Rieck
// notes  : https://neilrieck.net
//============================================================
#include <iostream>
#include "DllDemoSimple2.h" // get our function definitions

using std::cout;
using std::endl;

void main()
{   cout << "DllDemoSimple2Test" << endl;
    cout << "==================" << endl;
    cout << "2*20=" << SimpleDouble(20) << endl;
    cout << "3*30=" << SimpleTriple(30) << endl;
}

Back to Home
Neil Rieck
Waterloo, Ontario, Canada.