Windows DLL Demo 3 - Example Program

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)

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

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

//============================================================
// title  : DiskFree.h
// purpose: required for creating/referencing DiskFree.DLL
// source : p.454 of "Using Visual-C++.NET" published by Que
// notes  : visit http://www.quepublishing.com
//============================================================
#ifndef __DISKFREE_H				//
#define __DISKFREE_H				//
#ifndef __DISKFREE__				// define when calling from DLL
#define __DISKFREELIB__ __declspec(dllimport)	// used when called from app
#else						//
#define __DISKFREELIB__ __declspec(dllexport)	// used when called from DLL
#endif						// ifndef else __DISKFREE__
//
// prototypes
//
__DISKFREELIB__ unsigned long DiskFree( unsigned int drive);
#endif						// ifndef __DISKFREE_H

//============================================================
// title  : DiskFree.cpp
// purpose: this program builds into DiskFree.DLL
// source : p.455 of "Using Visual-C++.NET" published by Que
// notes  : visit http://www.quepublishing.com
//============================================================
#include <afx.h>	// needed for winbase
#include <winbase.h>	// needed for kernel32 GetDiskFreeSpace
#define __DISKFREE__	// force 'dllexport' in DiskFree.h
#include "DiskFree.h"	//
//------------------------------------------------------------
// purpose: returns the amount of free space on the drive number
// params : 0= drive-A, 1= drive-B, 2= drive-C, 3= drive-D
// note   : the precompiler will substitute __DISKFREELIB__ with 
//          __declspec(dllexport)
//------------------------------------------------------------
__DISKFREELIB__ unsigned long DiskFree(unsigned int drive)
{ unsigned long bytesPerSector,
                sectorsPerCluster,
                freeClusters,
                totalClusters;

    char DrivePath[4] = { char(drive + 65), ':', '\\', '\0' };
    if (GetDiskFreeSpace( DrivePath,
                          &sectorsPerCluster,
                          &bytesPerSector,
                          &freeClusters,
                          &totalClusters) )
    { return sectorsPerCluster * bytesPerSector * freeClusters;
    }
    else
    { return 0;
    }
}

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

click file->new->projects->"Console Application"
enter "TestDiskFree" in the project name text box
click ok
select a "Hello World" project
click finish
edit "TestDiskFree.cpp" replace that code with the code below
copy files "DiskFree.h", "DiskFree.lib", "DiskFree.dll" to this directory
click project->settings->link then add DiskFree.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  : TestDiskFree.cpp
// purpose: calls the DiskFree function in DiskFree.DLL
// source : p.457 of "Using Visual-C++.NET" published by Que
// notes  : visit http://www.quepublishing.com
//============================================================
#include "stdafx.h"
#include <iostream>
#include "DiskFree.h"	// copied from the other project directory

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

int main(int argc, char * argv[])
{	//
	// call the DiskFree() function inside "DiskFree.dll"
	//
	cout << "Freespace: " << DiskFree(2) << endl;
	return 0;
}

 Back to Home
Neil Rieck
Waterloo, Ontario, Canada.