CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Sunny South Africa

    Arrow [VB6] How Do I Get The CD ROM Drive Letter?

    Q: What APIs Are Needed In Order To Obtain The CD ROM Drive Letter On The System?

    A: You will only need 2 APIs for this purpose :

    You declare them like the following :

    'Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive
    Private Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
        (ByVal nDrive As String) As Long
    'Fills a buffer with strings that specify valid drives in the system.
    Private Declare Function GetLogicalDriveStrings Lib "kernel32" _
        Alias "GetLogicalDriveStringsA" _
        (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    Const DRIVE_CDROM& = 5 'Signifies CD ROM
    Q: How do I actually use these APIs, In Other Words, How Do We Implement Them?

    A: We will need to create a Function / Sub procedure to basically translate the GetLogicalDriveStrings API's return value, so that we can understand it properly, and provide the correct output to the user, in an understandable way :

    Public Function GetDriveStrings() As String
        ' Wrapper for calling the GetLogicalDriveStrings api
        Dim result As Long          ' Result of our API calls
        Dim strDrives As String     ' String to pass to API call
        Dim lenStrDrives As Long    ' Length of the above string
        ' Call GetLogicalDriveStrings with a buffer size of zero to
        ' find out how large our stringbuffer needs to be
        result = GetLogicalDriveStrings(0, strDrives)
        strDrives = String(result, 0)
        lenStrDrives = result
        ' Call again with our new buffer
        result = GetLogicalDriveStrings(lenStrDrives, strDrives)
        If result = 0 Then
            ' There was some error calling the API
            ' Pass back an empty string
            ' NOTE - TODO: Implement proper error handling here
            GetDriveStrings = ""
            GetDriveStrings = strDrives
        End If
    End Function

    Q: IS That All?

    A: Nope. Now that we have the correct converted output, we still need to inform the user. The next segment firstly ascertains whether or not a CD ROM exists on the system, then provides us with its Drive letter..

    Private Sub Command1_Click()
        Dim strDrives As String
        Dim blnDrive As Boolean
        ' Find out what drives we have on this machine
        strDrives = GetDriveStrings()
        If strDrives = "" Then
            ' No drives were found
            MsgBox "No Drives were found!", vbCritical
            ' Walk through the string and check the type of each drive
            ' displaying any cd-rom drives we find
            Dim pos As Long
            Dim drive As String
            Dim drivetype As Long
            pos = 1
            Do While Not Mid$(strDrives, pos, 1) = Chr(0)
                drive = Mid$(strDrives, pos, 3)
                pos = pos + 4
                drivetype = GetDriveType(drive)
                If drivetype = DRIVE_CDROM Then
                        blnDrive = True
                        blnDrive = False
                End If
            If blnDrive Then
                MsgBox "CD-ROM found at drive " & UCase(drive)
                MsgBox "No CD ROM found!"
            End If
        End If
    End Sub
    A full working sample is attached to this post.
    Attached Files Attached Files

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