Closing a Movie File

After you finish showing the movie, use the MCI_CLOSE command message to close the movie file. This also closes the Movie Player instance.

Before exiting, your application must close any MCI devices it opened. Each movie file an application opens is a separate MCI device. If an application fails to close a device, the device remains open after the application ends. This can prevent your application or other applications from reopening MCI devices or accessing movie files and might also cause unrecoverable application errors.

You can use the MCI_ALL_DEVICE_ID constant with the MCI_CLOSE command; using this constant closes all devices that your application opened. For example, the code in the following example closes all devices opened by the application:

dwError = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, 0, NULL); 

if(dwError)                         // Check return value
    showError(dwError);

This code fragment (as well as the others in this chapter) tests the values returned from the mciSendCommand function. It calls a showError function if an error occurs; the showError function uses the mciGetErrorString, MessageBeep, and MessageBox functions to display the MCI error message. The showError function is described in Chapter 4, “High-Level Audio Services.”

Your application should test the mciSendCommand return values; when learning to use MCI, it's easy to omit a required flag or constant.