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.
Like all MCI command messages, MCI_OPEN has an associated parameter block. The default parameter block for MCI_OPEN is the MCI_OPEN_PARMS data structure. Certain devices such as waveform, animation, and overlay devices have extended parameter blocks to accommodate additional optional parameters. Unless you need to use these additional parameters, you can use the MCI_OPEN_PARMS parameter block with any MCI device.
The MCI_OPEN_PARMS data structure has the following fields:
typedef struct {
DWORD dwCallback; /* callback for MCI_NOTIFY */
UINT wDeviceID; /* device ID returned to user */
UINT wReserved0; /* reserved */
LPCSTR lpstrDeviceType; /* device type */
LPCSTR lpstrElementName; /* device element */
LPCSTR lpstrAlias; /* optional device alias */
} MCI_OPEN_PARMS;
MCI uses the wDeviceID field of this structure to return the device ID to the 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.
Note:
You can also determine the device ID with mciGetDeviceID by specifying the device name.
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. |
To open a simple device such as a CD audio or videodisc device, use the lpstrDeviceType field in the MCI_OPEN_PARMS parameter block to identify which device to open. There are three ways to identify devices:
Specify a pointer to a null-terminated string containing the device name
Specify a device-type constant
Specify the actual name of the device driver
Regardless of which method you use to identify the device, you must specify the MCI_OPEN_TYPE flag to validate the lpstrDeviceType field. For example, the following code fragment opens a CD audio device by specifying the device name:
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
/* Open a compact disc device by specifying the device name
*/
mciOpenParms.lpstrDeviceType = "cdaudio";
if (dwReturn = mciSendCommand(NULL, MCI_OPEN, MCI_OPEN_TYPE,
(DWORD)(LPVOID) &mciOpenParms))
/* Error, unable to open device
*/
...
/* Device opened successfully, get the device ID
*/
wDeviceID = mciOpenParms.wDeviceID;
You can also open devices by specifying a device-type constant. The following table gives the device-type constants for various devices. To be opened with a device-type constant, device drivers must be installed in the [mci] section of SYSTEM.INI using the device names given in this table.
Device Name | 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 |
If you open a device by specifying a device-type constant, you must specify the MCI_OPEN_TYPE_ID flag in addition to the MCI_OPEN_TYPE flag. For example, the following code fragment opens a CD audio device by specifying a device-type constant:
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
/* Open a compact disc device by specifying a device-type constant
*/
mciOpenParms.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO;
if (dwReturn = mciSendCommand(NULL, MCI_OPEN,
MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID,
(DWORD)(LPVOID) &mciOpenParms))
/* Error, unable to open device
*/
...
/* Device opened successfully, get the device ID
*/
wDeviceID = mciOpenParms.wDeviceID;
When you open a device by specifying a device-type constant, MCI uses the high-order word of the lpstrDeviceType field to distinguish between multiple occurrences of the device type in the SYSTEM.INI file.
For example, if SYSTEM.INI lists entries for waveaudio1 and waveaudio2, the following statement sets the lpstrDeviceType field to specify the device identified by waveaudio2:
lpstrDeviceType = MAKELONG(MCI_DEVTYPE_WAVEFORM_AUDIO, 2);
Likewise, to specify the device identified by waveaudio1, you would set the high-order word of lpstrDeviceType to 0 or 1 (setting the high-order word to 0 tells MCI to use an unnumbered device or the lowest-numbered device).
You can also open a device by specifying the filename of the device driver in the lpstrDeviceType field of the MCI_OPEN_PARMS parameter block. You don't need to specify the complete path or the file extension—MCI assumes drivers are located in the Windows system directory and have a file extension of “DRV”. For example, the following code fragment opens a device by specifying the filename of the device driver:
Note:
Opening MCI devices by specifying the filename of the device driver may be useful during development and testing. However, this technique is not recommended for applications since it makes them device-dependent. Applications that open MCI devices by filename may not work in all systems.
There are three ways to open a compound device:
Specify only the device (using the device name, a device-type constant, or the filename of the device driver).
Specify both the device and the device element.
Specify only the device element.
The first method, specifying only the device, is useful for determining the capabilities of a device with the MCI_GETDEVCAPS command. To actually control a media device, you must specify a device element when you open the device.
To specify a device element, fill the lpstrElementName field of the MCI_OPEN_PARMS parameter block with a pointer to a null-terminated string containing the filename of the device element. You must also validate the lpstrElementName field by specifying the MCI_OPEN_ELEMENT flag in the dwFlags parameter of mciSendCommand. To specify the device, you can use the same methods used for simple devices: specify the device by device name by device-type constant or by the filename of the driver. For example, the following code fragment opens the waveform audio device with a WAVE file named “TIMPANI.WAV”:
UINT wDeviceID;
DWORD dwReturn;
MCI_OPEN_PARMS mciOpenParms;
/* Open a waveform device by specifying the device name and device element
*/
mciOpenParms.lpstrDeviceType = "waveaudio";
mciOpenParms.lpstrElementName = "timpani.wav";
if (dwReturn = mciSendCommand(NULL, MCI_OPEN,
MCI_OPEN_TYPE | MCI_OPEN_ELEMENT,
(DWORD)(LPVOID) &mciOpenParms))
/* Error, unable to open device
*/
...
/* Device opened successfully, get the device ID
*/
wDeviceID = mciOpenParms.wDeviceID;
You can also open a device by specifying just a device element—MCI uses the filename extension of the given device element along with the [mci extensions] section of WIN.INI to select which device to open. See “Opening MCI Devices,” earlier in this chapter, for more information about opening devices by specifying only a device element.