GetDrives( )

Purpose
Retrieves a list of installed drives.
 
Author Date of code (original) Updated on Date of page (original) Updated on
Bill Thoen
bthoen@ctmap.com
11Jan2001
11Jan2001

29Jan2001
 
Restrictions on use
This module is distributed under the terms of the Lesser GNU General Public License. Restrictions on the use of this work in a commercial application or derivative work is described in the Lesser GNU General Public License page at: http://www.fsf.org/copyleft/lesser.html
 
Description
Returns a semi-colon delimited string of drive specifications. Calls Windows API GetLogicalDriveString32() to return the system drive strings (e.g. a:\, b:\, etc.). However the API function returns this as a single string with null byte delimiters, and MapInfo can't handle strings with null bytes in them. Instead we pass an array of integers and parse the bytes into a MapInfo semi-colon delimited string. The WinAPI declaration must be modifed as well, but this works because strings and integer arrays are mapped into the same memory space on the stack. Note that strings are one byte per character, while integers contain 4 bytes each.
 
External resources
Uses Windows API functions in kernel32.
 
Declare statement of sub_function. Include in your program. Copy/Paste if needed.
declare function GetDrives( )
   as string
 
Returned value(s) (function only)
  Semi-colon delimited string of drive specifications
 
Other required declare statement(s). Include in your program. Copy/Paste if needed.
declare function GetLogicalDriveString32 lib "kernel32"
   alias "GetLogicalDriveStringsA" (
   byval nBufferLength as integer,
   sBuffer() as integer
   ) as integer
 
MapBasic Code. Copy/Paste if needed.
function GetDrives () as string

dim nCount, nStatus as integer
dim sBuffer as string
dim sChar() as integer
dim i, n as integer

   nCount = 128
   redim sChar(nCount)

   nCount = GetLogicalDriveString32 (nCount*4, sChar)
   sBuffer = ""
   for i = 1 to nCount\4
      'Convert 4 bytes of integer into 4 bytes of string.
      'Note that byte order for integers is reversed
      'from byte-order of strings.
      n = (sChar(i) \ 256^0) mod 256
      if n = 0 then n = asc(";") end if
      sBuffer = sBuffer + chr$(n)
      n = (sChar(i) \ 256^1) mod 256
      if n = 0 then n = asc(";") end if
      sBuffer = sBuffer + chr$(n)
      n = (sChar(i) \ 256^2) mod 256
      if n = 0 then n = asc(";") end if
      sBuffer = sBuffer + chr$(n)
      n = (sChar(i) \ 256^3) mod 256
      if n = 0 then n = asc(";") end if
      sBuffer = sBuffer + chr$(n)
   next
   GetDrives = UCase$(sBuffer)
end function
 
Availability for download
Dir_File.zip
 
Example
dim sDrives as string

   sDrives = GetDrives()
   sDrives might now contain "A:\;C:\;D:\"
 
Comments

 
See also
GetFilenames
GetDir