Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created April 10, 2021 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-mat/fb2843b99dedc15316d718396cdc7a6d to your computer and use it in GitHub Desktop.
Save t-mat/fb2843b99dedc15316d718396cdc7a6d to your computer and use it in GitHub Desktop.
[WIN32] Retrieve disk information from available drives.
// Retrieve disk information from available drives.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <locale.h>
#include <tuple>
std::tuple<uint64_t, uint64_t, uint64_t> getDiskFreeSpace(const wchar_t* path) {
wchar_t volumePath[256];
if(FALSE == GetVolumePathNameW(path, volumePath, static_cast<DWORD>(std::size(volumePath)))) {
return { 0, 0, 0 };
}
ULARGE_INTEGER freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes;
if(0 == GetDiskFreeSpaceExW(volumePath, &freeBytesAvailableToCaller, &totalNumberOfBytes, &totalNumberOfFreeBytes)) {
return { 0, 0, 0 };
}
return { freeBytesAvailableToCaller.QuadPart, totalNumberOfBytes.QuadPart, totalNumberOfFreeBytes.QuadPart };
}
void test() {
const DWORD drives = GetLogicalDrives();
for(int i = -1; i < 26; ++i) {
wchar_t path[3];
if(i == -1) {
path[0] = '.';
path[1] = 0;
path[2] = 0;
} else {
if(((drives >> i) & 1) == 0) {
continue;
}
path[0] = i + L'A';
path[1] = ':';
path[2] = 0;
}
const auto [freeBytesAvailableToCaller, totalNumberOfBytes, totalNumberOfFreeBytes] = getDiskFreeSpace(path);
const auto totalSize = totalNumberOfBytes / 1024.0 / 1024.0 / 1024.0;
const auto freeSize = freeBytesAvailableToCaller / 1024.0 / 1024.0 / 1024.0;
wprintf(L"path=%2s, totalSize=%8.2fGiB, free=%8.2fGiB\n", path, totalSize, freeSize);
}
}
int main() {
setlocale(LC_ALL, "");
test();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment