SLAA453A january 2011 – may 2023
Unlike with traditional COM ports, USB applications must be aware that the user may detach (or attach) a device at any time. This is the dynamic "plug-and-play" aspect of USB. Windows handles this through notifications, implemented in the MFC library. This occurs outside the API, because the notifications go directly to the application's window object; but when these notifications are received, the application can call API handlers specifically provided for this purpose. The demo application shows how this can be done.
These are the steps required for an application to handle these notifications:
The definition of the function should be as shown:
afx_msg BOOL OnDeviceChange(
UINT nEventType,
DWORD_PTR dwData
);
This function needs to determine if any of the "HID devices" used by this application were removed from the system. To that end, it can call HID_IsDeviceAffected() for each open HID device to determine if they have been removed.
When exiting the application, the application should be sure to unregister it using HID_UnregisterForDeviceNotification().