Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,707 questions

55,467 answers

573 users

How to get the total disk size using the Win32 API in C++

2 Answers

0 votes
#include <windows.h>
#include <iostream>
#include <string>
#include <iomanip>

int main() {
    std::wstring rootPath = L"C:\\";

    ULARGE_INTEGER freeBytesAvailable{};
    ULARGE_INTEGER totalBytes{};
    ULARGE_INTEGER totalFreeBytes{};

    BOOL ok = GetDiskFreeSpaceExW(
        rootPath.c_str(),
        &freeBytesAvailable,
        &totalBytes,
        &totalFreeBytes
    );

    if (!ok) {
        std::wcerr << L"Error: " << GetLastError() << L"\n";
        return 1;
    }

    constexpr double GB = 1024.0 * 1024.0 * 1024.0;

    double totalGB = static_cast<double>(totalBytes.QuadPart) / GB;
    double freeGB = static_cast<double>(totalFreeBytes.QuadPart) / GB;

    std::wcout << L"Disk: " << rootPath << L"\n";
    std::wcout << L"Total size: " << totalBytes.QuadPart
        << L" bytes (" << std::fixed << std::setprecision(2)
        << totalGB << L" GB)\n";

    std::wcout << L"Free space: " << totalFreeBytes.QuadPart
        << L" bytes (" << std::fixed << std::setprecision(2)
        << freeGB << L" GB)\n";

    return 0;
}



/*
run:

Disk: C:\
Total size: 999559262208 bytes (930.91 GB)
Free space: 284177965056 bytes (264.66 GB)

*/

 



answered Jan 30 by avibootz
0 votes
#include <windows.h>
#include <iostream>
#include <string>
#include <iomanip>

struct DiskInfo {
    unsigned long long totalBytes{};
    unsigned long long freeBytes{};
};

DiskInfo getDiskInfo(const std::wstring& rootPath) {
    ULARGE_INTEGER freeBytesAvailable{};
    ULARGE_INTEGER totalBytes{};
    ULARGE_INTEGER totalFreeBytes{};

    BOOL ok = GetDiskFreeSpaceExW(
        rootPath.c_str(),
        &freeBytesAvailable,
        &totalBytes,
        &totalFreeBytes
    );

    if (!ok) {
        throw std::runtime_error("GetDiskFreeSpaceExW failed with error " +
            std::to_string(GetLastError()));
    }

    DiskInfo info;
    info.totalBytes = totalBytes.QuadPart;
    info.freeBytes = totalFreeBytes.QuadPart;

    return info;
}

int main() {
    std::wstring rootPath = L"C:\\";

    try {
        DiskInfo info = getDiskInfo(rootPath);

        constexpr double GB = 1024.0 * 1024.0 * 1024.0;

        double totalGB = static_cast<double>(info.totalBytes) / GB;
        double freeGB = static_cast<double>(info.freeBytes) / GB;

        std::wcout << L"Disk: " << rootPath << L"\n";
        std::wcout << L"Total size: " << info.totalBytes
            << L" bytes (" << std::fixed << std::setprecision(2)
            << totalGB << L" GB)\n";

        std::wcout << L"Free space: " << info.freeBytes
            << L" bytes (" << std::fixed << std::setprecision(2)
            << freeGB << L" GB)\n";
    }
    catch (const std::exception& ex) {
        std::cerr << "Error: " << ex.what() << "\n";
        return 1;
    }

    return 0;
}




/*
run:

Disk: C:\
Total size: 999559262208 bytes (930.91 GB)
Free space: 284546682880 bytes (265.00 GB)

*/

 



answered Jan 30 by avibootz
...