how to detect cd rom drive


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: how to detect cd rom drive

  1. #1
    Join Date
    Aug 2004
    Posts
    43,023

    how to detect cd rom drive

    [Originally posted by liju john]

    is there any vb code /api function to detect
    the name of cdrom drive in our computer?

  2. #2
    Join Date
    Aug 2004
    Posts
    43,023

    Re:how to detect cd rom drive

    [Originally posted by Ron Weller]


    ' **********************************************************************
    '
    ' FUNCTION:
    ' GetFirstCdRomDriveLetter()
    '
    ' PURPOSE:
    ' Finds the first CD-ROM device and then returns its drive letter.
    '
    ' ARGUMENTS:
    ' None
    '
    ' RETURNS:
    ' A string that represents the first CD-ROM drive letter. If the
    ' function fails for any reason, it returns vbNullString.
    '
    ' **********************************************************************
    Declare Function GetDriveType Lib "kernel32" Alias _
    "GetDriveTypeA" (ByVal nDrive As String) As Long

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

    Public Const DRIVE_CDROM As Long = 5

    Function GetFirstCdRomDriveLetter() As String

    ' Declare variables.
    Dim lDriveType As Long
    Dim strDrive As String
    Dim lStart As Long: lStart = 1

    ' Create a string to hold the logical drives.
    Dim strDrives As String
    strDrives = Space(150)

    ' Get the logial drives on the system.
    ' If the function fails it returns zero.
    Dim lRetVal As Long
    lRetVal = GetLogicalDriveStrings(150, strDrives)

    ' Check to see if GetLogicalDriveStrings() worked.
    If lRetVal = 0 Then

    ' Get GetLogicalDriveStrings() failed.
    GetFirstCdRomDriveLetter = vbNullString
    Exit Function
    End If

    ' Get the string that represents the first drive.
    strDrive = Mid(strDrives, lStart, 3)

    Do

    ' Test the first drive.
    lDriveType = GetDriveType(strDrive)

    ' Check if the drive type is a CD-ROM.
    If lDriveType = DRIVE_CDROM Then

    ' Found the first CD-ROM drive on the system.
    GetFirstCdRomDriveLetter = strDrive
    Exit Function
    End If

    ' Increment lStart to next drive in the string.
    lStart = lStart + 4

    ' Get the string that represents the first drive.
    strDrive = Mid(strDrives, lStart, 3)

    Loop While (Mid(strDrives, lStart, 1) <> vbNullChar)

    End Function


    'Following is an example of calling the GetFirstCdRomDriveLetter() function from a macro (Sub procedure):

    Sub Main

    Dim strDriveLetter as String

    ' Call the GetFirstCdRomDriveLetter() and store the
    ' return value in strDriveLetter.
    strDriveLetter = GetFirstCdRomDriveLetter()

    ' Display the drive letter in a message box.
    MsgBox strDriveLetter

    End Sub



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center