Notes:
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,
§orsPerCluster,
&bytesPerSector,
&freeClusters,
&totalClusters) )
{ return sectorsPerCluster * bytesPerSector * freeClusters;
}
else
{ return 0;
}
}
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; }