IntroductionIn this article I will describe how to detect device changes in your user-mode applications on the Windows operating system, IOW how to detect the situation when new devices are plugged or removed from the PC (i.e. plug-in USB stick, modem device or mobile phone, mount/unmount the new disk). Basic approach is receiving notification messages from the system. Hardware change detectingWindows sends events to top-level windows about arriving and removing a USB device interface. All we need to do is to add a handler to handle this event. First of all, we should register our windows to receive device notifications by calling RegisterDeviceNotification function. This function is defined as follows:HDEVNOTIFY WINAPI RegisterDeviceNotification(
__in HANDLE hRecipient,
__in LPVOID NotificationFilter,
__in DWORD Flags
);For the NotificationFilter parameter we will use constant DBT_DEVTYP_DEVICEINTERFACE: GUID guidForModemDevices = {0x2c7089aa, 0x2e0e, 0x11d1, {0xb1, 0x14, 0x00, 0xc0, 0x4f, 0xc2, 0xaa, 0xe4}};
DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
ZeroMemory( ¬ificationFilter, sizeof(notificationFilter) );
notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
notificationFilter.dbcc_classguid = guid;Window creating
The hRecipient parameter in described above RegisterDeviceNotification function is a window handle or a service status handle. In our sample we will use window handle.Read more: Codeproject
__in HANDLE hRecipient,
__in LPVOID NotificationFilter,
__in DWORD Flags
);For the NotificationFilter parameter we will use constant DBT_DEVTYP_DEVICEINTERFACE: GUID guidForModemDevices = {0x2c7089aa, 0x2e0e, 0x11d1, {0xb1, 0x14, 0x00, 0xc0, 0x4f, 0xc2, 0xaa, 0xe4}};
DEV_BROADCAST_DEVICEINTERFACE notificationFilter;
ZeroMemory( ¬ificationFilter, sizeof(notificationFilter) );
notificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
notificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
notificationFilter.dbcc_classguid = guid;Window creating
The hRecipient parameter in described above RegisterDeviceNotification function is a window handle or a service status handle. In our sample we will use window handle.Read more: Codeproject
0 comments:
Post a Comment