Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GetLogicalDriveStrings 1

Status
Not open for further replies.

TheTuna

Programmer
Dec 13, 2002
544
US
GetLogicalDriveStrings

Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength as Long, ByVal lpBuffer as String)

Retreives a string containing the root drive paths for all current logical drives.

nBufferLength = Long, The length of the lpBuffer String
lpBuffer = String, A string to load with the logical drive names. Each name is separated by a NULL Character, with two NULLS after the last name.

My question is regarding the use of this API Call. So far, all I can manage to get this to do is crash the IDE.

This function should return Zero on error, or a long that contains the required buffer length (if the present buffer length is not long enough).

I did not try to pre-init the string... maybe that's it. I'll try it, but has anyone used this call before?


[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 

Tuna, give this a try...
[tt]
Option Explicit

Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long


Private Sub Form_Load()

Dim L As Long, S As String, ReturnValue As Long

ReturnValue = GetLogicalDriveStrings(L, S)

S = String(ReturnValue, Chr(0))
L = ReturnValue

ReturnValue = GetLogicalDriveStrings(L, S)

End Sub
[/tt]

Walk through it!

Good Luck

 
Dim sBuffer1 As String
Dim lVal As Long
Dim retVal As Long

lVal = 250
sBuffer1 = String(lVal, Chr(255))
retVal = GetLogicalDriveStrings(lVal, sBuffer1)
Text1.Text = sBuffer1

Yep, I was write, I needed to init the string first... VBer, you're quick, when I returned to post that it's working, i didn't expect to see a reply already!

Thanks though!

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 

Just for your info though, let me point out the return values from help...
[tt]
Return Values
If the function succeeds, the return value is the length, in characters, of the strings copied to the buffer, not including the terminating null character. Note that an ANSI-ASCII null character uses one byte, but a Unicode null character uses two bytes.

If the buffer is not large enough, the return value is greater than nBufferLength. It is the size of the buffer required to hold the drive strings.

If the function fails, the return value is zero. To get extended error information, use the GetLastError function.
[/tt]

Good Luck

 
VB5er... shouldn't a USB Flash Drive (256 Meg Jump Drive) show up as a logical drive?

This code only seems to return A:\...

Would the NULL character that separates the drives be causing this? I'm directing the output to a text box, as you probably noticed from my code.

I suspect I'll need to parse the string for the Nulls.

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 

Actually I think I would do something like...
[tt]
Dim MyArray() As String
MyArray = Split(S, Chr(0))
...
[/tt]

If you walk through my example or your code you will find that the string returned is delimited by Chr(0) so a simple split and then a loop to populate (or join with another character instead of chr(0)) should do the trick for you.

Good Luck

 
Thanks, I was trying to split on vbNull... which obviously didn't work! You were right to suggest splitting on the chr(0).

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 
Note, when using split, myarray must be variant...

Thanks my programmin brotha!

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 
TheTuna: i realised you sussed it but i figure ill throw in this code, cant remember where it came from (book,site etc) but its been pretty handy for me in the past

Option Explicit

Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" ( _
ByVal nBufferLength As Long, _
ByVal lpBuffer As String _
) As Long

Private Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" ( _
ByVal nDrive As String _
) As Long

Private Const DRIVE_CDROM = 5
Private Const DRIVE_FIXED = 3
Private Const DRIVE_RAMDISK = 6
Private Const DRIVE_REMOTE = 4
Private Const DRIVE_REMOVABLE = 2


Private Sub GetAllDrives()

Dim myLength As Integer
Dim myBuffer As String
Dim i As Integer
Dim myDrive As String
Dim myType As Long

myLength = GetLogicalDriveStrings(0, 0)
myBuffer = String$(myLength, 0)
myLength = GetLogicalDriveStrings(myLength, myBuffer)
For i = 1 To Len(myBuffer) - 1 Step 4
myDrive = Mid$(myBuffer, i, 3)
myType = GetDriveType(myDrive)
Select Case myType
Case DRIVE_CDROM
MsgBox myDrive & " is a CD-Rom Drive", _
vbOKOnly + vbInformation, _
"Your Drives"
Case DRIVE_FIXED
MsgBox myDrive & " is a Fixed Drive", _
vbOKOnly + vbInformation, _
"Your Drives"
Case DRIVE_RAMDISK
MsgBox myDrive & " is a Ram Disk", _
vbOKOnly + vbInformation, _
"Your Drives"
Case DRIVE_REMOTE
MsgBox myDrive & " is a Remote Drive", _
vbOKOnly + vbInformation, _
"Your Drives"
Case DRIVE_REMOVABLE
MsgBox myDrive & " is a Removable Drive", _
vbOKOnly + vbInformation, _
"Your Drives"
Case Else
MsgBox "The following drive " & CStr(Asc(myDrive)) & " is not recognised.", _
vbOKOnly + vbCritical, _
"Your Drives"
End Select
Next

End Sub


If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Thanks for the code sample, but in my case, all I'm trying to do is detect when a USB Flash Drive is plugged in: So, within the timer event of a timer control, I have the following code:

Dim sBuffer1 As String
Dim retVal As Long
Dim Splitter As Variant

sBuffer1 = String(32, Chr(255))
retVal = GetLogicalDriveStrings(32, sBuffer1)
Splitter = Split(sBuffer1, Chr(0))
Text1.Text = Splitter(UBound(Splitter) - 2)

Now, what happens when the USB Drive is plugged in, when the next timer interval cycles, the last drive goes from c:\ to d:\ (d:\ to e:\ on some kiosks)...

If the last drive becomes either of these I know that the drive has been plugged in, and I fire another event to copy update files from the flash drive to the hard drive... then that script processor executes and the kiosk updates...

I had to do this because the users who may be updating the kiosks are not always going to be PC literate. This way they just plug it in and the software gets updated, and they didn't have to copy files, remember paths, read directions, etc.

This way, if something goes wrong, it's my fault, since I should have tested the script and the software prior to releasing it... hey wait, what was I thinking! ;-)

[fish] No Dolphins were harmed in the posting of this message... Dolphin Friendly Tuna!
 
fair enough! [lol]

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top