IOCTL_DISK_GET_DRIVE_GEOMETRY: 獲取磁盤參數
c++實現:
#include <Windows.h>
#include <winioctl.h>
#include <stdio.h>
int main()
{
HANDLE hDev = CreateFile("\\\\.\\G:",
GENERIC_READ,
FILE_SHARE_READ,
0,
OPEN_EXISTING,
0,
0);
if (hDev != INVALID_HANDLE_VALUE)
{
DISK_GEOMETRY disk_geometry;
DeviceIoControl(hDev, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &disk_geometry, sizeof(disk_geometry), NULL, NULL);
printf(" 柱面數量: %d\n", disk_geometry.Cylinders); //柱面數量
printf(" 介質類型: %d\n", disk_geometry.MediaType); //介質類型
printf("每柱面磁道數: %d\n", disk_geometry.TracksPerCylinder); //每柱面磁道數
printf("每磁道扇區數: %d\n", disk_geometry.SectorsPerTrack); //每磁道扇區數
printf("每扇區字節數: %d\n", disk_geometry.BytesPerSector); //每扇區字節數
CloseHandle(hDev);
}
}
golang實現:
type DISK_GEOMETRY struct {
Cylinders int64
MediaType int32
TracksPerCylinder int32
SectorsPerTrack int32
BytesPerSector int32
}
func getHdev(letter byte) int {
deviceName := "\\\\.\\" + string(letter) + ":"
kernel32 := syscall.NewLazyDLL("kernel32.dll")
CreateFile := kernel32.NewProc("CreateFileA")
hDev, _, _ := CreateFile.Call(BytePtr([]byte(deviceName)), uintptr(0x80000000), uintptr(1), 0, uintptr(3), 0, 0)
return int(hDev)
}
func getDiskGeometry(hDev int) {
const IOCTL_DISK_GET_DRIVE_GEOMETRY = 0x70000
var read = 0
disk_geometry := DISK_GEOMETRY{}
kernel32 := syscall.NewLazyDLL("kernel32.dll")
DeviceIoControl := kernel32.NewProc("DeviceIoControl")
DeviceIoControl.Call(uintptr(hDev), uintptr(IOCTL_DISK_GET_DRIVE_GEOMETRY), 0, 0, uintptr(unsafe.Pointer(&disk_geometry)), 24, uintptr(unsafe.Pointer(&read)), 0)
fmt.Println(" 柱面數量:", disk_geometry.Cylinders) //柱面數量
fmt.Println(" 介質類型:", disk_geometry.MediaType) //介質類型
fmt.Println("每柱面磁道數:", disk_geometry.TracksPerCylinder) //每柱面磁道數
fmt.Println("每磁道扇區數:", disk_geometry.SectorsPerTrack) //每磁道扇區數
fmt.Println("每扇區字節數:", disk_geometry.BytesPerSector) //每扇區字節數
fmt.Println(" 總容量:", (disk_geometry.Cylinders*int64(disk_geometry.TracksPerCylinder)*int64(disk_geometry.SectorsPerTrack)*int64(disk_geometry.BytesPerSector))/1024/1024, "MB")
}
