March 05, 2021, 06:59:15 AM

Author Topic: [bmx] Get list of valid drives (Windows-only) by BlitzSupport [ 1+ years ago ]  (Read 686 times)

Offline BlitzBot

Title : Get list of valid drives (Windows-only)
Author : BlitzSupport
Posted : 1+ years ago

Description : Retrieve list of drives on a Windows system.

Code :
Code: BlitzMax
  1. SuperStrict
  2.  
  3. Extern "win32"
  4.         Function GetLogicalDriveStringsA (buffer_size:Int, buffer:Byte Ptr)
  5.         Function GetDriveTypeA (name:Byte Ptr)
  6. End Extern
  7.  
  8. Function GetDriveList:TList ()
  9.  
  10.         Local drivelist:TList = CreateList ()
  11.        
  12.         ' Stupid, stupid Windows...
  13.        
  14.         Local mem:TBank = CreateBank (255)
  15.         Local result:Int = GetLogicalDriveStringsA (255, BankBuf (mem))
  16.  
  17.         If result > 255
  18.                 ResizeBank (mem, result)
  19.                 result = GetLogicalDriveStringsA (result, BankBuf (mem))
  20.         EndIf
  21.        
  22.         If result
  23.        
  24.                 Local drive:String = ""
  25.                
  26.                 For Local a:Int = 0 To result - 1
  27.        
  28.                         Local char:Byte = PeekByte (mem, a)
  29.        
  30.                         If char
  31.                                 drive = drive + Chr (char)
  32.                         Else
  33.                                 ListAddLast drivelist, drive
  34.                                 drive$ = ""
  35.                         EndIf
  36.                        
  37.                 Next
  38.        
  39.                 mem = Null
  40.                 Return drivelist
  41.        
  42.         Else
  43.        
  44.                 mem = Null
  45.                 Return Null
  46.        
  47.         EndIf
  48.  
  49. End Function
  50.  
  51. Function GetDriveType:String (drive:String)
  52.  
  53.         ' You can pass "A:" or "A:"...
  54.        
  55.         If Right (drive, 1) <> "" Then drive = drive + ""
  56.        
  57.         Const DRIVE_UNKNOWN:Int = 0
  58.         Const DRIVE_NO_ROOT_DIR:Int = 1
  59.         Const DRIVE_REMOVABLE:Int = 2
  60.         Const DRIVE_FIXED:Int = 3
  61.         Const DRIVE_REMOTE:Int = 4
  62.         Const DRIVE_CDROM:Int = 5
  63.         Const DRIVE_RAMDISK:Int = 6
  64.  
  65.         Select GetDriveTypeA (drive.ToCString ())
  66.                 Case DRIVE_UNKNOWN
  67.                         Return "Unknown drive type"
  68.                 Case DRIVE_NO_ROOT_DIR
  69.                         Return "Drive doesn't exist"
  70.                 Case DRIVE_REMOVABLE
  71.                         Return "Removable drive"
  72.                 Case DRIVE_FIXED
  73.                         Return "Fixed drive"
  74.                 Case DRIVE_REMOTE
  75.                         Return "Network drive"
  76.                 Case DRIVE_CDROM
  77.                         Return "CD/DVD drive"
  78.                 Case DRIVE_RAMDISK
  79.                         Return "RAM-based drive"
  80.         End Select
  81.  
  82. End Function
  83.  
  84. Local drives:TList = GetDriveList ()
  85.  
  86. If drives
  87.  
  88.         For Local letter:String = EachIn drives
  89.                 ' Print letter                                  ' Drive name only...
  90.                 Print letter + "~t~t" + GetDriveType (letter)   ' Drive and type...
  91.         Next
  92.        
  93.         ' Print "$:~t~t" + GetDriveType ("$:") ' Non-existent drive test!
  94.        
  95. EndIf


Comments :


xlsior(Posted 1+ years ago)

 FWIW: This also includes network drives and SUBST-ed drive mappings, in case anyone is wondering.


Floyd(Posted 1+ years ago)

 That's interesting. The output also shows drive Q, which I had completely forgotten about. It's a non-existent virtual drive, apparently a relic of a trial version of MS Office.


BlitzSupport(Posted 1+ years ago)

 Just added drive types and did a minor bug-fix -- was missing result = on the second GetLogicalDriveStringsA call.


xlsior(Posted 1+ years ago)

 Nice, thanks


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal