# windows API to find disk size



## sampl (Sep 11, 2006)

Is there any API that gives me the disk size by taking the drive letter as the input ? (in windows 2000) 

For example: 

If a disk of 120GB contains C: (40 GB), D: (20GB) and remaining raw. 

If the input is C: then GetDiskFreeSpaceEx() gives the partition size i.e, it gives the total size and free size in C: 

I want an API that takes the input C: and gives the entire disk size (120GB) 

Any help would be very much appreciated.


----------



## Rollin' Rog (Dec 9, 2000)

Don't know as this will help you any (I'm not really into coding), but as I moved your post here and see no responses, it might be useful.

Download and unzip the attached file.

Run it and select from among the clases offered, "W32_diskdrive".

Save the file with a .vbs extension and run it. It will produce a text file ("rename me text") of disk related info.

You can edit the vbs file to contain only the objects (such as "size") that you want.

The orginal scriptomatic was offered by Microsoft (http://www.microsoft.com/technet/scriptcenter/tools/wmimatic.mspx). This version was modified by Mosaic1 to produce a text output


----------



## dquigley (Apr 17, 2006)

There isn't a single API to do this I am aware of. But you can do it with a couple API calls. Look up IOCTL_DISK_GET_DRIVE_GEOMETRY_EX.

You'll have to make your own function that uses the CreateFile function to open the physical disk drive or volume. Be sure to read the section of the docs that show you how to "open" the physical drive. (search for \\.\PHYSICALDRIVE0). The CreateFile function used in that way returns a handle that can be used with the DeviceIoControl function, which will take IOCTL_DISK_GET_DRIVE_GEOMETRY_EX as a command parameter, giving you the physical drive size.

The thread you call from will need to run with Administrative privledges to gain access to that info.

Here is a code snippit I cut and pasted from the MSFT docs.


```
/* The code of interest is in the subroutine GetDriveGeometry. The 
   code in main shows how to interpret the results of the call. */

#include <windows.h>
#include <winioctl.h>
#include <stdio.h>

BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
  HANDLE hDevice;               // handle to the drive to be examined 
  BOOL bResult;                 // results flag
  DWORD junk;                   // discard results

  hDevice = CreateFile("\\\\.\\PhysicalDrive0",  // drive to open
                    0,                // no access to the drive
                    FILE_SHARE_READ | // share mode
                    FILE_SHARE_WRITE, // 
                    NULL,             // default security attributes
                    OPEN_EXISTING,    // disposition
                    0,                // file attributes
                    NULL);            // do not copy file attributes

  if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
  {
    return (FALSE);
  }

  bResult = DeviceIoControl(hDevice,  // device to be queried
      IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
                             NULL, 0, // no input buffer
                            pdg, sizeof(*pdg),     // output buffer
                            &junk,                 // # bytes returned
                            (LPOVERLAPPED) NULL);  // synchronous I/O

  CloseHandle(hDevice);

  return (bResult);
}

int main(int argc, char *argv[])
{
  DISK_GEOMETRY pdg;            // disk drive geometry structure
  BOOL bResult;                 // generic results flag
  ULONGLONG DiskSize;           // size of the drive, in bytes

  bResult = GetDriveGeometry (&pdg);

  if (bResult) 
  {
    printf("Cylinders = %I64d\n", pdg.Cylinders);
    printf("Tracks/cylinder = %ld\n", (ULONG) pdg.TracksPerCylinder);
    printf("Sectors/track = %ld\n", (ULONG) pdg.SectorsPerTrack);
    printf("Bytes/sector = %ld\n", (ULONG) pdg.BytesPerSector);

    DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
      (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,
           DiskSize / (1024 * 1024 * 1024));
  } 
  else 
  {
    printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());
  }

  return ((int)bResult);
}
```
Best,
Dan


----------



## sampl (Sep 11, 2006)

Thanks a lot Rollin' Rog & Dan..

This code does exactly what I needed. 
Thanks for all your time and help..


----------



## sampl (Sep 11, 2006)

Just one question Dan.. Where can I get the MSFT docs? Can you post the link..

Thanks in advance


----------



## dquigley (Apr 17, 2006)

Sure.

http://msdn.microsoft.com


----------



## blaqDeaph (Nov 22, 2005)

dquigley said:


> Sure.
> 
> http://msdn.microsoft.com


Although IMO I think you'd be better off googling up what you want, since MSDN doesn't allow you to sort your searches by programming lanauge etc.

Another good resource is www.codeguru.com, which provides code and examples (free) for interesting tasks.


----------

