Click to See Complete Forum and Search --> : Detecting CD ROM Drive


BrewGuru99
December 28th, 1999, 04:40 PM
I would like to beable to detect wether or not the cd for my program is in the cd rom drive. You know, inorder to require that my cd be in the drive to use the program.

Is this possible in VB?

Brewguru99

AndyK
December 28th, 1999, 05:44 PM
Yes it's posible, you can make a hidden file say named Check.rak (any file!!!) then you put it on your cd, and your program will check for existance of that file.
This code checks for file:

private Function FileExist(FileName as string)
FileExist = false
on error resume next
FileExist = (FileLen(FileName) = FileLen(FileName))
End Function



This code detects cd drive letter and uses FileExist function to check if file exists

This is declarations

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
Const DRIVE_CDROM = 5



this is code itself, you can place it into Command1_Click() or Form_Load(), etc.

Dim r as Long
Dim DriveType as Long
Dim allDrives as string
Dim JustOneDrive as string
Dim CDLabel as string
Dim pos as Integer
Dim CDfound as Boolean
Dim found as string
allDrives$ = Space$(64)
r& = GetLogicalDriveStrings(len(allDrives$), allDrives$)

allDrives$ = Left$(allDrives$, r&)


Do
pos% = InStr(allDrives$, Chr$(0))


If pos% then

JustOneDrive$ = Left$(allDrives$, pos% - 1)


allDrives$ = mid$(allDrives$, pos% + 1, len(allDrives$))

DriveType& = GetDriveType(JustOneDrive$)


If DriveType& = DRIVE_CDROM then

CDfound = true
Exit Do
End If

End If

Loop Until allDrives$ = "" Or DriveType& = DRIVE_CDROM

If CDfound then
found = UCase$(JustOneDrive$)
else: MsgBox "CD ROM wasn't detected!!!", vbCritical
End If
If FileExist(found + "Check.rak") = true then
MsgBox "Exists"
else
MsgBox "Does not exist"
End If

BrewGuru99
December 29th, 1999, 01:23 PM
Wonderful!

The only problem is if there are multiple CD ROM drives on the system, and my disk happens to be in a latter drive. I'll post my mods when I finish them.

Thanks Andy!

Brewguru99

AndyK
December 29th, 1999, 03:22 PM
No problem, good luck with your project!!!