Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to get the size of a physical disk?

How to get the size of a physical disk?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
7 Posts 4 Posters 9 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Maxwell Chen
    wrote on last edited by
    #1

    I would like to know the way to get the size of physical disks (hard disks).
    When I use IOCTL_DISK_GET_DRIVE_GEOMETRY, it tells me the size of \\.\physicaldisk0 is 33814126080 bytes = 32247 MB
    But actually it is a Maxtor 57GB hard disk, also WIndows Disk Management Tool tells me the size is 55.91 GB.
    Could anyone teach me a correct way to get the size of physical disks?
    Thanks. BuggyMax

    X R D 3 Replies Last reply
    0
    • M Maxwell Chen

      I would like to know the way to get the size of physical disks (hard disks).
      When I use IOCTL_DISK_GET_DRIVE_GEOMETRY, it tells me the size of \\.\physicaldisk0 is 33814126080 bytes = 32247 MB
      But actually it is a Maxtor 57GB hard disk, also WIndows Disk Management Tool tells me the size is 55.91 GB.
      Could anyone teach me a correct way to get the size of physical disks?
      Thanks. BuggyMax

      X Offline
      X Offline
      Xander80
      wrote on last edited by
      #2

      Does this help??? :confused::confused: LPCTSTR lpDirectoryName; // directory name ULARGE_INTEGER lpFreeBytesAvailable; // bytes available to caller ULARGE_INTEGER lpTotalNumberOfBytes; // bytes on disk ULARGE_INTEGER lpTotalNumberOfFreeBytes; // free bytes on disk lpDirectoryName = "c:"; lpFreeBytesAvailable.QuadPart = 0; lpTotalNumberOfBytes.QuadPart = 0; lpTotalNumberOfFreeBytes.QuadPart = 0; FARPROC pGetDiskFreeSpaceEx; pGetDiskFreeSpaceEx = GetProcAddress( GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA"); if (pGetDiskFreeSpaceEx) { GetDiskFreeSpaceEx (lpDirectoryName, (PULARGE_INTEGER)&lpFreeBytesAvailable, (PULARGE_INTEGER)&lpTotalNumberOfBytes, (PULARGE_INTEGER)&lpTotalNumberOfFreeBytes); } else { AfxMessageBox(_T("Error")); }

      M 1 Reply Last reply
      0
      • M Maxwell Chen

        I would like to know the way to get the size of physical disks (hard disks).
        When I use IOCTL_DISK_GET_DRIVE_GEOMETRY, it tells me the size of \\.\physicaldisk0 is 33814126080 bytes = 32247 MB
        But actually it is a Maxtor 57GB hard disk, also WIndows Disk Management Tool tells me the size is 55.91 GB.
        Could anyone teach me a correct way to get the size of physical disks?
        Thanks. BuggyMax

        R Offline
        R Offline
        Rage
        wrote on last edited by
        #3

        BuggyMax wrote: physicaldisk0 is 33814126080 byte How do you get that ? You should multiply : bytes per sector * sectors per track * track per cylinder * cylinders to get your size. If this does not work, maybe use IOCTL_DISK_GET_DRIVE_LAYOUT to get the partitions and IOCTL_DISK_GET_PARTITION_INFO to retrieve the sizes. ~RaGE();

        M 1 Reply Last reply
        0
        • M Maxwell Chen

          I would like to know the way to get the size of physical disks (hard disks).
          When I use IOCTL_DISK_GET_DRIVE_GEOMETRY, it tells me the size of \\.\physicaldisk0 is 33814126080 bytes = 32247 MB
          But actually it is a Maxtor 57GB hard disk, also WIndows Disk Management Tool tells me the size is 55.91 GB.
          Could anyone teach me a correct way to get the size of physical disks?
          Thanks. BuggyMax

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          This correctly reports that I have a (roughly) 13GB disk:

          HANDLE disk;
          DWORD dwNeeded;
          DISK_GEOMETRY *geometry;

          disk=CreateFile("\\\\.\\PHYSICALDRIVE0",
          GENERIC_READ,
          0,
          NULL,
          OPEN_EXISTING,
          0,
          NULL);
          if (INVALID_HANDLE_VALUE != disk)
          {
          geometry = new DISK_GEOMETRY;

          DeviceIoControl(disk,
               IOCTL\_DISK\_GET\_DRIVE\_GEOMETRY,
               NULL,
               0,
               geometry,
               sizeof(DISK\_GEOMETRY),
               &dwNeeded,
               NULL);
          
          TRACE("      Cylinders: %I64u\\n", geometry->Cylinders);
          TRACE("     Media Type: %d\\n",    geometry->MediaType);
          TRACE("Tracks/Cylinder: %lu\\n",   geometry->TracksPerCylinder);
          TRACE("  Sectors/Track: %lu\\n",   geometry->SectorsPerTrack);
          TRACE("   Bytes/Sector: %lu\\n",   geometry->BytesPerSector);
          
          delete \[\] geometry;
          
          CloseHandle(disk);
          

          }

          M 1 Reply Last reply
          0
          • X Xander80

            Does this help??? :confused::confused: LPCTSTR lpDirectoryName; // directory name ULARGE_INTEGER lpFreeBytesAvailable; // bytes available to caller ULARGE_INTEGER lpTotalNumberOfBytes; // bytes on disk ULARGE_INTEGER lpTotalNumberOfFreeBytes; // free bytes on disk lpDirectoryName = "c:"; lpFreeBytesAvailable.QuadPart = 0; lpTotalNumberOfBytes.QuadPart = 0; lpTotalNumberOfFreeBytes.QuadPart = 0; FARPROC pGetDiskFreeSpaceEx; pGetDiskFreeSpaceEx = GetProcAddress( GetModuleHandle("kernel32.dll"), "GetDiskFreeSpaceExA"); if (pGetDiskFreeSpaceEx) { GetDiskFreeSpaceEx (lpDirectoryName, (PULARGE_INTEGER)&lpFreeBytesAvailable, (PULARGE_INTEGER)&lpTotalNumberOfBytes, (PULARGE_INTEGER)&lpTotalNumberOfFreeBytes); } else { AfxMessageBox(_T("Error")); }

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #5

            Xander80 wrote: lpDirectoryName = "c:"; Thanks but "C:" is logical drive. What I need is size of physical disks. Thanks. BuggyMax

            1 Reply Last reply
            0
            • R Rage

              BuggyMax wrote: physicaldisk0 is 33814126080 byte How do you get that ? You should multiply : bytes per sector * sectors per track * track per cylinder * cylinders to get your size. If this does not work, maybe use IOCTL_DISK_GET_DRIVE_LAYOUT to get the partitions and IOCTL_DISK_GET_PARTITION_INFO to retrieve the sizes. ~RaGE();

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #6

              Rage wrote: You should multiply : bytes per sector * sectors per track * track per cylinder * cylinders to get your size. Yes that huge number (33814126080) is the result of multiplying the four values. Rage wrote: If this does not work, maybe use IOCTL_DISK_GET_DRIVE_LAYOUT to get the partitions and IOCTL_DISK_GET_PARTITION_INFO to retrieve the sizes. What if the disk contains NO partitions? ;) BuggyMax

              1 Reply Last reply
              0
              • D David Crow

                This correctly reports that I have a (roughly) 13GB disk:

                HANDLE disk;
                DWORD dwNeeded;
                DISK_GEOMETRY *geometry;

                disk=CreateFile("\\\\.\\PHYSICALDRIVE0",
                GENERIC_READ,
                0,
                NULL,
                OPEN_EXISTING,
                0,
                NULL);
                if (INVALID_HANDLE_VALUE != disk)
                {
                geometry = new DISK_GEOMETRY;

                DeviceIoControl(disk,
                     IOCTL\_DISK\_GET\_DRIVE\_GEOMETRY,
                     NULL,
                     0,
                     geometry,
                     sizeof(DISK\_GEOMETRY),
                     &dwNeeded,
                     NULL);
                
                TRACE("      Cylinders: %I64u\\n", geometry->Cylinders);
                TRACE("     Media Type: %d\\n",    geometry->MediaType);
                TRACE("Tracks/Cylinder: %lu\\n",   geometry->TracksPerCylinder);
                TRACE("  Sectors/Track: %lu\\n",   geometry->SectorsPerTrack);
                TRACE("   Bytes/Sector: %lu\\n",   geometry->BytesPerSector);
                
                delete \[\] geometry;
                
                CloseHandle(disk);
                

                }

                M Offline
                M Offline
                Maxwell Chen
                wrote on last edited by
                #7

                That source code was also what I used. But the values I got were: Cylinders: 4111 ( I use __int64 already.) TracksPerCylinder: 255 SectorsPerTrack: 63 BytesPerSector: 512 The result, as you see, is 33GB. But actually it is a 55.9GB one. It means amount of cylinders should be 6964. BuggyMax

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups