Announcement

Collapse
No announcement yet.

Get HDD Cluster Size

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Get HDD Cluster Size

    Would GetDiskFreeSpace be the right call to
    use to obtain the cluster size of a given HDD?

    Thanks

    Michael Ritter


  • #2
    GetDiskFreeSpace does not work correctly with large HDD's.
    GetDiskFreeSpaceEx does, but of course MS has taken away the
    possibility to get cluster data etc. from that one.

    One could always use GetVolumeInformation to determine if the
    HDD uses FAT32 and then create a look-up table with different
    cluster sizes, in regards to the total drive size, which you
    can get by using GetDiskFreeSpaceEx for big HDD's (above 2.1 GB)

    Otherwise, I don't know how to do it properly in Win85/98/NT2000,
    but I think it's a bit tricky to cover them all in one take.
    Someone called DKittrell posted the following table in the
    Programming-forum in November 21, 1998, which I guess could
    be useful here:
    Code:
    ' Partition or        FAT32
    ' Logical Volume      Cluster Size
    '--------------------------------
    ' less than 512 meg  (must use FAT16)
    ' 512 Meg to  8 Gig      4,096
    '   8 Gig to 16 Gig      8,192
    '  16 Gig to 32 Gig     16,384
    '  Over 32 Gig ¿?       32,768
    '
    ' Max partition size is 2 terabytes in FAT32.
    ------------------

    Comment


    • #3
      When I try to think more clearly about it, there must be a way to
      retrive cluster data directly from the HDD. Does anyone know if
      (rather, how) this can be done? Some API call I've missed?

      If one go to the DOS prompt, one can use CHKDSK to get cluster
      size, so it should be possible, or..?

      The funny thing is, I have some 16-bit asm code here that works
      fine in all systems, but since it uses Dos3Call - which isn't
      available in 32-bit code - it can't be used in 32-bit software.
      Still it works with 16-bit software, running in Win95/98/NT/2000..


      ------------------

      Comment


      • #4
        GetFreeDiskSpaceEx may or may not be available on a given system. It is best to look for it dynamically, using LoadLibrary. I believe we have details on this in the FAQ or in previous forum messages.


        ------------------
        Tom Hanlin
        PowerBASIC Staff

        Comment


        • #5
          Borje --
          99,99% sure that Chkdsk uses GetDiskFreeSpace.
          Reason : both return incorrect values for my FAT drive.
          Code:
             #Compile Exe
             #Register None
             #Dim All
             #Include "Win32Api.Inc"
             Function PbMain
                Dim SectorsPerCluster As Dword, BytesPerSector As Dword, _
                    NumberOfFreeClusters As Dword, TotalNumberOfClusters As Dword
                GetDiskFreeSpace "C:\", SectorsPerCluster, _
                BytesPerSector, NumberOfFreeClusters,  TotalNumberOfClusters
                MsgBox Str$(BytesperSector * SectorsPerCluster)
                AllocConsole
                Shell "chkdsk c:"
                Sleep 2000
                FreeConsole
             End Function


          [This message has been edited by Semen Matusovski (edited July 26, 2000).]

          Comment


          • #6
            I think if the freespace is beyond 2 gig, you may assume clustersize 4kb?

            But then, what do i know about FAT but food..


            ------------------
            [email protected]
            hellobasic

            Comment


            • #7
              Code:
              Michael--
                 
              For reliable results, you use DeviceIOControl.  Under 9x, you use 
              DeviceIOControl with the old Get Drive Parameters call.  Under NT/2K, 
              it's simpler.  You use DeviceIOControl with %IOCTL_DISK_GET_DRIVE_GEOMETRY.  
              If all of this sounds rather cryptic, begin with studying DeviceIOControl.  
                 
              Accessing hardware, even the physical drives, is possible under Win32 
              but rarely simple.  However, when identifying sector or cluster size, 
              accessing hardware is also not appropriate.  These geometries are a 
              function of the OS, which supplies the information when 
              queried.

              [This message has been edited by Greg Turgeon (edited July 26, 2000).]

              Comment


              • #8
                Semen - CHKDSK returns proper values on my disk,
                FAT32/Win98. ??

                Volume Serial Number is 07CF-0C16

                11,627,712 kilobytes total disk space
                307,136 kilobytes free

                8,192 bytes in each allocation unit
                1,453,464 total allocation units on disk
                38,392 available allocation units on disk

                655,360 total bytes memory
                618,480 bytes free

                It is becoming clear that GetFreeDiskSpace is not the
                answer. It is curious though how Chkdsk gets it right,
                (on my machine anyway). Windows knows the magic number
                because it calculates it when you right click on any
                given directory in explorer and choose properties.
                It calculates filebytes as well as bytes used by those
                files so it must know allocation size.


                Thanks for the lead Greg. Reading through the Knowledge
                Base, it looks like the right way to do it. If anyone
                has a snippet I would appreciate it, otherwise I will
                post later what I discover.


                ------------------


                [This message has been edited by Michael Ritter (edited July 26, 2000).]

                Comment


                • #9
                  Borje --
                  On my PC, under Win2000 chkdsk reports that cluster size for FAT-16 disk is 32768 bytes.
                  The same GetDiskFreeSpace (512 * 64).
                  But I opened chkdsk and didn't find expected GetDiskFreeSpace.
                  So, I agree - chkdsk calculates by another way.
                  I see references to exotic DLL.

                  ------------------

                  Comment


                  • #10
                    Posted a sample that seems to work fine with both FAT16 and FAT32
                    in the source code section. MS have "simplified" things in 32-bit
                    environments their usual way, see my code..


                    ------------------

                    Comment


                    • #11
                      Thanks Borje,

                      You beat me to the punch. That's where I was headed after
                      Greg suggested the DeviceIOControl functions. I also had
                      a hell of a time squeezing water from the MSDN stone
                      on this one. Seems to work fine on my machine(98),
                      anyone care to try it on NT? I will give it a good
                      workover with various cluster sizes and OS's asap and
                      let you know the results.

                      Michael



                      ------------------

                      Comment


                      • #12
                        I have only tested it in Win98, on a FAT16 and a FAT32 drive, with
                        good results. According to MS, it should work in NT/2000 too.

                        BTW, just fixed an Error in the code. "IF IsOSR2 = 1 THEN" should
                        be "IF IsOSR2 THEN", since that function returns true or false.


                        ------------------

                        Comment

                        Working...
                        X