Skip to content

Instantly share code, notes, and snippets.

@khchen
Created November 9, 2021 18:25
Show Gist options
  • Select an option

  • Save khchen/b8d4ac4fc26c43bd791e6487c6af244d to your computer and use it in GitHub Desktop.

Select an option

Save khchen/b8d4ac4fc26c43bd791e6487c6af244d to your computer and use it in GitHub Desktop.
#[
Author: Ward
Example of DeviceIoControl
References:
https://docs.microsoft.com/en-us/windows/win32/devio/calling-deviceiocontrol
]#
import winim/lean
import winim/inc/winioctl
import sugar, strformat
# This converter is really convenient.
converter nimIntTocint(x: int): cint = cint x
const drive = r"\\.\PhysicalDrive0"
let hDevice = CreateFile(drive, 0, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0)
if hDevice != INVALID_HANDLE_VALUE:
defer: CloseHandle(hDevice)
var
dg: DISK_GEOMETRY
junk: DWORD
if DeviceIoControl(hDevice, IOCTL_DISK_GET_DRIVE_GEOMETRY, nil, 0,
&dg, sizeof(dg), &junk, nil) != 0:
dump drive
dump dg.Cylinders.QuadPart
dump dg.MediaType
dump dg.TracksPerCylinder
dump dg.SectorsPerTrack
dump dg.BytesPerSector
let diskSize = dg.Cylinders.QuadPart * dg.TracksPerCylinder *
dg.SectorsPerTrack * dg.BytesPerSector
echo fmt"Disk Size: {diskSize.float / (1024 * 1024 * 1024):.2f} GB"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment