Detecting Connected Disc Drives ( or CD Drives )

Blitz3D Forums/Blitz3D Programming/Detecting Connected Disc Drives ( or CD Drives )

Cold Storage(Posted 2005) [#1]
Is there a way of detecting which drives are mounted?

I am currently doing a ReadDir of "A:\" ... "Z:\" and if I get a zero on a drive then there's no drive.

BUT!

If there is a CD drive then Windows in it's infinite wisdom ( on some computers ) says "No Disc in Drive! - Cancel / Continue / Blah ". On other computers it doesn't report the drive as being there - nothing like consistency.

Is there a non invasive, Blitz Basic 3D way of just getting a list of mounted drives?

I'm sure this could be cured with .dll but that would firstly mean I'd have to upgrade the version of BB3D I'm using (not just now thanks - I'm just at the end of a project), and I'd have to get someone to write the C++ .dll! ;O)


John Blackledge(Posted 2005) [#2]
Can't remember where I got this from (probably in the toolbox) but it's faster to include the code than go looking for it.
It can be edited down for your own needs of course.
I needed the serial number of the C: drive, and I think I got it doiwn to 4 lines.
; Volume Information

; !!   place these in 'Kernel32.decls'   !!
; *****************************************
; GetDriveType%(drivename$):"GetDriveTypeA"
; GetVolumeInformation%(Path$,VolNameBuff*,VolLen%,Serial*,MaxComponentLen*,fsFlags*,fsNameBuff*,fsNameLen%):"GetVolumeInformationA"
; GetLogicalDriveStrings%(bufflen%,buffer*):"GetLogicalDriveStringsA"
; *****************************************

; volume details are stored here
Type volumeinfo
	Field driveletter$     ; Drives letter                "A:\"  "C:\"   "F:\"
	Field drivename$       ; Name of device               "My Computer"
	Field drivetype$       ; What type                    "Floppy" "CD-Rom"
	Field serial%          ; Serial number                1234567890
	Field maxcomponentlen% ; long/short name support      8.3  255
	Field flags%           ; associated flags             012345
	Field filesystem$      ; file system used             FAT32 NTFS CDFS
End Type
Global vol.volumeinfo
;----------------------------------------------------------

Graphics 800,600

Print SystemProperty("appdir")
Print SystemProperty("windowsdir")
Print SystemProperty("systemdir")
Print
GetVolumeInfo ; fill 'volumeinfo' type with available volumes information

Print "Vol     Name                Type          Serial       Flags    FileSystem"
Print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
For vol=Each volumeinfo
	r$=vol\driveletter$+"     "+Left$(vol\drivename$+String$(" ",19),19)
	r$=r$+" "+Left$(vol\drivetype$+String$(" ",12),12)
	r$=r$+" "+Right$(String$(" ",10)+Str$(vol\serial),11)+"   "
	r$=r$+Right$("      "+Str$(vol\flags),6)+"   "+vol\filesystem
	Print r$
Next
Print

Print SystemProperty("appdir")
Print SystemProperty("windowsdir")
Print SystemProperty("systemdir")
Print

a$=Input$("Done ... RETURN to end")
End
;----------------------------------------------------------



;-----------------------
Function GetVolumeInfo()
;-----------------------
	; first, get a list of available volumes  .. A:\B:\C:\F:\ ...
	vlist=CreateBank(256)
	GetLogicalDriveStrings 255,vlist
	drivelist$=PeekString$(vlist,256)
	FreeBank vlist
	; run through list of voulumes
	For x=0 To Len(drivelist$)/3-1
		vol=New volumeinfo
		vol\driveletter$=Mid$(drivelist$,x*3+1,3)
		vol\drivename$="(not available)"
		Select GetDriveType(vol\driveletter$)
			Case 2 : vol\drivetype$= "Removable"
			Case 3 : vol\drivetype$= "Drive Fixed"
			Case 4 : vol\drivetype$= "Remote"
			Case 5 : vol\drivetype$= "Cd-Rom"
			Case 6 : vol\drivetype$= "Ram disk"
			Default : vol\drivetype$= "Unrecognized"
		End Select
		vn=CreateBank(256) : sn=CreateBank(4)
		mcl=CreateBank(4) : flags=CreateBank(4) : fs=CreateBank(256)
		GetVolumeInformation vol\driveletter$,vn,255,sn,mcl,flags,fs,255
		vol\drivename$=PeekString$(vn,256)
		If vol\drivename$="" Then vol\drivename$="(not available)"
		vol\serial=PeekInt(sn,0)
		vol\maxcomponentlen=PeekInt(mcl,0)
		vol\flags=PeekInt(flags,0)
		vol\filesystem=PeekString$(fs,256)
		; free the banks
		FreeBank sn : FreeBank mcl : FreeBank flags
		FreeBank vn : FreeBank fs
	Next
End Function

; build and return a string of characters from a bank
;-----------------------
Function PeekString$(bank,numbytes)
;-----------------------
	Local a$=""
	For pos=0 To numbytes-1
		byte=PeekByte(bank,pos)
		If byte<>0 Then a$=a$+Chr$(byte)
	Next
	Return a$
End Function

;--------------------------------------------------------------------------------------------
;--------------------------------------------------------------------------------------------



Cold Storage(Posted 2005) [#3]
Thanks for that John... :O)

Where exactly should Kernel32.decls go and ...er... what exactly is Kernel32.decls?

I'm running Blitz 3D v1.64 - will I have to upgrade before I can do this cleverness?

Thanks again!
Tim.


John Blackledge(Posted 2005) [#4]
Yes, I think you will have to upgrade, though I can't remember which version brought in userlibs.

Try userlibs forum :
http://www.blitzbasic.co.nz/Community/topics.php?forum=94

Otherwise tell me and I'll email it to you.