IOCTL_DISK_GET_DRIVE_GEOMETRY

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

你可能感兴趣的:(IOCTL_DISK_GET_DRIVE_GEOMETRY)