Popular Posts

Monday, January 2, 2012

Processing WM_DEVICECHANGE

     Each time a USB device configuration changes, the window which you registered will receive a message on message queue WM_DEVICECHANGE. Where wParam will be filled with the types of events and lParam contains a pointer to a data structure DEV_BROADCAST_HDR. The following code is an example of how to handle this message.


Procedure TUSBDeviceNotifier.WMDeviceChange(var amsg: TMessage);
begin
  case amsg.WParam of
    DBT_DEVICEARRIVAL:
        DoDeviceArrival(PDevBroadcastHdr(amsg.LParam));
    DBT_DEVICEREMOVECOMPLETE:
        DoDeviceRemoveComplete(PDevBroadcastHdr(amsg.LParam));
  end;
end;

        To obtain more specific informationyou can do a typecastDEV_BROADCAST_HDR structure to another structureaccording to type of device type that you monitor. Since we useDBT_DEVTYP_DEVICEINTERFACEthen we can typecast thelParam DEV_BROADCAST_DEVICEINTERFACE.

Register and Unregister Window Handle Process for USB Device

Actually according to MSDN, this WM_DEVICECHANGE sent to all top-level window when there are changes to the device on the PC. However, Windows provides a new mechanism for applications that want to monitor the installation / release of certain types of devices. The mechanism it uses RegisterDeviceNotification () and UnregisterDeviceNotification (). Both functions are available from Windows 2000. 
In the Delphi declaration in Windows.pas unit.
RegisterDeviceNotification function (hRecipient: THandle;NotificationFilter: Pointer;Flags: DWORD):HDEVNOTIFY; stdcall;
UnregisterDeviceNotification function (Handle: HDEVNOTIFY): BOOL;stdcall;
hRecipient is the window handle to send messages WM_DEVICECHANGE. NotificationFilter contains a pointer to a data structure of type DEV_BROADCAST_DEVICEINTERFACE or DEV_BROADCAST_VOLUME. Because we want to monitor is a USB device, DEV_BROADCAST_DEVICEINTERFACE type we use. Fill in the fields of data structures you can see in this code.
Example call to register a USB device notifications.



procedure TUSBDeviceNotifier.InitResources;var _dev:TDevBroadcastDeviceInterface;begin FWndHandle:=AllocateHwnd(WndProc); ZeroMemory(@_dev, sizeOf(TDevBroadcastDeviceInterface)); with _dev do begin dbcc_size:=sizeOf(TDevBroadcastDeviceInterface); dbcc_devicetype:=DBT_DEVTYP_DEVICEINTERFACE; dbcc_reserved:=0; dbcc_classguid:= GUID_DEVINTERFACE_USB_DEVICE; dbcc_name:=0; end; FPtrHandle:=RegisterDeviceNotification(FWndHandle, @_dev, DEVICE_NOTIFY_WINDOW_HANDLE); end;

In the code above we list window to get the relevant notification events related to device configuration changes (DBT_DEVTYP_DEVICEINTERFACE), more specifically the USB device (GUID_DEVINTERFACE_USB_DEVICE). This GUID declaration is as follows:
const
  GUID_DEVINTERFACE_USB_DEVICE:
     TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';

Next we let Windows, the window with the handle stored in FWndHandle like to receive notifications when there are events change the USB device. So that Windows knows that FWndHandle contains the window handle, we are content with the Flags parameter DEVICE_NOTIFY_WINDOW_HANDLE.
If calling RegisterDeviceNotification () successful, this function will return the handle that you can use to remove from the notification list when we do not need it. Pass the handle to the function UnregisterDeviceNotification ().

    Monitoring USB devices

    USB flash disk  that is data storage device that compact, small, easy to read and write but the capacity of thousands of times the size of the floppy disk is 1:44 ". These features led to a USB flash disk is very popular. Popularity of USB flash disk causes the software developer is required to support these devices. Many current software, especially multimedia player software, which includes a feature for synchronizing data with a USB flash disk device. The first step of course is how to detect if a USB device is plugged in or removed.


    This article was written to answer questions about how to detect when a USB flash disk is installed or removed to a computer on the Windows operating system. Detail the question here.


    WM_DEVICECHANGE


    WM_DEVICECHANGE message will be sent to top-level window when there are changes to existing hardware configuration on the computer. Some of the events leading WM_DEVICECHANGE sent, one of which, is the change in the removable storage media such as CD / DVD or USB flash disk.
    Message handlers can use the wParam parameter to specify the type of device change events. Some of them are:

    • DBT_DEVICEARRIVAL. Devices or media recently installed and ready for use.
    • DBT_DEVICEREMOVECOMPLETE. Device or removable media success.
    • DBT_DEVICEREMOVEPENDING. Devices or media success is delayed.
    • DBT_DEVICEQUERYREMOVE. Request permission that a device or removable media want. You can refuse a request to remove the device or media. If your application is accessing data in a file that is located in the flash disk and flash disk does not allow it unplugged, you can cancel the request by returning the value BROADCAST_DENY_REQUEST. If agreed to release the flash disk, the contents of the return value to TRUE.
    • DBT_DEVICEQUERYREMOVEFAILED. Device or removable media fails.

    Technology