QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: hanness on January 26, 2021, 05:13:05 am

Title: Using _DIREXISTS with BitLocker encrypted drives
Post by: hanness on January 26, 2021, 05:13:05 am
When checking for the existence of a drive letter, I have been using the _DIREXISTS function.

For example:

IF _DIREXISTS(D:) then
   PRINT "Drive letter D: exists on this system"
END IF

However, if a drive is BitLocker protected AND that drive is currently locked, then the _DIREXISTS for that drive letter will return false leading to the erroneous conclusion that the drive letter is available.

To work around this, if _DIREXISTS returns false, I always follow up by running a "manage-bde -status D:" (or whatever drive letter) via a SHELL command and capturing / parsing the output.

So, this leads me to my question: Is there any more foolproof way to determine a drive letter's existence? There is the _DIREXISTS function, as well as the _FILEEXISTS function, but no _DRIVEEXISTS function or anything similar as far as I can tell. My workaround works, just wondering if there is a more direct test of the existence of a drive letter.


Title: Re: Using _DIREXISTS with BitLocker encrypted drives
Post by: RhoSigma on January 26, 2021, 06:59:03 am
Using the WinAPI (I assume you are on Windows OS),

Code: QB64: [Select]
  1.     FUNCTION GetLogicalDriveStringsA& (BYVAL bufSize&, buffer$)
  2.  
  3. '--- get available drives once during your program init procedure ---
  4. DIM SHARED allDrives$
  5. buffer$ = SPACE$(112)
  6. length% = GetLogicalDriveStringsA&(LEN(buffer$), buffer$)
  7. allDrives$ = ""
  8. FOR position% = 1 TO length% STEP 4
  9.     allDrives$ = allDrives$ + MID$(buffer$, position%, 1)
  10. NEXT position%
  11.  
  12. '--- in your program flow test drives whenever needed (returns false(0) or true(-1)) ---
  13. PRINT DRIVEEXISTS%("A")
  14. PRINT DRIVEEXISTS%("B")
  15. PRINT DRIVEEXISTS%("C")
  16. PRINT DRIVEEXISTS%("D")
  17.  
  18. '--- a quick test function ---
  19. FUNCTION DRIVEEXISTS% (drv$)
  20. DRIVEEXISTS% = (INSTR(allDrives$, UCASE$(drv$)) > 0)
  21.  
Title: Re: Using _DIREXISTS with BitLocker encrypted drives
Post by: hanness on January 26, 2021, 02:00:57 pm
Thanks, much appreciated.

I have to admit that I've never done anything using the WinAPI so I guess I better start educating myself on this :-)
Title: Re: Using _DIREXISTS with BitLocker encrypted drives
Post by: Pete on January 26, 2021, 02:16:26 pm
Thanks, much appreciated.

I have to admit that I've never done anything using the WinAPI so I guess I better start educating myself on this :-)

You won't be disappointed, but if you make your own routines, be advised there is lag when you poll the Windows libraries, so if you don't get the results you expected, put a _delay .3 ( or some other suitable value) in your code, to let the API return a value before polling for that value in your code.

Pete