July 1, 1995
Within your Visual Basic® application, you can determine whether an attached drive is actually a CD-ROM drive. This article explains how you can identify a CD-ROM drive in Visual Basic.
From within a Visual Basic® application program, you can determine whether a disk drive is actually a CD-ROM drive. To do this, however, you need to use a special dynamic-link library (DLL) called VBASM.DLL.
The VBASM.DLL dynamic-link library allows you to perform low-level routines that Visual Basic itself cannot perform. Written entirely in assembly language, VBASM is available in the Microsoft® Development Library. (See Additional References at the end of this article.)
The example program below tests each possible disk drive from 0 through 25 (for a total of 26 possible disk drives) to see whether that particular drive is a CD-ROM drive. The program does this by calling a low-level Int 2Fh multiplex interrupt function.
Function 150Bh, Int 2Fh, tells you whether or not the specified disk drive is a valid CD-ROM drive. To call this function, you set the AX register to 150Bh and the CX register to the number of the disk drive you want to check. The function will return with the BX register set to ADADh if the MSCDEX.EXE device driver (that is, the CD-ROM driver) is installed, and the AX register is set to a nonzero value if the specified disk is a CD-ROM drive. It is, therefore, simply a matter of testing each possible disk drive, from 0 through 25, to determine exactly how many CD-ROM drives are attached to the computer system.
This program tests each installed disk drive to determine whether it is a CD-ROM drive. In addition, the drive letter of each CD-ROM drive is displayed in the Text Box along with the total number of CD-ROM drives that the program found.
Private Sub Command1_Click()
Dim I As Integer
Dim DriveType As Integer
Dim Drive As String * 2
Dim TotalCDDrives As Integer
Dim Regs As VBREGS
TotalCDDrives = 0
Text1.Text = ""
For I = 0 To 25
Regs.AX = &H150B
Regs.BX = &H0
Regs.CX = I
Call vbInterrupt(&H2F, Regs, Regs)
If (Regs.BX = &HADAD) Then
Debug.Print Regs.AX
If (Regs.AX <> 0) Then
TotalCDDrives = TotalCDDrives + 1
Text1.Text = Text1.Text & Chr$(I + 65) & " is a CD-ROM drive" &
Chr$(13) & Chr$(10)
End If
End If
Next I
If (TotalCDDrives = 0) Then
Text1.Text = Text1.Text & "No CD-ROM drives were found."
Else
Text1.Text = Text1.Text & Chr$(13) & Chr$(10) & Str$(TotalCDDrives) &
"CD-ROM drives were found."
End If
End Sub
Run the example program by pressing f5. Click the command button. In the Text Box control, the program will display the drive letter of each CD-ROM drive found installed in the computer system, in addition to a total count of the number of attached CD-ROM drives.
"Detecting CD-ROM Drives." (MSDN Library Archive, Technical Articles, Ask Dr. GUI, Ask Dr. GUI #7)
"DRIVES: Determines Drive and File System Type." (MSDN Library, Product Samples)
Knowledge Base Q105922. "How to Determine Drive Types in Windows."