2

Get simple information like serial number, bytes/sector, sectors/cluster, free and total disk space of your hard drive using Windows API calls.

// get hard drive info for the C: drive
// original Delphi Pascal, recoded in C and tested with Pelles C
// vegaseat      27nov2004 
// (add two \\ to the end of C: if missing)

#include <stdio.h>     // printf()
#include <windows.h>   // windows api header

int main()
{
  DWORD VolumeSerialNumber = 0;
  DWORD MaximumComponentLength = 0;
  DWORD FileSystemFlags = 0;
  
  DWORD SectorsPerCluster = 0;
  DWORD BytesPerSector = 0;  
  DWORD FreeClusters = 0;  
  DWORD TotalClusters = 0;

  DWORD FreeSpace = 0;      // free space available
  DWORD TotalSpace = 0;     // total space available

  GetVolumeInformation("C:\\", NULL, 0, &VolumeSerialNumber, &MaximumComponentLength,
                        &FileSystemFlags, NULL, 0);
  printf("Serial number for drive C = %X\n\n",VolumeSerialNumber);
  
  GetDiskFreeSpace("C:\\", &SectorsPerCluster, &BytesPerSector, 
                   &FreeClusters, &TotalClusters);
                   
  FreeSpace  = BytesPerSector * SectorsPerCluster * FreeClusters;
  TotalSpace = BytesPerSector * SectorsPerCluster * TotalClusters;
  
  printf("Bytes/Sector    = %d\n",BytesPerSector);
  printf("Sectors/Cluster = %d\n\n",SectorsPerCluster);
  
  printf("Drive C Total Disk Space: %Ld bytes\n",TotalSpace);  
  printf("Drive C Free  Disk Space: %Ld bytes\n",FreeSpace);
 
  getchar();  // wait
  return 0;
}
2
Contributors
1
Reply
4
Views
12 Years
Discussion Span
Last Post by sytheron
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.