Platform SDK: Hardware

Registering for Device Notification

Applications can use the RegisterDeviceNotification function to register to receive notification messages from the system. The following example function demonstrates how to register for notification of events for the device interfaces which are members of the interface class whose GUID is passed to the function.

BOOL DoRegisterDeviceInterface( 
    GUID InterfaceClassGuid, 
    HDEVNOTIFY *hDevNotify 
)
/*
Routine Description:
    Registers for notification of changes in the device interfaces for
    the specified interface class GUID. 

Parameters:
    InterfaceClassGuid - The interface class GUID for the device 
        interfaces. 

    hDevNotify - Receives the device notification handle. On failure, 
        this value is NULL.

Return Value:
    If the function succeeds, the return value is TRUE.

    If the function fails, the return value is FALSE.
*/
{
    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
    DWORD Err;

    ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
    NotificationFilter.dbcc_size = 
        sizeof(DEV_BROADCAST_DEVICEINTERFACE);
    NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = InterfaceClassGuid;

    *hDevNotify = RegisterDeviceNotification( hWnd, 
        &NotificationFilter,
        DEVICE_NOTIFY_WINDOW_HANDLE
    );

    if(!*hDevNotify) 
    {
        Err = GetLastError();
        printf( "RegisterDeviceNotification failed: %lx.\n", Err);
        return FALSE;
    }

    return TRUE;
}

The application will receive the WM_DEVICECHANGE message whenever a device interface event notification is sent. Currently, the only events that will be received for device interfaces are DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE.