Opening a Device

Before using a device, you must initialize it using the MCI_OPEN command message. The variations of this command make it one of the most complex of all MCI commands.

Obtaining the Device ID

MCI always needs a pointer to the data structure corresponding to the MCI_OPEN command message. Most devices use the MCI_OPEN_PARMS data structure. However, some devices use a device-specific data structure that defines the same data fields as MCI_OPEN_PARMS, as well as additional device-specific data fields. The MCI_OPEN_PARMS data structure has the following fields:

typedef struct {
     DWORD        dwCallback;                        /* callback for MCI_NOTIFY */
     WORD        wDeviceID;                        /* device ID returned to user */
     WORD        wReserved0;                        /* reserved */
     LPSTR        lpstrDeviceType;        /* device type */
     LPSTR        lpstrElementName;        /* device element */
     LPSTR        lpstrAlias;                        /* optional device alias */
} MCI_OPEN_PARMS;

MCI uses the wDeviceID field of this structure to return the device ID to your application. You should check the return value from mciSendCommand before using the device ID to be sure that it is valid. A non-zero return value indicates that an error occurred during the open process.

Other fields used in opening a device correspond to the following flags for the command.

Flag Description

MCI_OPEN_ALIAS Specifies that the lpstrAlias field of the data structure contains a pointer to a device alias.
MCI_OPEN_ELEMENT Specifies that the lpstrElementName field of the data structure contains a pointer to the element name.
MCI_OPEN_SHAREABLE Specifies that the device or element was opened as shareable.
MCI_OPEN_TYPE Specifies that the lpstrDeviceType field of the data structure contains a pointer to the device-type identifier.
MCI_OPEN_TYPE_ID Specifies that the lpstrDeviceType field of the data structure contains an integer device-type identifier.

Opening Simple Devices

Simple devices don't require a device element. Typically, this means a device does not have a file associated with it. For example, CD audio and videodisc players operate on the installed disc; the device does not need any application-supplied information on the contents of the media it is to operate.

To open a simple device, your application can use just the wDeviceID and lpstrDeviceType fields of the data structure. Set the wDeviceID field to NULL for the initial open. Set the lpstrDeviceType field to point to the null-terminated string specifying either the device type used in your SYSTEM.INI file or the filename of the device driver. If you specify the filename of the device driver, do not include the path to the file. You can optionally exclude the .DRV extension. When sending this data structure to MCI, tell MCI that the lpstrDeviceType field contains data by also sending it the MCI_OPEN_TYPE flag in dwParam1.

Opening Compound Devices

Compound devices use the lpstrElementName field for the device element and the lpstrDeviceType field for the device type. The MCI_OPEN_ELEMENT and MCI_OPEN_TYPE flags tell MCI that the fields in the data structure contain valid information. Depending on your application, there are three ways you can open a compound device:

Specify only the device type

Specify both the device type and the device element

Specify only the device element

To determine the capabilities of a device, you can open a device by specifying just the device type. You can specify the device type either by using the device type from the SYSTEM.INI file or by using the filename of the device driver. If you specify the filename of the device driver, don't include the path to the file. You can optionally exclude the .DRV extension. When opened this way, most com-pound devices will only let you determine their capabilities and close them.

To associate a device element with a particular device, you must specify both the device type (or filename) and element name. This combination lets your application specify the MCI device it needs.

To associate a default MCI device with a device element, you can specify NULL for the device type. In this case, MCI uses the extension of the device-element name to select the default device from the list in the [mci extensions] section of your WIN.INI file. The entries in the [mci extensions] section have the following form:

file extension=device type

MCI implicitly uses the device type if the extension is found. The following fragment shows a typical [mci extensions] section:

[mci extensions]
wav=waveaudio
mid=sequencer
rmi=sequencer
mmm=animation

Using the Shareable Flag

If your application opens a device or device element without the MCI_OPEN_SHAREABLE flag, no other application can access it simulta-neously. If your application opens a device or device element as shareable, other applications can also access it by also opening it as shareable. The shared device or device element gives each application the ability to change the operating parameters or state of the device or device element.

If you make a device or device element shareable, your application should not make any assumptions about the state of a device. When working with shared devices, your application might need to compensate for changes made by other applications using the same services.

If a device can service only one application or task, it will fail an open that uses the MCI_OPEN_SHAREABLE flag.

Using the Device Type Constant

Your application can identify the device type by an integer rather than by string name. The following list contains the constants you can use to identify device types (if you use a constant to identify the device type, you must use the MCI_OPEN_TYPE_ID flag combined with the MCI_OPEN_TYPE flag):

Device Type Constant

animation MCI_DEVTYPE_ANIMATION
cdaudio MCI_DEVTYPE_CD_AUDIO
dat MCI_DEVTYPE_DAT
digitalvideo MCI_DEVTYPE_DIGITAL_VIDEO
other MCI_DEVTYPE_OTHER
overlay MCI_DEVTYPE_OVERLAY
scanner MCI_DEVTYPE_SCANNER
sequencer MCI_DEVTYPE_SEQUENCER
vcr MCI_DEVTYPE_VIDEOTAPE
videodisc MCI_DEVTYPE_VIDEODISC
waveaudio MCI_DEVTYPE_WAVEFORM_AUDIO

When the MCI_OPEN_TYPE_ID flag is combined with the MCI_OPEN_TYPE flag, MCI interprets the low-order word of the lpstrDeviceType field as the device-type constant. MCI uses the high-order word of this field to distinguish between multiple occurrences of a device type in your SYSTEM.INI file.

For example, if your SYSTEM.INI file lists “waveaudio1” and “waveaudio2”, and your application sets the low-order word of lpstrDeviceType to the “waveaudio” constant and the high-order word to 2, MCI would address the device identified by “waveaudio2”. MCI would address “waveaudio1” if your application set the high-order word to 0 or 1 (setting the high-order word to 0 tells MCI to use an unnumbered device or the lowest-numbered device).