Link to home
Start Free TrialLog in
Avatar of Kdukes24
Kdukes24

asked on

Accurate LBA Total Sector Count

I am stumped on how to find an accurate representation of Sector count for a given hard disk.
I have tried wmi but the numbers don't jive with the manufacturer stated total sectors.

I have tried using WMI from GetObject("WinMgmts:").InstancesOf("Win32_DiskDrive")
it 'seems' to return the number of sectors in the volume and not the physical drive.
Uusing winapi :
DeviceIoControl(hDrive, _
                      DFP_RECEIVE_DRIVE_DATA, _
                      SCIP, _
                      Len(SCIP) - 4, _
                      bArrOut(0), _
                      OUTPUT_DATA_SIZE, _
                      cbBytesReturned, _
                      ByVal 0&)
I get a structure in the form of
Private Type IDSECTOR
   wGenConfig                 As Integer
   wNumCyls                   As Integer
   wReserved                  As Integer
   wNumHeads                  As Integer
   wBytesPerTrack             As Integer
   wBytesPerSector            As Integer
   wSectorsPerTrack           As Integer
   wVendorUnique(2)           As Integer
   sSerialNumber(19)          As Byte
   wBufferType                As Integer
   wBufferSize                As Integer
   wECCSize                   As Integer
   sFirmwareRev(7)            As Byte
   sModelNumber(39)           As Byte
   wMoreVendorUnique          As Integer
   wDoubleWordIO              As Integer
   wCapabilities              As Integer
   wReserved1                 As Integer
   wPIOTiming                 As Integer
   wDMATiming                 As Integer
   wBS                        As Integer
   wNumCurrentCyls            As Integer
   wNumCurrentHeads           As Integer
   wNumCurrentSectorsPerTrack As Integer
   ulCurrentSectorCapacity    As Long
   wMultSectorStuff           As Integer
   ulTotalAddressableSectors  As Long
   wSingleWordDMA             As Integer
   wMultiWordDMA              As Integer
   bReserved(127)             As Byte
End Type

ulTotalAddressableSectors  As Long
returns a long value that I am not sure of how to interpret.

This snippet is from the main sample code that is on this site as well as most of google regarding reading /  writing hard drives from vb6.

I have changed the declaration of ulTotalAddressableSectors and also ulCurrentSectorCapacity to currency but the numbers still don't make sense in relation to the physical disks real sector count.


Avatar of Kdukes24
Kdukes24

ASKER

Const IOCTL_DISK_GET_LENGTH_INFO = &H7405C
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByRef lpInBuffer As Any, ByVal nInBufferSize As Long, ByRef lpOutBuffer As Any, ByVal nOutBufferSize As Long, ByRef lpBytesReturned As Long, ByVal lpOverlapped As Long) As Long

hdevice = handle to \\.\PhysicalDrive0
Public Function GetDiskPartitionInfo(hDevice As Long) As DISK_GEOMETRY
   
   Dim bytesReturned As Long
    Dim DiskLen As Currency
  'Another handle check!
   If hDevice <> INVALID_HANDLE_VALUE Then
       
       'Call the function. The returned
       'information is passed directly
       'to the return value of this function.
        DeviceIoControl hDevice, _
                        IOCTL_DISK_GET_LENGTH_INFO, _
                        Null, _
                        0&, _
                        DiskLen, _
                        LenB(DiskLen), _
                        bytesReturned, _
                        ByVal 0&
    End If
    DiskLen = DiskLen * 10000
End Function
Mods, How do I close my own question and answer?
ASKER CERTIFIED SOLUTION
Avatar of Kdukes24
Kdukes24

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial