출력

C:\ Total : 20002 MByte  Free : 1225 MByte
D:\ Total : 132614 MByte         Free : 26944 MByte
E:\ Total : 61443 MByte  Free : 61373 MByte
F:\ Total : 171898 MByte         Free : 156790 MByte
G:\ Total : 5130 MByte   Free : 3876 MByte
                http://fehead.tistory.com


소스

#include <windows.h>
#include <iostream>
#include <direct.h>

using namespace std;

struct ST_HDDUsage
{
    unsigned long   total;          // MByte
    unsigned long   free;           // MByte
};

class HddUsage
{
    typedef BOOL (WINAPI *GET_DISK_FREE_SPACE_EX)(
        LPCTSTR, PULARGE_INTEGER,
        PULARGE_INTEGER, PULARGE_INTEGER);

private:
    HINSTANCE       m_hInstLib;
    GET_DISK_FREE_SPACE_EX  m_pGetDiskFreeSpaceEx;

public:
    HddUsage()      :       m_hInstLib( 0 ), m_pGetDiskFreeSpaceEx( 0 )     {}
    ~HddUsage()             {       destroy();      }

    bool    init()
    {
        m_hInstLib = LoadLibrary( "kernel32.dll" );
        if( m_hInstLib == 0 )
            return false;

        m_pGetDiskFreeSpaceEx = reinterpret_cast<GET_DISK_FREE_SPACE_EX>(
            GetProcAddress( m_hInstLib, "GetDiskFreeSpaceExA") );

        if( m_pGetDiskFreeSpaceEx == 0 )
            return false;

        return true;
    }

    void destroy()
    {
        if( m_hInstLib )
            FreeLibrary( m_hInstLib);
        m_hInstLib = 0;
    }

    bool GetDiskUsage( const char * pszDrive, ST_HDDUsage * pHddUsage )
    {
        unsigned __int64 i64FreeBytesToCaller = 0;
        unsigned __int64 i64TotalBytes = 0;
        unsigned __int64 i64FreeBytes = 0;

        BOOL ret = m_pGetDiskFreeSpaceEx( pszDrive,
            reinterpret_cast<PULARGE_INTEGER>( &i64FreeBytesToCaller ),
            reinterpret_cast<PULARGE_INTEGER>( &i64TotalBytes ),
            reinterpret_cast<PULARGE_INTEGER>( &i64FreeBytes ) );
        if( !ret )
            return false;

        pHddUsage->total = static_cast<unsigned long >(
            i64TotalBytes / (1024*1024) );          // Hdd total (MByte)

        pHddUsage->free = static_cast<unsigned long >(
            i64FreeBytes / (1024*1024) );           // Hdd Free (MByte)

        return true;
    }

};

int main(int, char *[])
{
    HddUsage        hddUsage;
    if( hddUsage.init() ) {
      
        // 있는 하드 디스크만 출력.
        char    driveName[4] = "A:\\";
        for( ULONG uDriveMask = _getdrives() ; uDriveMask ; uDriveMask >>= 1 )
        {
            if( uDriveMask & 1 )
            {
                // 하드 디스크일때만 출력.
                if( GetDriveType( driveName ) == DRIVE_FIXED ) {
                    ST_HDDUsage     hdd;
                    if( hddUsage.GetDiskUsage( driveName, &hdd ) ) {
                        cout << driveName << " Total : " << hdd.total <<
                            " MByte\t Free : " << hdd.free << " MByte\n";
                    }
                }
            }
            ++driveName[0];         // 드라이브명 변경. A:\, B:\ ~~~Z:\~~
        }
    }
    cout << "\t\thttp://fehead.tistory.com\n";
    return 0;
}

+ Recent posts