win32 读取硬盘大小

读取内存大小和CPU个数什么的都很简单,硬盘要稍微麻烦点。

// SysInfo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#define BUFFERSIZE 255
long GetDiskSpaceInformation(LPSTR szDrive)
{
// printf("volume %s \n",szDrive);

    DWORD64 dwAvailableByte, dwTotalNumberOfByte=0,dwFreeNumberOfByte;
    if (!GetDiskFreeSpaceEx(szDrive,(PULARGE_INTEGER)&dwAvailableByte,
        (PULARGE_INTEGER)&dwTotalNumberOfByte,(PULARGE_INTEGER)&dwFreeNumberOfByte))
    {
        printf("cannot get disk free space \n");
    }
    else
    {
        printf("磁盘空间总大小(字节) : %I64d GB\n",dwTotalNumberOfByte/1024/1024/1024);
    }
  return dwTotalNumberOfByte/1024/1024/1024;
}

long GetDisksInformationEx()  
{  
    long total_size=0;
    printf("Begin Call GetDisksInformationEx()\n");  
    CHAR szLogicalDriveString[BUFFERSIZE]={0};  
    PCHAR szDrive;  

    GetLogicalDriveStrings(BUFFERSIZE - 1, szLogicalDriveString);  
    szDrive = szLogicalDriveString;  
    while (*szDrive)  
    {  
        if(GetDriveType(szDrive) == DRIVE_FIXED){
            printf("volume: %s\n",szDrive);
            long size = GetDiskSpaceInformation(szDrive);  
            total_size+=size;
            szDrive += (lstrlen(szDrive) + 1);  
        }else{//读到非本地磁盘
            break;
        }
    }  
    return total_size;
}  

int main(int argc, char* argv[])
{
    printf("硬盘大小: %I64dGB\n",GetDisksInformationEx()); 

    while(1);
    return 0;
}

你可能感兴趣的:(win32 读取硬盘大小)