uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, ComCtrls, Registry;<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>type<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> 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> TUSB = class(TObject)<br> private <br> FWindowHandle: HWND; <br> FOnUSBArrival: TNotifyEvent; <br> FOnUSBRemove: TNotifyEvent;<br> ListView: TListView;<br> ADOQuery: TADOQuery;<br> procedure WndProc(var Msg: TMessage); <br> function USBRegister: Boolean;<br> protected<br> procedure WMDeviceChange(var Msg: TMessage); dynamic;<br> function GetUSBStoreDriveName: String;<br> function GetUSBStoreDriveSerial: String;<br> public<br> constructor Create(ListView: TListView; ADOQuery: TADOQuery);<br> destructor Destroy; override; <br> property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival; <br> property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;<br> end;<br> <br><br>implementation<br><br>{$R *.dfm}<br><br>{ TUSB }<br><br>constructor TUSB.Create(ListView: TListView; ADOQuery: TADOQuery);<br>begin<br> //Self.ListView := ListView; // 自己决定是否显示在ListView上<br> //Self.ADOQuery := ADOQuery; // 自己决定是否保存到数据库中<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 Result := True;<br>end;<br><br>procedure TUSB.WMDeviceChange(var Msg: TMessage);<br>var<br> devType: Integer;<br> Datos: PDevBroadcastHdr;<br> HostIP, WorkTime, WorkType, WorkName, WorkPvid: String;<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> HostIP := IPAddressToString(StringToIPAddress(GetLocalHostIP));<br> WorkTime := FormatDateTime('yyyy-mm-dd hh:nn', Now);<br><br> if Msg.wParam = DBT_DEVICEARRIVAL then<br> begin<br> if Assigned(FOnUSBArrival) then FOnUSBArrival(Self);<br> WorkType := '接入';<br> WorkName := GetUSBStoreDriveName;<br> WorkPvid := GetUSBStoreDriveSerial;<br> end<br> else<br> begin<br> if Assigned(FOnUSBRemove) then FOnUSBRemove(Self);<br> WorkType := '移出';<br> WorkName := '';<br> WorkPvid := '';<br> end;<br>{ 如果保存到数据库中用这段代码<br> ADOQuery.Active := false;<br> ADOQuery.SQL.Text := 'INSERT INTO InspectUsbscout (WorkTime, WorkType, WorkName, WorkPvid) VALUES(' + Chr(39) + WorkTime + Chr(39) + ',' + Chr(39) + WorkType + Chr(39) + ',' + Chr(39) + WorkName + Chr(39) + ',' + Chr(39) + WorkPvid + Chr(39) + ')';<br> ADOQuery.ExecSQL;<br>}<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 else Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);<br>end;<br>// 读移动存储设备名<br>function TUSB.GetUSBStoreDriveName: String;<br>var<br> Reg: TRegistry;<br> KeyItem: WideString;<br> Count: Integer;<br> Value: WideString;<br>begin<br> Result := '';<br> Reg := TRegistry.Create;<br> Reg.Access := KEY_QUERY_VALUE;<br> Reg.RootKey := HKEY_LOCAL_MACHINE;<br> try<br> // 取新接入的移动存储设备名键值<br> KeyItem := '/SYSTEM/CurrentControlSet/Services/Disk/Enum';<br> Reg.OpenKey(KeyItem, False);<br> Count := Reg.ReadInteger('Count');<br> Value := Reg.ReadString(IntToStr(Count - 1));<br> // 取新接入的移动存储设备详细值<br> KeyItem := '/SYSTEM/CurrentControlSet/Enum/' + Value;<br> Reg.OpenKey(KeyItem, False);<br> Value := Reg.ReadString('FriendlyName');<br> Result := Value;<br> finally<br> Reg.CloseKey;<br> Reg.Free;<br> inherited;<br> end;<br>end;<br><br>// 读移动存储设备序列号<br>function TUSB.GetUSBStoreDriveSerial: String;<br>var<br> Reg: TRegistry;<br> KeyItem: WideString;<br> Count: Integer;<br> Value: WideString;<br>begin<br> Result := '';<br> Reg := TRegistry.Create;<br> Reg.Access := KEY_QUERY_VALUE;<br> Reg.RootKey := HKEY_LOCAL_MACHINE;<br> try<br> // 取新接入的移动存储设备键值<br> KeyItem := '/SYSTEM/CurrentControlSet/Services/usbstor/Enum';<br> if(Reg.OpenKey(KeyItem, False)) then<br> begin<br> Count := Reg.ReadInteger('Count');<br> Value := Reg.ReadString(IntToStr(Count - 1));<br> Result := Value;<br> end;<br> finally<br> Reg.CloseKey;<br> Reg.Free;<br> inherited;<br> end;<br>end;