Results 1 to 5 of 5

Thread: GetLogicalDrives question

  1. #1

    Thread Starter
    Junior Member mharley's Avatar
    Join Date
    Jan 2002
    Location
    Nova Scotia, Canada
    Posts
    24

    GetLogicalDrives question

    I have been learning how to use the API's in my spare time, and I have a question regarding the notation I found at www.vbapi.com. For the GetLogicalDrives api, the example is given as

    If (driveflags And 1) = 1 Then Debug.Print "Drive A: exists."
    If (driveflags And 2) = 2 Then Debug.Print "Drive B: exists."
    If (driveflags And 4) = 4 Then Debug.Print "Drive C: exists."
    If (driveflags And 8) = 8 Then Debug.Print "Drive D: exists."

    My question is, where do the numbers (1,2,4,8 etc) come from? Are there specific numbers assigned to each potential drive letter? How do you find out what the others are? Thanks for the help.

    Mark

  2. #2

    Thread Starter
    Junior Member mharley's Avatar
    Join Date
    Jan 2002
    Location
    Nova Scotia, Canada
    Posts
    24

    Ammendment

    Just to note, I do know that the numbers increase in powers of 2, (so I assume that means it jumps from 8 to 16 to 32 etc), but what do the numbers mean? Are they just arbitrary assignments?

  3. #3
    Si_the_geek
    Guest
    It is what is known as a bit-mask. Computers store numbers as binary (1's and 0's), so the number 7 is stored as:

    binary: 0 1 1 1

    which is the same as (8*0) + (4*1) + (2*1) + (1*1) = 7

    each bit you add to the front of a binary number is double the previous (ie: in the above an extra digit would be 16). It is done this way to save on memory usage (using booleans/integers instead would use 32 times as much memory).


    In your example each drive is assigned a bit to say whether it exists (1 means it does), so yes you can just multiply by two to find out about the next drive.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Here is a better example (at least, I think so)
    VB Code:
    1. 'From 101 Tech Tips, Visual Basic Programmers Journal, March 2001
    2.  
    3. Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
    4.  
    5. Private Sub GetDrives(CB As ComboBox, ByVal IsUsed As Boolean)
    6.            
    7.             Dim DriveLtr As Long
    8.             Dim StartPos As Integer
    9.  
    10.             If IsUsed = True Then  'if True is returned, then we want
    11.                StartPos = 0        'all drive letters in use
    12.             Else
    13.                StartPos = 4        'if False is returned, then we want
    14.             End If                 'to find an unused network drive, so
    15.                                    'skip A:, B:, C:, and D:
    16.             CB.Clear
    17.             For DriveLtr =StartPos To 25
    18.                 If CBool(GetLogicalDrives And (2 ^ DriveLtr)) = IsUsed Then
    19.                    CB.AddItem Chr$(Asc("A") + DriveLtr) & ":"
    20.                    'To check to see if a drive is already mapped, add this code
    21.                     'If Chr$(Asc("A") + DriveLtr) = "W" Then
    22.                      ' MsgBox "here it is"
    23.                    'End If
    24.                 End If
    25.             Next
    26. 'To Use:   GetDrives Combo1, True returns all drives letters currently in use
    27. '              GetDrives Combo1, False returns all available drive letters
    28. End Sub

  5. #5
    Member
    Join Date
    Jan 2002
    Posts
    35
    heres another example

    VB Code:
    1. Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias _
    2.   "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer _
    3.   As String) As Long
    4.  
    5. Private Function GetDriveLetters() As String
    6.     Dim strDrives As String, i As Long
    7.     strDrives = Space(GetLogicalDriveStrings(0, strDrives))
    8.     GetLogicalDriveStrings Len(strDrives), strDrives
    9.     For i = 1 To Len(strDrives) - 4 Step 4
    10.        GetDriveLetters = GetDriveLetters & Mid(strDrives, i, 3)
    11.     Next i
    12. End Function

    this will return a string ex "A:/C:/D:/"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width