Hello, user mode? Plug and Play calling
Updated: May 25, 2007
You can use Plug and Play notifications for simple one-way communication from a kernel-mode driver to a user-mode application. To do this, call IoReportTargetDeviceChangeAsynchronous with a TARGET_DEVICE_CUSTOM_NOTIFICATION structure that describes the event. This routine signals the Plug and Play manager that the event has occurred on a device.
The system defines several types of Plug and Play events that signal changes to a device interface, hardware profile, or target device. These are sent automatically, without any action on the part of the driver. A driver can define custom events, which don't necessarily have to relate to hardware. All that's needed is to publish a GUID for the event and populate the TARGET_DEVICE_CUSTOM_NOTIFICATION structure with the GUID and other information about the event.
Any number of user-mode applications can register for PnP notifications from your driver, by calling RegisterDeviceNotification and passing a DEV_BROADCAST_HANDLE notification filter that includes the handle to the device. (A kernel-mode driver registers for system-defined Plug and Play events by calling IoRegisterPlugPlayNotification to register a callback routine for a particular event category.)
When the event occurs, the system notifies all registered user-mode applications through the standard Windows messaging mechanism: Each registered application receives a WM_DEVICECHANGE message with a WPARAM of DBT_CUSTOMEVENT and an LPARAM that points to a structure containing event-specific data.
Plug and Play notifications are not guaranteed to work if the system is low on memory. IoReportTargetDeviceChangeAsynchronous allocates memory immediately on being called. If the allocation fails, the notification is never sent. In user mode, the system must allocate memory for the LPARAM for the message; if that fails, the notification is never received.
Also, notifications are transitory and there is no stored history. Assuming memory allocations succeed, an application receives only notifications that were sent after it registered to receive them. Notifications that were sent before the application registered are no longer available.
Finally, because Plug and Play notifications are one-way communication, you'll need to use a different mechanism if your driver must synchronize with a user-mode application. Shared events and driver-defined IOCTLs are two ways of accomplishing this. See the Event sample in the Windows DDK for sample code that illustrates these techniques.
What should you do?
To use Plug and Play notifications to communicate with a user-mode application:
Example 1: Broadcast Notification of an Event. The following code sample shows how a kernel-mode driver would use IoReportTargetDeviceChangeAsynchronous to broadcast notification of an event-in this case, GUID_SOME_PNP_EVENT, which consists of the message "Hello."
Example 2: Register for Notification. The following code sample shows how a user-mode application would register for notification of changes in device interfaces for an interface class GUID.
Example 3: Handle a Custom Event. The following code sample shows how a user-mode application would handle the WM_DEVICECHANGE message for a custom event. See the Notify sample in the Windows DDK for more code that demonstrates how to register for and handle Plug and Play notifications.
For more information:
Using PnP Notification