public override void ReadDiskInformation() { // IOCTL_DISK_GET_DRIVE_GEOMETRY DiskGeometry = DeviceIoControl <Kernel32.DISK_GEOMETRY>(Kernel32.IOCTL_DISK_GET_DRIVE_GEOMETRY); BytesPerSector = DiskGeometry.BytesPerSector; // IOCTL_DISK_GET_DRIVE_LAYOUT var delta = Marshal.SizeOf(typeof(Kernel32.PARTITION_INFORMATION)); var bytes = DeviceIoControl(Kernel32.IOCTL_DISK_GET_DRIVE_LAYOUT, Marshal.SizeOf(typeof(Kernel32.DRIVE_LAYOUT_INFORMATION)), delta * 4); DriveLayoutInformation = MarshalEx.BytesToStruct <Kernel32.DRIVE_LAYOUT_INFORMATION>(bytes); PartitionInformation = new Kernel32.PARTITION_INFORMATION[DriveLayoutInformation.PartitionCount]; var offset = Marshal.SizeOf(typeof(Kernel32.DRIVE_LAYOUT_INFORMATION)); for (var i = 0; i < DriveLayoutInformation.PartitionCount; i++) { PartitionInformation[i] = MarshalEx.BytesToStruct <Kernel32.PARTITION_INFORMATION>(bytes, offset, delta); offset += delta; } }
// DeviceIoControl public TypeOut DeviceIoControl <TypeOut>(UInt32 ioControlCode) where TypeOut : struct { var outBuffer = MarshalEx.AllocateBytesForStruct <TypeOut>(); DeviceIoControl(ioControlCode, null, outBuffer); return(MarshalEx.BytesToStruct <TypeOut>(outBuffer)); }
public TypeOut DeviceIoControl <TypeIn, TypeOut>(UInt32 ioControlCode, TypeIn inStruct) where TypeIn : struct where TypeOut : struct { var inBuffer = MarshalEx.StructToBytes <TypeIn>(inStruct); var outBuffer = MarshalEx.AllocateBytesForStruct <TypeOut>(); DeviceIoControl(ioControlCode, inBuffer, outBuffer); return(MarshalEx.BytesToStruct <TypeOut>(outBuffer)); }
public override void ReadDiskInformation() { // PARTITION_INFORMATION PartitionInformation = DeviceIoControl <Kernel32.PARTITION_INFORMATION>(Kernel32.IOCTL_DISK_GET_PARTITION_INFO); Byte[] sector = Read(0, 512); VolumeBootRecord = MarshalEx.BytesToStruct <VolumeBootRecord>(sector); BytesPerSector = VolumeBootRecord.BiosParameterBlock.BytesPerSector; }