Back to Amir Israeli Homepage

[CDiskSpace : Getting size of Disk (total+free) space :(doesn't use Shell32.dll) ]

This article was contributed by Amir Israeli Amir Israeli.
Amir Israeli Web Site

Environment: Win 95/98 VC6 SP4

How many of you want to know the total size of disk, and the free size of disk ?
A quick look into windows takes us to two functions GetDiskFreeSpaceEx , GetDiskFreeSpace. :These functions are located in kernel32.dll and present a problem : they are not always implemented . (Well , today most people use OSes better than Windows 95 OSR2 , if you want to ensure youself with less checks dont use them.
Another problem with GetDiskFreeSpace() is : that you need to do your own calculations (It is a greater function to get the characteristics of the hard-disk then getting directly the free/tootal space.
The shell also provides a nice function : with the same name but SH as prefix SHGetDiskFreeSpace : but this function is not always implemented in Shell32.dll. (It is implemented only from Version 4.71 = Win98).

My searches for a function (I looked for a function that did the job in prior versions to Win95 , even in the old friendly ugly DOS) took me to the headers "dos.h" and "direct.h" where a function named "_getdiskfree" uses a structure named "_diskfree_t" . This function is always implemented : it's located inside the C++ libraries , and the structure is big enough for any existing disk size .

The class provides 4 functions with default parameters , in general disk space (free/total) is returned as float value . You must provide the float value. the index of logical drive (or its' string representation) and requested format of answer : "mb" : the size will be as MB. "kb" will be as KB etc.

	BOOL GetDiskSpace(float &fOut,int nIndex,LPCTSTR lpszFormat=_T("mb"));
	BOOL GetDiskSpace(float &fOut,LPCTSTR lpszDisk,LPCTSTR lpszFormat=_T("mb"));
	BOOL GetDiskFreeSpace(float &fOut,int nIndex,LPCTSTR lpszFormat=_T("mb"));
	BOOL GetDiskFreeSpace(float &fOut,LPCTSTR lpszDisk,LPCTSTR lpszFormat=_T("mb"));
The class does additional checks : it check for availability of disk-drive , The format. These functions are protected and are only used internally by the class.


Downloads

Download demo project - 37 Kb
Download source - 2 Kb

History

Date Posted: December 20, 2000

Home  |  Links  |  About

Make your own free website on Tripod.com