Platform SDK: Hardware

Device Events

Applications, including services, can register to receive notification of device events. For example, a catalog service can receive notice of volumes being mounted or dismounted so it can adjust the paths to files on the volume. The system notifies an application that a device event has occurred by sending the application a WM_DEVICECHANGE message. The system notifies a service that a device event has occurred by invoking the service's event handler function, HandlerEx.

To receive device event notices, call the RegisterDeviceNotification function with a DEV_BROADCAST_HANDLE structure. Be sure to set the dbch_handle member to the device handle obtained from the CreateFile function. Also, set the dbch_devicetype member to DBT_DEVTYP_HANDLE. The function returns a device notification handle. Note that this is not the same as the volume handle.

When your application receives notification, if the event type is DBT_CUSTOMEVENT, you may have received one of the device events defined in IoEvent.h. To determine if one of these events has occurred, use the following steps.

  1. Treat the event data as a DEV_BROADCAST_HDR structure. Verify that the dbch_devicetype member is set to DBT_DEVTYPE_HANDLE.
  2. If dbch_devicetype is DBT_DEVTYPE_HANDLE, the event data is really a pointer to a DEV_BROADCAST_HANDLE structure.
  3. Compare the dbch_eventguid member to the GUIDs listed in the following table using the IsEqualGUID function.
Device Event GUID Meaning
GUID_IO_VOLUME_CHANGE The volume label has changed.
GUID_IO_VOLUME_DISMOUNT An attempt to dismount the volume is in progress. You should close all handles to files and directories on the volume. GUID_IO_VOLUME_DISMOUNT will not necessarily be preceded by a GUID_IO_VOLUME_LOCK notice.
GUID_IO_VOLUME_DISMOUNT_FAILED An attempt to dismount a volume failed. This often happens because another process failed to respond to a GUID_IO_VOLUME_DISMOUNT notice by closing its outstanding handles. Because the dismount failed, you may reopen any handles to the affected volume.
GUID_IO_VOLUME_LOCK Another process is attempting to lock the volume. You should close all handles to files and directories on the volume.
GUID_IO_VOLUME_LOCK_FAILED An attempt to lock a volume failed. This often happens because another process failed to respond to a GUID_IO_VOLUME_LOCK notice by closing its outstanding handles. Because the lock failed, you may reopen any handles to the affected volume.
GUID_IO_VOLUME_MOUNT The volume has been mounted by another process. You may open one or more handles to it.
GUID_IO_VOLUME_UNLOCK The volume has been unlocked by another process. You may open one or more handles to it.

The GUID_IO_VOLUME_DISMOUNT and GUID_IO_VOLUME_DISMOUNT_FAILED events are related, as are the GUID_IO_VOLUME_LOCK and GUID_IO_VOLUME_LOCK_FAILED event. The GUID_IO_VOLUME_DISMOUNT and GUID_IO_VOLUME_LOCK events indicate that an operation is being attempted. You should act on the event notification, and record the action taken. The GUID_IO_VOLUME_DISMOUNT_FAILED and GUID_IO_VOLUME_LOCK_FAILED events indicate that the attempted operation failed. You may then use your record to undo the actions you made in response to the operation.

The dbch_hdevnotify member of the DEV_BROADCAST_HANDLE structure indicates the affected device. Note that this is the device notification handle returned by RegisterDeviceNotification, not a volume handle. To perform operations on the volume, map this handle to the corresponding volume handle.