#include #include "DiskSpace.h" typedef BOOL (* diskEx) (LPCTSTR root, PULARGE_INTEGER a, PULARGE_INTEGER B, PULARGE_INTEGER C); DiskSpace::DiskSpace(const char *rootName) { char rootPath[128]; DWORD totalClusters; OSVERSIONINFO osVerInfo; // copy the rootname to our own path strcpy(rootPath, rootName); // get some guide to the os version osVerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osVerInfo); // if get disk free space fails then note fatal initialisation error if (!GetDiskFreeSpace(rootName, §orsPerCluster, &bytesPerSector, &freeClusters, &totalClusters)) { this->initialised = FALSE; return; } // call the advanced form of getDiskSpace if we have access to it; this // will cope with 2Gb+ partitions correctly if (osVerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && LOWORD(osVerInfo.dwBuildNumber) > 1000 || TRUE) { HMODULE module; FARPROC proc; ULARGE_INTEGER freeBytes, freeToCaller, totalBytes; module = LoadLibrary("KERNEL32.DLL"); proc = GetProcAddress(module, "GetDiskFreeSpaceEx"); if (proc == NULL) { this->initialised = TRUE; return; } (* ((diskEx) proc))(rootPath, &freeBytes, &freeToCaller, &totalBytes); // Get free clusters - use the ULARGE_INTEGER variables to get the // result and avoid overflow problems //freeBytes = freeBytes / sectorsPerCluster; //(sectorsPerCluster * bytesPerSector); //freeClusters = totalBytes; } this->initialised = TRUE; return; } BOOL DiskSpace::initialisedOk() { return this->initialised; } unsigned double DiskSpace::totalFreeSpace() { return this->freeClusters * (((double) (this->sectorsPerCluster * this->bytesPerSector)) / (double) (1024)); } int DiskSpace::fileClusters(int fileSize) { int sectors, clusters; sectors = (fileSize + this->bytesPerSector - 1) / this->bytesPerSector; clusters = (sectors + this->sectorsPerCluster - 1) / this->sectorsPerCluster; return clusters; } int DiskSpace::fileSpace(int fileSize) { return this->fileClusters(fileSize) * this->bytesPerSector * this->sectorsPerCluster; }