Created
November 9, 2021 18:25
-
-
Save khchen/b8d4ac4fc26c43bd791e6487c6af244d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #[ | |
| 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