See RegisterDeviceNotification

It works as I recall.