IOCTL_DISK_GET_DRIVE_GEOMETRY
阅读原文时间:2023年07月13日阅读:4

IOCTL_DISK_GET_DRIVE_GEOMETRY: 获取磁盘参数

c++实现:

#include
#include
#include

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")  

}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章