|
-
Jan 31st, 2002, 09:17 AM
#1
Thread Starter
Junior Member
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
-
Jan 31st, 2002, 09:20 AM
#2
Thread Starter
Junior Member
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?
-
Jan 31st, 2002, 10:04 AM
#3
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.
-
Jan 31st, 2002, 10:17 AM
#4
Here is a better example (at least, I think so)
VB Code:
'From 101 Tech Tips, Visual Basic Programmers Journal, March 2001
Private Declare Function GetLogicalDrives Lib "kernel32" () As Long
Private Sub GetDrives(CB As ComboBox, ByVal IsUsed As Boolean)
Dim DriveLtr As Long
Dim StartPos As Integer
If IsUsed = True Then 'if True is returned, then we want
StartPos = 0 'all drive letters in use
Else
StartPos = 4 'if False is returned, then we want
End If 'to find an unused network drive, so
'skip A:, B:, C:, and D:
CB.Clear
For DriveLtr =StartPos To 25
If CBool(GetLogicalDrives And (2 ^ DriveLtr)) = IsUsed Then
CB.AddItem Chr$(Asc("A") + DriveLtr) & ":"
'To check to see if a drive is already mapped, add this code
'If Chr$(Asc("A") + DriveLtr) = "W" Then
' MsgBox "here it is"
'End If
End If
Next
'To Use: GetDrives Combo1, True returns all drives letters currently in use
' GetDrives Combo1, False returns all available drive letters
End Sub
-
Feb 1st, 2002, 03:19 AM
#5
Member
heres another example
VB Code:
Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias _
"GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer _
As String) As Long
Private Function GetDriveLetters() As String
Dim strDrives As String, i As Long
strDrives = Space(GetLogicalDriveStrings(0, strDrives))
GetLogicalDriveStrings Len(strDrives), strDrives
For i = 1 To Len(strDrives) - 4 Step 4
GetDriveLetters = GetDriveLetters & Mid(strDrives, i, 3)
Next i
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|