Skip to content

SysWin Dmi Disks

Paul Squires edited this page Nov 30, 2026 · 1 revision

Home › All Categories › sys/win › sys/win/dmi › Disk functions

Disk functions

Returns a disk's size, geometry, model and serial number, and the disk a volume is on.

The functions open a device by its path and send its driver an I/O control code with DeviceIoControl. They open it for no access, only to ask, so they need no administrator rights and do not lock or touch the disk.

Paths. A device path is written with forward or backward slashes:

Path Opens
//./PhysicalDrive0, //./PhysicalDrive1 … A whole disk, by the number Windows gave it. The default of every function that has one.
//./C: A volume, by its drive letter: no trailing slash.

Disk numbers are not drive letters and need not follow them: C: is often not on disk 0. GetVolumeDiskNumber finds the disk of a volume.

Results. Except GetVolumeDiskNumber, each function returns a tuple: the value and a Windows error code, 0 on success. On failure the value is 0 or "":

var (nSize, dwErr) = GetDiskSize( "//./PhysicalDrive0" )
if dwErr then print GetWinErrMsg( dwErr ) else print nSize; " bytes"

ERROR_FILE_NOT_FOUND (2) means there is no such device. Take a single field with its name when the error does not matter: GetDiskSize().diskSize.

Each call opens and closes the device again; the geometry functions each read the whole DISK_GEOMETRY_EX.

Importing

#import "sys/win"
#import "sys/win/dmi"
using win, dmi

Example

Lists every disk with its model, bus, size, sector size and serial number:

#import "sys/win"
#import "sys/win/dmi"
using win, dmi

function main() as long
    for i as int32 = 0 to 15
        dim ustrDrive as ustring = "//./PhysicalDrive" & i
        var (pDesc, dwErr) = GetStorageDeviceDescriptor( ustrDrive )
        if dwErr = ERROR_FILE_NOT_FOUND then continue for   ' no disk with this number
        if dwErr then
            print ustrDrive; ": "; GetWinErrMsg( dwErr )
            continue for
        end if
        print ustrDrive; ": "; trim( GetStorageDeviceDescriptorString( pDesc, pDesc->ProductIdOffset ) ); _
              ", bus type"; pDesc->BusType
        Deallocate( pDesc )

        var (nSize, e1) = GetDiskSize( ustrDrive )
        var (nBps, e2) = GetBytesPerSector( ustrDrive )
        var (nMedia, e3) = GetMediaType( ustrDrive )
        print "    "; nSize \ 1000000000; " GB,"; nBps; " bytes a sector, "; _
              iif( nMedia = FixedMedia, "fixed", "removable" )
        print "    serial "; GetDeviceSerialNumber( ustrDrive ).serialNumber
    next
    return 0
end function

end main()

On one desktop it prints (its serial numbers replaced here):

//./PhysicalDrive0: P3-2TB, bus type 11
     2048 GB,512 bytes a sector, fixed
    serial 1234567890123
//./PhysicalDrive1: WD_BLACK SN850X 2000GB, bus type 17
     2000 GB,512 bytes a sector, fixed
    serial ABCD_0000_1234_0001_001B_448B_0000_0001.

Bus type 11 is SATA and 17 NVMe (BusTypeSata, BusTypeNvme); 7 is USB. The serial number is the string the drive reports, punctuation included.

Functions

Function Does
GetDiskSize Returns the size of a disk in bytes.
GetBytesPerSector Returns the number of bytes in a sector.
GetCylinders Returns the number of cylinders.
GetTracksPerCylinder Returns the number of tracks per cylinder.
GetSectorsPerTrack Returns the number of sectors per track.
GetMediaType Returns the type of media, fixed or removable.
GetDeviceSerialNumber Returns the serial number of a disk.
GetStorageDeviceDescriptor Returns the vendor, product, serial and bus of a disk, in one structure.
GetStorageDeviceDescriptorString Returns one of the strings of that structure.
GetVolumeDiskNumber Returns the number of the disk a volume is on.

Reference

GetDiskSize

function GetDiskSize( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (diskSize as LONGLONG, errcode as DWORD)

Returns the size of a disk in bytes, as its driver reports it.

Parameter Meaning
wszPath The device path.

Returns the size and 0, or 0 and the error code.

Example

var (nSize, dwErr) = GetDiskSize( "//./PhysicalDrive1" )
if dwErr = 0 then print nSize \ ( 1024 * 1024 * 1024 ); " GiB"

Microsoft Learn: IOCTL_DISK_GET_DRIVE_GEOMETRY_EX

GetBytesPerSector

function GetBytesPerSector( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (bps as DWORD, errcode as DWORD)

Returns the number of bytes in a sector of the disk: 512 on most disks, 4096 on some.

Parameter Meaning
wszPath The device path.

Returns the sector size and 0, or 0 and the error code.

Example

var (bps, errcode) = GetBytesPerSector()
if errcode then print GetWinErrMsg( errcode ) else print "bytes per sector:"; bps

GetCylinders

function GetCylinders( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (cylinders as LONGLONG, errcode as DWORD)

Returns the number of cylinders of the disk. Modern disks have no real cylinders; the driver reports a geometry that multiplies out to about the disk's size, with GetTracksPerCylinder, GetSectorsPerTrack and GetBytesPerSector.

Parameter Meaning
wszPath The device path.

Returns the cylinders and 0, or 0 and the error code.

Example

print "cylinders:"; GetCylinders().cylinders

GetTracksPerCylinder

function GetTracksPerCylinder( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (tracks as DWORD, errcode as DWORD)

Returns the number of tracks per cylinder (heads) in the geometry the driver reports.

Parameter Meaning
wszPath The device path.

Returns the tracks and 0, or 0 and the error code.

Example

print "tracks per cylinder:"; GetTracksPerCylinder().tracks

GetSectorsPerTrack

function GetSectorsPerTrack( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (spt as DWORD, errcode as DWORD)

Returns the number of sectors per track in the geometry the driver reports.

Parameter Meaning
wszPath The device path.

Returns the sectors and 0, or 0 and the error code.

Example

dim nCapacity as int64 = GetCylinders().cylinders * GetTracksPerCylinder().tracks _
                       * GetSectorsPerTrack().spt * GetBytesPerSector().bps
print "geometry capacity:"; nCapacity; " of"; GetDiskSize().diskSize

GetMediaType

function GetMediaType( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (mediaType as MEDIA_TYPE, errcode as DWORD)

Returns the type of media in the drive: FixedMedia (12) for a hard disk or SSD, RemovableMedia (11) for other removable media, or one of the floppy formats of the MEDIA_TYPE enumeration.

Parameter Meaning
wszPath The device path.

Returns the media type and 0, or 0 (Unknown) and the error code.

Example

if GetMediaType( "//./PhysicalDrive1" ).mediaType = RemovableMedia then print "removable"

Microsoft Learn: MEDIA_TYPE

GetDeviceSerialNumber

function GetDeviceSerialNumber( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (serialNumber as string, errCode as DWORD)

Returns the serial number the disk reports through IOCTL_STORAGE_QUERY_PROPERTY. This is the hardware's number, not the volume serial number that dir shows. On a volume path such as //./C: it is the serial number of the disk the volume is on.

Parameter Meaning
wszPath The device path.

Returns the serial number and 0, or "" and the error code: ERROR_INVALID_ADDRESS when the device reports no serial number.

Example

var (strSerial, dwErr) = GetDeviceSerialNumber( "//./C:" )
if dwErr then print GetWinErrMsg( dwErr ) else print "serial: "; trim( strSerial )

Microsoft Learn: IOCTL_STORAGE_QUERY_PROPERTY

GetStorageDeviceDescriptor

function GetStorageDeviceDescriptor( byref wszPath as wstring = "//./PhysicalDrive0" ) _
    as (pdesc as PSTORAGE_DEVICE_DESCRIPTOR, errcode as DWORD)

Returns the device's STORAGE_DEVICE_DESCRIPTOR: the bus type (BusType), whether the media is removable (RemovableMedia), and the offsets of the vendor, product, revision and serial number strings, which GetStorageDeviceDescriptorString reads.

The structure is allocated for you: free it with Deallocate when you are done.

Parameter Meaning
wszPath The device path.

Returns a pointer to the descriptor and 0, or 0 and the error code.

Example

var (pDesc, dwErr) = GetStorageDeviceDescriptor( "//./PhysicalDrive0" )
if dwErr then
    print GetWinErrMsg( dwErr )
else
    print "vendor:   "; GetStorageDeviceDescriptorString( pDesc, pDesc->VendorIdOffset )
    print "product:  "; GetStorageDeviceDescriptorString( pDesc, pDesc->ProductIdOffset )
    print "revision: "; GetStorageDeviceDescriptorString( pDesc, pDesc->ProductRevisionOffset )
    print "bus type:"; pDesc->BusType
    Deallocate( pDesc )
end if

Microsoft Learn: STORAGE_DEVICE_DESCRIPTOR

GetStorageDeviceDescriptorString

function GetStorageDeviceDescriptorString( byval pdesc as PSTORAGE_DEVICE_DESCRIPTOR, _
    byval offset as DWORD ) as string

Returns the string at one of the descriptor's offsets: VendorIdOffset, ProductIdOffset, ProductRevisionOffset or SerialNumberOffset.

Parameter Meaning
pdesc A descriptor from GetStorageDeviceDescriptor.
offset One of its offset fields.

Returns the string, or "" when the offset is 0: the device does not report that string. Many NVMe disks report no vendor string; their product string names the maker.

Example

print GetStorageDeviceDescriptorString( pDesc, pDesc->SerialNumberOffset )

GetVolumeDiskNumber

function GetVolumeDiskNumber( byref wszVolume as wstring ) as long

Returns the number of the disk a volume is on, the n of //./PhysicalDriven. A volume that spans several disks gives the first.

Parameter Meaning
wszVolume The volume path, such as //./C:.

Returns the disk number, or −1 if the volume cannot be opened or asked. It returns no error code.

Example

dim nDisk as int32 = GetVolumeDiskNumber( "//./C:" )
if nDisk >= 0 then print "C: is on PhysicalDrive"; nDisk

Microsoft Learn: IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS

See also

Clone this wiki locally