N
N
nalimite2016-10-20 12:32:01
Delphi
nalimite, 2016-10-20 12:32:01

Delphi. How to find the disk number by its letter?

Hello. I am writing a program and I need to get the disk number to create a batch file in diskpart.
You need to get the system disk number.
What exactly you need to know is in the screenshot.
The question is how to get the disk number using standard Delphi methods?
hHKxZHD.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-10-20
@nalimite

type
  TDiskExtent = record
    DiskNumber: Cardinal;
    StartingOffset: Int64;
    ExtentLength: Int64;
  end;
  
  TVolumeDiskExtents = record
    NumberOfDiskExtents: Cardinal;
    Extents: array[0..0] of TDiskExtent;
  end;

  PVolumeDiskExtents = ^TVolumeDiskExtents;

...

const
  METHOD_BUFFERED                      = 0;
  FILE_ANY_ACCESS                      = 0;
  IOCTL_VOLUME_BASE                    = DWORD('V');
  IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS = ((IOCTL_VOLUME_BASE shl 16) or (FILE_ANY_ACCESS shl 14) or (0 shl 2) or METHOD_BUFFERED);

...

function GetLD(Drive: Char):Cardinal;
var
  Buffer : String;
begin
  Buffer := Format('\\.\%s:', [Drive]);
  Result := CreateFile(PChar(Buffer), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0);
end;

function GetPhysicalDiskNumber(Drive: Char):Byte;
var
  LD : DWORD;
  DiskExtents : PVolumeDiskExtents;
  DiskExtent : TDiskExtent;
  BytesReturned : Cardinal;
begin
  Result := MAXBYTE;
  LD := GetLD(Drive);
  if LD = INVALID_HANDLE_VALUE then exit;

  try
    DiskExtents := AllocMem(Max_Path);
    DeviceIOControl(LD, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, nil, 0, DiskExtents, Max_Path, BytesReturned, nil);
    if DiskExtents^.NumberOfDiskExtents > 0 then
    begin
      DiskExtent := DiskExtents^.Extents[0];
      Result := DiskExtent.DiskNumber;
    end;
  finally
    CloseHandle(LD);
  end;
end;

Source: https://www.experts-exchange.com/questions/2413489... (I tweaked the code a bit)

M
Mercury13, 2016-10-20
@Mercury13

stackoverflow.com/questions/5501749/how-do-you-get... Will you translate
into Pascal yourself? Attention, works only for fixed disks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question