show available drives [SOLVED] lazarus / 67-Windows ---------------------------------------------------------------------- bjlockie1 · 2020-11-20 21:20 I'm trying to list available drives. These functions show 3 letters, C, D, Z. It can't ChDir to drive D:\ but C:\ and Z:\ work fine. I don't think drive D: should be listed. I am not a Windows programmer so I cobbled this code together. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetBit(Value: QWord; Index: Byte): Boolean;begin  Result := ((Value shr Index) and 1) = 1;end; function GetDriveLetters(): TStringList;{$ifdef Windows}var dword     : QWord;    letter    : Char;    i         : Byte = 0;    shortname : String;{$endif}begin    Result:=TStringList.Create();    {$ifdef Windows}        dword:=GetLogicalDrives();         for letter:='A' to 'Z' do        begin            if GetBit(dword,i)=True then            begin              shortname:='(DRIVE) '+letter+':\';                Result.Add( shortname );            end;            i:=i+1;        end;    {$endif}end; ---------------------------------------------------------------------- Remy Lebeau · 2020-11-20 21:24 Quote from: bjlockie1 on November 20, 2020, 10:20:24 pmIt can't ChDir to drive D:\ but C:\ and Z:\ work fine. What kind of drive is D:\ exactly?  What does GetDriveType() report for it?  Is it a partition on an HDD/SSD?  Is it a CD/DVD disc drive?  Is it a removable USB drive?  Is it a network drive share? ---------------------------------------------------------------------- 440bx · 2020-11-21 01:12 Quote from: bjlockie1 on November 20, 2020, 10:20:24 pmI'm trying to list available drives. These functions show 3 letters, C, D, Z. It can't ChDir to drive D:\ but C:\ and Z:\ work fine. I don't think drive D: should be listed. The fact that Windows allocated a letter to a drive does not always imply the drive is accessible.  That's why @Remy Lebeau above was asking for the drive type. You can find an example of showing the available drives and their type at https://forum.lazarus.freepascal.org/index.php/topic,44377.msg31191 0.html#msg311910 But, again, the fact that a letter/drive is listed does _not_ mean it is accessible. HTH. ---------------------------------------------------------------------- winni · 2020-11-21 01:32 Quote from: bjlockie1 on November 20, 2020, 10:20:24 pmI'm trying to list available drives. Why do you start a new topic with the same issue while the other topic is still active?? ---------------------------------------------------------------------- bjlockie1 · 2020-11-21 04:32 Quote from: winni on November 21, 2020, 02:32:58 amWhy do you start a new topic with the same issue while the other topic is still active?? The other topic was in the Linux forum. I said I was coming here to hopefully get more Windows specific help. Was that wrong? ---------------------------------------------------------------------- bjlockie1 · 2020-11-21 04:34 Quote from: Remy Lebeau on November 20, 2020, 10:24:27 pmQuote from: bjlockie1 on November 20, 2020, 10:20:24 pmIt can't ChDir to drive D:\ but C:\ and Z:\ work fine. What kind of drive is D:\ exactly?  What does GetDriveType() report for it?  Is it a partition on an HDD/SSD?  Is it a CD/DVD drive that doesn't have a disc in it?  Is it a removable USB drive? C -> 3 D -> 5 Z -> 4 D is probably an empty cdrom. :-) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function GetDriveLetters(): TStringList;{$ifdef Windows}var dword     : QWord;    letter    : Char;    i         : Byte = 0;    shortname : String;  dr        : Packed array[0..3] of char;    dt        : UINT;{$endif}begin    Result:=TStringList.Create();    {$ifdef Windows}        ZeroMemory(@dr, sizeof(dr));        lstrcpy(dr, 'A:\');             dword:=GetLogicalDrives();         for letter:='A' to 'Z' do        begin            if GetBit(dword,i)=True then          begin                dr[0]:=char(ord('A')+i);                dt:=GetDriveType(dr);                shortname:='(DRIVE) '+letter+':\' + IntToStr(dt);                Result.Add( shortname );            end;            inc(i);        end;    {$endif}end; ---------------------------------------------------------------------- Bart · 2020-11-21 15:47 I showed you simple code how to do that in https://forum.lazarus.freep ascal.org/index.php/topic,52203.msg384399.html#msg384399 Use the function GetAvailableDrives(DiskMustBeInDrive: Boolean = False): TSysCharSet; function. Set the DiskMustBeInDrive parameter to True. If there is no disc in the CDRom, it wont be included in the resulting set. Bart ---------------------------------------------------------------------- Bart · 2020-11-21 16:44 I'll make it even simpler for you. Download http://svn.code.sf.net/p/flyingsheep/code/trunk/MijnLib/fsiwinutils.pp Now you can do things like this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button1Click(Sender: TObject);var  Drives: TSysCharSet;  D: Char;begin  writeln('Reported drives by Windows:');  Drives := GetAvailableDrives(False);  for D in Drives do writeln(D + ':\',' [',DiskInDrive(D),']');  writeln;  writeln('Actual mounted drives:');  Drives := GetAvailableDrives(True);  for D in Drives do writeln(D + ':\');  writeln;  writeln('Removable drives:');  Drives := GetAvailableDrives(False, [dtRemovable]);  for D in Drives do writeln(D + ':\');  writeln;  writeln('CD-Roms''s:');  Drives := GetAvailableDrives(False, [dtCDROM]);  for D in Drives do writeln(D + ':\');  writeln;  writeln('Actual mounted CD-Roms''s:');  Drives := GetAvailableDrives(True, [dtCDROM]);  for D in Drives do writeln(D + ':\');  writeln;end;[code] For me it outputs:[code]Reported drives by Windows:C:\ [TRUE]D:\ [TRUE]E:\ [FALSE]F:\ [TRUE] Actual mounted drives:C:\D:\F:\ Removable drives:F:\ CD-Roms's:E:\ Actual mounted CD- Roms's:  Bart ---------------------------------------------------------------------- bjlockie1 · 2020-11-22 01:05 I thought I could wrap ChDir in a try but I still get exception class 'RunError(152)'. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ChangeDir( dirname : String );begin    try        ChDir( dirname );  except        ShowMessage('Can''t change to '''+dirname+'''');    end;end; ---------------------------------------------------------------------- bjlockie1 · 2020-11-22 03:29 I turned off the debugging and ran it from outside the IDE and the try catches the exception.