Определить — подключено ли USB устройство ?
Определить — подключено ли USB устройство ?
Определить - подключено ли USB устройство ? autor: miguel lucero homepage: www.lsis.com.ar // component to detect when usb devices are connected or disconnected // using registerdevicenotification unit u_usb; interface uses windows, messages, sysutils, classes, forms; type pdevbroadcasthdr = ^dev_broadcast_hdr; dev_broadcast_hdr = packed record dbch_size: dword; dbch_devicetype: dword; dbch_reserved: dword; end; pdevbroadcastdeviceinterface = ^dev_broadcast_deviceinterface; dev_broadcast_deviceinterface = record dbcc_size: dword; dbcc_devicetype: dword; dbcc_reserved: dword; dbcc_classguid: tguid; dbcc_name: short; end; const guid_devinterface_usb_device: tguid = '{a5dcbf10-6530-11d2-901f-00c04fb951ed}'; dbt_devicearrival = $8000; // system detected a new device dbt_deviceremovecomplete = $8004; // device is gone dbt_devtyp_deviceinterface = $00000005; // device interface class type tcomponentusb = class(tcomponent) private fwindowhandle: hwnd; fonusbarrival: tnotifyevent; fonusbremove: tnotifyevent; procedure wndproc(var msg: tmessage); function usbregister: boolean; protected procedure wmdevicechange(var msg: tmessage); dynamic; public constructor create(aowner: tcomponent); override; destructor destroy; override; published property onusbarrival: tnotifyevent read fonusbarrival write fonusbarrival; property onusbremove: tnotifyevent read fonusbremove write fonusbremove; end; implementation constructor tcomponentusb.create(aowner: tcomponent); begin inherited create(aowner); fwindowhandle := allocatehwnd(wndproc); usbregister; end; destructor tcomponentusb.destroy; begin deallocatehwnd(fwindowhandle); inherited destroy; end; procedure tcomponentusb.wndproc(var msg: tmessage); begin if (msg.msg = wm_devicechange) then begin try wmdevicechange(msg); except application.handleexception(self); end; end else msg.result := defwindowproc(fwindowhandle, msg.msg, msg.wparam, msg.lparam); end; procedure tcomponentusb.wmdevicechange(var msg: tmessage); var devtype: integer; datos: pdevbroadcasthdr; begin if (msg.wparam = dbt_devicearrival) or (msg.wparam = dbt_deviceremovecomplete) then begin datos := pdevbroadcasthdr(msg.lparam); devtype := datos^.dbch_devicetype; if devtype = dbt_devtyp_deviceinterface then begin // usb device if msg.wparam = dbt_devicearrival then begin if assigned(fonusbarrival) then fonusbarrival(self); end else begin if assigned(fonusbremove) then fonusbremove(self); end; function tcomponentusb.usbregister: boolean; var dbi: dev_broadcast_deviceinterface; size: integer; r: pointer; begin result := false; size := sizeof(dev_broadcast_deviceinterface); zeromemory(@dbi, size); dbi.dbcc_size := size; dbi.dbcc_devicetype := dbt_devtyp_deviceinterface; dbi.dbcc_reserved := 0; dbi.dbcc_classguid := guid_devinterface_usb_device; dbi.dbcc_name := 0; r := registerdevicenotification(fwindowhandle, @dbi, device_notify_window_handle ); if assigned(r) then result := true; end; end.