Article ID: 163503 - View products that this article applies to.
This article was previously published under Q163503
Some applications need to know when the user inserts or removes a compact disc or DVD from a CD-ROM drive without polling for media changes. Windows provide a way to notify these applications through the WM_DEVICECHANGE message. This article explains how to handle the WM_DEVICECHANGE message to detect CD-ROM or DVDmedia changes.
Windows sends all top-level windows a WM_DEVICECHANGE message when new devices or media are added and become available, and when existing devices or media are removed. Each WM_DEVICECHANGE message has an event associated with it to describe the change, and a structure that provides detailed information about the change.
The structure consists of an event-independent header followed by an event- dependent structure. The event-dependent part of the structure describes which device the event applies to. To use this structure, applications must first determine the event type and the device type. Then, they can use the correct structure to take appropriate action.
When the user inserts a new compact disc or DVD into a drive, applications get a WM_DEVICECHANCE message with a DBT_DEVICEARRIVAL event. The application must check the event to make sure that the type of device arriving is a volume (DBT_DEVTYP_VOLUME) and that the event's media flag (DBTF_MEDIA) is set.
When the user removes a compact disc from a CD-ROM drive or DVD, applications will get a WM_DEVICECHANCE message with a DBT_DEVICEREMOVECOMPLETE event. As with DBT_DEVICEARRIVAL above, the application must check the event to make sure that the device being removed is a volume and that the event's media flag is set.
Sample CodeThe following code demonstrates how to use the WM_DEVICECHANGE message to check for compact disc or DVD insertion or removal.
Although this sample code only checks for volume arrivals due to the insertion of new media, it can be extended to get notification of other hardware events for other types too. To do so, you have to add cases for other device events and handle different device types for each event.
Microsoft Platform SDK documentation, "Device Management" overview" chapter
Article ID: 163503 - Last Review: May 1, 2007 - Revision: 4.3
Contact us for more help
Connect with Answer Desk for expert help.