unit USBUnit;<br><br>interface<br><br>uses<br> Windows, SysUtils, Classes, Messages, Forms;<br><br>type<br> PDevBroadcastHdr = ^DEV_BROADCAST_HDR;<br> DEV_BROADCAST_HDR = packed record<br> dbch_size: DWORD;<br> dbch_devicetype: DWORD;<br> dbch_reserved: DWORD;<br> end;<br><br> PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;<br> DEV_BROADCAST_DEVICEINTERFACE = record<br> dbcc_size: DWORD;<br> dbcc_devicetype: DWORD;<br> dbcc_reserved: DWORD;<br> dbcc_classguid: TGUID;<br> dbcc_name: short;<br> end;<br><br> TUSB = class(TObject)<br> private<br> FWindowHandle: HWND;<br> FOnUSBArrival: TNotifyEvent;<br> FOnUSBRemove: TNotifyEvent;<br> procedure WndProc(var Msg: TMessage);<br> function USBRegister: Boolean;<br> protected<br> procedure WMDeviceChange(var Msg: TMessage); dynamic;<br> public<br> constructor Create;<br> destructor Destroy; override;<br> property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;<br> property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;<br> end;<br><br>const<br> GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';<br> DBT_DEVICEARRIVAL = $8000; // system detected a new device<br> DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone<br> DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface class<br><br>var<br> USB: TUSB;<br><br>implementation<br><br>{ TUSB }<br><br>constructor TUSB.Create;<br>begin<br> FWindowHandle := AllocateHWnd(WndProc);<br> USBRegister;<br>end;<br><br>destructor TUSB.Destroy;<br>begin<br> DeallocateHWnd(FWindowHandle);<br> inherited Destroy;<br>end;<br><br>function TUSB.USBRegister: Boolean;<br>var<br> dbi: DEV_BROADCAST_DEVICEINTERFACE;<br> Size: Integer;<br> r: Pointer;<br>begin<br> Result := False;<br> Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);<br> ZeroMemory(@dbi, Size);<br> dbi.dbcc_size := Size;<br> dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;<br> dbi.dbcc_reserved := 0;<br> dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE;<br> dbi.dbcc_name := 0;<br> r := RegisterDeviceNotification(FWindowHandle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);<br> if Assigned(r) then<br> Result := True;<br>end;<br><br>procedure TUSB.WMDeviceChange(var Msg: TMessage);<br>var<br> devType: Integer;<br> Datos: PDevBroadcastHdr;<br>begin<br> if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then<br> begin<br> Datos := PDevBroadcastHdr(Msg.lParam);<br> devType := Datos^.dbch_devicetype;<br> if devType = DBT_DEVTYP_DEVICEINTERFACE then<br> begin // USB Device<br> if Msg.wParam = DBT_DEVICEARRIVAL then<br> begin<br> if Assigned(FOnUSBArrival) then<br> FOnUSBArrival(Self);<br> end<br> else<br> begin<br> if Assigned(FOnUSBRemove) then<br> FOnUSBRemove(Self);<br> end;<br> end;<br> end;<br>end;<br><br>procedure TUSB.WndProc(var Msg: TMessage);<br>begin<br> if (Msg.Msg = WM_DEVICECHANGE) then<br> begin<br> try<br> WMDeviceChange(Msg);<br> except<br> Application.HandleException(Self);<br> end;<br> end<br> else<br> Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);<br>end;<br><br>initialization<br> if not Assigned(USB) then<br> USB := TUSB.Create;<br><br>finalization<br> FreeAndNil(USB);<br><br>end.