Enumerating CD Devices on Your System

This short code sample shows how to determine all the CDRom drives you have connected to your computer using the WinAPI.

Although this can now be done with the new FileSystemObject that comes with VB6 (and as an extra with VB5), this is how to get the drives through code when the target machine may not have the SSCRUN.DLL.


Screen-Shot

The code is quite simple and uses the GetDriveType and GetLogicalDriveStrings Windows API calls.


'
private Declare Function GetDriveType Lib "kernel32" _
        Alias "GetDriveTypeA" (byval nDrive as string) as Long
private Declare Function GetLogicalDriveStrings Lib "kernel32" _
        Alias "GetLogicalDriveStringsA" (byval nBufferLength as Long, _
        byval lpBuffer as string) as Long
private Const DRIVE_CDROM = 5
'
private Sub ListCDROMs()
    Dim sDrives as string
    Dim sDrive as string

    sDrives = Space(255)
    sDrives = Left$(sDrives, GetLogicalDriveStrings(255, byval sDrives))
    While InStr(sDrives, "\")
        sDrive = Left$(sDrives, InStr(sDrives, "\"))
        If GetDriveType(sDrive) = DRIVE_CDROM then
            List1.AddItem "(CDRom) " & sDrive
        End If
        sDrives = mid$(sDrives, len(sDrive) + 2)
    Wend
End Sub
'

Download Zipped Project File (3k)



Comments

  • More helpful for me

    Posted by Legacy on 07/25/2003 12:00am

    Originally posted by: Mohan

    Thanks lot to provide code. Your e-help is much appreciated.
    

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds