BuildOSBufferBy

function TIoControlFile.BuildOSBufferBy<InputType, OutputType>(
  var InputBuffer: InputType; var OutputBuffer: OutputType): TIoControlIOBuffer;
begin
  result.InputBuffer.Size := SizeOf(InputBuffer);
  result.InputBuffer.Buffer := @InputBuffer;
  result.OutputBuffer.Size := SizeOf(OutputBuffer);
  result.OutputBuffer.Buffer := @OutputBuffer;
end;

function TIoControlFile.TDeviceIoControlCodeToOSControlCode(
  ControlCode: TIoControlCode): Integer;
const
  IOCTL_SCSI_BASE = FILE_DEVICE_CONTROLLER;
  IOCTL_STORAGE_BASE = $2D;
  IOCTL_ATA_PASS_THROUGH =
    (IOCTL_SCSI_BASE shl 16) or
    ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14) or ($040B shl 2) or
    (METHOD_BUFFERED);
  IOCTL_ATA_PASS_THROUGH_DIRECT = $4D030;
  IOCTL_SCSI_PASS_THROUGH =
    (IOCTL_SCSI_BASE shl 16) or
    ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14) or ($0401 shl 2) or
    (METHOD_BUFFERED);
  IOCTL_STORAGE_PROTOCOL_COMMAND =
    (IOCTL_SCSI_BASE shl 16) or
    ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14) or ($04F0 shl 2) or
    (METHOD_BUFFERED);
  IOCTL_SCSI_MINIPORT =
    (IOCTL_SCSI_BASE shl 16) or
    ((FILE_READ_ACCESS or FILE_WRITE_ACCESS) shl 14) or ($0402 shl 2) or
    (METHOD_BUFFERED);
  IOCTL_STORAGE_MANAGE_DATA_SET_ATTRIBUTES =
    (IOCTL_STORAGE_BASE shl 16) or
    (FILE_WRITE_ACCESS shl 14) or ($0501 shl 2) or
    (METHOD_BUFFERED);
  IOCTL_SCSI_GET_ADDRESS =
    (IOCTL_SCSI_BASE shl 16) or
    (FILE_ANY_ACCESS shl 14) or ($0406 shl 2) or
    (METHOD_BUFFERED);

  OSControlCodeOfIoControlCode: Array[TIoControlCode] of Integer =
    (IOCTL_ATA_PASS_THROUGH,
     IOCTL_ATA_PASS_THROUGH_DIRECT,
     IOCTL_SCSI_PASS_THROUGH,
     IOCTL_STORAGE_PROTOCOL_COMMAND,
     IOCTL_STORAGE_QUERY_PROPERTY,
     IOCTL_STORAGE_CHECK_VERIFY,
     FSCTL_GET_VOLUME_BITMAP,
     IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
     FSCTL_GET_NTFS_VOLUME_DATA,
     IOCTL_DISK_GET_DRIVE_GEOMETRY_EX,
     IOCTL_STORAGE_MANAGE_DATA_SET_ATTRIBUTES,
     IOCTL_SCSI_MINIPORT,
     IOCTL_SCSI_GET_ADDRESS,
     FSCTL_LOCK_VOLUME,
     FSCTL_UNLOCK_VOLUME,
     IOCTL_STORAGE_LOAD_MEDIA,
     0);
begin
  if (ControlCode = TIoControlCode.Unknown) or (IsOutOfRange(ControlCode)) then
    raise EInvalidIoControlCode.Create
      ('InvalidIoControlCode: There''s no such IoControlCode - ' +
       IntToStr(Cardinal(ControlCode)));
  exit(OSControlCodeOfIoControlCode[ControlCode]);
end;