CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Detecting CD ROM Drive

    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

  2. #2
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: Detecting CD ROM Drive

    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







  3. #3
    Join Date
    Oct 1999
    Location
    CA
    Posts
    91

    Re: Detecting CD ROM Drive

    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

  4. #4
    Join Date
    Aug 1999
    Location
    US, Florida
    Posts
    817

    Re: Detecting CD ROM Drive

    No problem, good luck with your project!!!


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

Featured