Results 1 to 7 of 7

Thread: determine CD-Rom Drive

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2001
    Location
    Philippines
    Posts
    101

    determine CD-Rom Drive

    hi,

    how can i determine the drive letter of a PC's CD-Rom?
    Thanks in advance

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    try this sample from Kaverin,

    VB Code:
    1. 'Author Kaverin
    2. 'in a module
    3. Public Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    4. Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    5.  
    6. Public Function GetCDDriveLetter() As String
    7.    Dim strBuffer As String
    8.    Dim astrDrives As Variant
    9.    Dim lngLength As Long
    10.    Dim i As Integer
    11.    Const CDROM As Long = 5
    12.    'make buffer
    13.    strBuffer = Space$(256)
    14.    'get all drive strings
    15.    lngLength = GetLogicalDriveStrings(Len(strBuffer), strBuffer)
    16.    'chop off the unnecessary part
    17.    strBuffer = Left$(strBuffer, lngLength)
    18.    'break up the string into the drives
    19.    astrDrives = Split(strBuffer, vbNullChar)
    20.    For i = 0 To UBound(astrDrives)
    21.       If GetDriveType(astrDrives(i)) = CDROM Then
    22.          'if the drive is a cd rom, we're done, so return the letter
    23.          GetCDDriveLetter = Left$(astrDrives(i), 1)
    24.          Exit Function
    25.       End If
    26.    Next i
    27.    'if you get to this point, there is no cd rom, so return ""
    28.    GetCDDriveLetter = ""
    29. End Function

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    Private Declare Function GetDriveType Lib "kernel32" Alias _
    "GetDriveTypeA" (ByVal nDrive As String) As Long
    
    Private Sub Form_Load()
        Dim intLoopCounter As Integer
        
        For intLoopCounter = 65 To 90
        
            If (GetDriveType(Chr(intLoopCounter) & ":\") = 5) Then
                MsgBox "The first CD-ROM drive is :" & Chr(intLoopCounter)
                Exit For
            End If
        Next intLoopCounter
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    It is becoming more frequent that PCs have more than one CD Rom drive. If anyone has a better way of determing all CD Rom drives, that would be very cool. This works, but it returns all mapped drives, rather than just the CD Rom drives.
    VB Code:
    1. Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    2. Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
    3.  
    4. Private Function GetDriveTypeByLetter(MyDriveLetter As String) As String
    5.  
    6. Dim MyDrive As String
    7.  
    8. If Right(MyDriveLetter, 1) <> "\" Then
    9.     MyDrive = MyDriveLetter & "\"
    10. Else
    11.     MyDrive = MyDriveLetter
    12. End If
    13.  
    14. Select Case GetDriveType(MyDrive)
    15.     Case 2
    16.     GetDriveTypeByLetter = "Removable"
    17.     Case 3
    18.     GetDriveTypeByLetter = "Fixed Hard Drive"
    19.     Case 4
    20.     GetDriveTypeByLetter = "Remote"
    21.     Case 5
    22.     GetDriveTypeByLetter = "CD-ROM"
    23.     Case 6
    24.     GetDriveTypeByLetter = "Ram disk"
    25.     Case Else
    26.     GetDriveTypeByLetter = "Unrecognized"
    27. End Select
    28.  
    29. End Function
    30. Private Sub Form_Load()
    31.  
    32. Dim SaveIt As String
    33. Dim i As Long
    34. Dim MyDrives As Long
    35.  
    36. SaveIt = String(255, Chr$(0))
    37. MyDrives = GetLogicalDriveStrings(255, SaveIt)
    38.  
    39. For i = 1 To 100
    40.     If Left$(SaveIt, InStr(1, SaveIt, Chr$(0))) = Chr$(0) Then Exit For
    41.     List1.AddItem Left$(SaveIt, InStr(1, SaveIt, Chr$(0)) - 1) & vbTab & GetDriveTypeByLetter(Left$(SaveIt, InStr(1, SaveIt, Chr$(0)) - 1))
    42.     SaveIt = Right$(SaveIt, Len(SaveIt) - InStr(1, SaveIt, Chr$(0)))
    43. Next
    44.  
    45. End Sub

  5. #5
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    There is a simpler way - my code loops through A-Z, just picks the first up & then exits the function, however, changing a few lines - will make it return all of the CD rom drives no problem :
    Code:
    Private Declare Function GetDriveType Lib "kernel32" Alias _
    "GetDriveTypeA" (ByVal nDrive As String) As Long
    
    Private Sub Form_Load()
        Dim intLoopCounter As Integer, intAryCounter As Integer
        Dim strAryCDDrives() As String
    
        intAryCounter = 0
        
        For intLoopCounter = 65 To 90
        
            If (GetDriveType(Chr(intLoopCounter) & ":\") = 5) Then
                ReDim strAryCDDrives(intAryCounter + 1)
                strAryCDDrives(intAryCounter) = Chr(intLoopCounter)
                intAryCounter = intAryCounter + 1
            End If
            
        Next intLoopCounter
        
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Sorry, getting complicated - that one was putting them in an array, this is a simple one that does the same to a string. Hope it helps
    Code:
    Private Declare Function GetDriveType Lib "kernel32" Alias _
    "GetDriveTypeA" (ByVal nDrive As String) As Long
    
    Private Sub Form_Load()
        Dim intLoopCounter As Integer
        Dim strCDDrives As String
        
        For intLoopCounter = 65 To 90
            If (GetDriveType(Chr(intLoopCounter) & ":\") = 5) Then
                strCDDrives = strCDDrives & chr(intLoopCounter) & VBCrlf
            End If
        Next intLoopCounter
     
        Msgbox "CD Rom Drives On this PC are : " & _
        VBCrlf & VBCrlf & strCDDrives    
    End Sub
    Last edited by alex_read; Jan 4th, 2002 at 09:59 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Very cool Alex!

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