急!!!如果识别u盘的名称.(100分)

  • 主题发起人 主题发起人 newmaid
  • 开始时间 开始时间
N

newmaid

Unregistered / Unconfirmed
GUEST, unregistred user!
比如,我插入'朗科'的U盘,系统会显示查找到'netac only disk usb device';<br>也可以在'设备管理器'中的'磁盘驱动器'中看到这个u盘的名称.<br>我要的这就是个名称,没是盘符.请给出源代码.<br>新手,请大家帮帮忙,万分感谢.
 
uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, ComCtrls, Registry;<br><br>const<br>&nbsp; GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}';<br>&nbsp; DBT_DEVICEARRIVAL = $8000; // system detected a new device<br>&nbsp; DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone<br>&nbsp; DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface class<br><br>type<br>&nbsp; PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE;<br>&nbsp; DEV_BROADCAST_DEVICEINTERFACE = record<br>&nbsp; &nbsp; dbcc_size: DWORD;<br>&nbsp; &nbsp; dbcc_devicetype: DWORD;<br>&nbsp; &nbsp; dbcc_reserved: DWORD;<br>&nbsp; &nbsp; dbcc_classguid: TGUID;<br>&nbsp; &nbsp; dbcc_name: short;<br>&nbsp; end;<br><br>&nbsp; PDevBroadcastHdr = ^DEV_BROADCAST_HDR;<br>&nbsp; DEV_BROADCAST_HDR = packed record<br>&nbsp; &nbsp; dbch_size: DWORD;<br>&nbsp; &nbsp; dbch_devicetype: DWORD;<br>&nbsp; &nbsp; dbch_reserved: DWORD;<br>&nbsp; end;<br><br>&nbsp; TUSB = class(TObject)<br>&nbsp; private <br>&nbsp; &nbsp; FWindowHandle: HWND; <br>&nbsp; &nbsp; FOnUSBArrival: TNotifyEvent; <br>&nbsp; &nbsp; FOnUSBRemove: TNotifyEvent;<br>&nbsp; &nbsp; ListView: TListView;<br>&nbsp; &nbsp; ADOQuery: TADOQuery;<br>&nbsp; &nbsp; procedure WndProc(var Msg: TMessage); <br>&nbsp; &nbsp; function USBRegister: Boolean;<br>&nbsp; protected<br>&nbsp; &nbsp; procedure WMDeviceChange(var Msg: TMessage); dynamic;<br>&nbsp; &nbsp; function GetUSBStoreDriveName: String;<br>&nbsp; &nbsp; function GetUSBStoreDriveSerial: String;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create(ListView: TListView; ADOQuery: TADOQuery);<br>&nbsp; &nbsp; destructor Destroy; override; <br>&nbsp; &nbsp; property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival; <br>&nbsp; &nbsp; property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;<br>&nbsp; end;<br>&nbsp; <br><br>implementation<br><br>{$R *.dfm}<br><br>{ TUSB }<br><br>constructor TUSB.Create(ListView: TListView; ADOQuery: TADOQuery);<br>begin<br>&nbsp; &nbsp;//Self.ListView := ListView; // 自己决定是否显示在ListView上<br>&nbsp; &nbsp;//Self.ADOQuery := ADOQuery; // 自己决定是否保存到数据库中<br>&nbsp; &nbsp;FWindowHandle := AllocateHWnd(WndProc);<br>&nbsp; &nbsp;USBRegister;<br>end;<br><br>destructor TUSB.Destroy;<br>begin<br>&nbsp; &nbsp;DeallocateHWnd(FWindowHandle);<br>&nbsp; &nbsp;inherited Destroy;<br>end;<br><br>function TUSB.USBRegister: Boolean;<br>var<br>&nbsp; &nbsp;dbi: DEV_BROADCAST_DEVICEINTERFACE;<br>&nbsp; &nbsp;Size: Integer;<br>&nbsp; &nbsp;r: Pointer;<br>begin<br>&nbsp; &nbsp;Result := False;<br>&nbsp; &nbsp;Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE);<br>&nbsp; &nbsp;ZeroMemory(@dbi, Size);<br>&nbsp; &nbsp;dbi.dbcc_size := Size;<br>&nbsp; &nbsp;dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE;<br>&nbsp; &nbsp;dbi.dbcc_reserved := 0;<br>&nbsp; &nbsp;dbi.dbcc_classguid &nbsp;:= GUID_DEVINTERFACE_USB_DEVICE;<br>&nbsp; &nbsp;dbi.dbcc_name := 0;<br>&nbsp; &nbsp;r := RegisterDeviceNotification(FWindowHandle, @dbi, DEVICE_NOTIFY_WINDOW_HANDLE);<br>&nbsp; &nbsp;if Assigned(r) then Result := True;<br>end;<br><br>procedure TUSB.WMDeviceChange(var Msg: TMessage);<br>var<br>&nbsp; &nbsp;devType: Integer;<br>&nbsp; &nbsp;Datos: PDevBroadcastHdr;<br>&nbsp; &nbsp;HostIP, WorkTime, WorkType, WorkName, WorkPvid: String;<br>begin<br>&nbsp; &nbsp;if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; Datos := PDevBroadcastHdr(Msg.lParam);<br>&nbsp; &nbsp; &nbsp; devType := Datos^.dbch_devicetype;<br>&nbsp; &nbsp; &nbsp; if devType = DBT_DEVTYP_DEVICEINTERFACE then<br>&nbsp; &nbsp; &nbsp; begin // USB Device<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;HostIP := IPAddressToString(StringToIPAddress(GetLocalHostIP));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WorkTime := FormatDateTime('yyyy-mm-dd hh:nn', Now);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if Msg.wParam = DBT_DEVICEARRIVAL then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Assigned(FOnUSBArrival) then FOnUSBArrival(Self);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkType := '接入';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkName := GetUSBStoreDriveName;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkPvid := GetUSBStoreDriveSerial;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Assigned(FOnUSBRemove) then FOnUSBRemove(Self);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkType := '移出';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkName := '';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WorkPvid := '';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>{ 如果保存到数据库中用这段代码<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQuery.Active := false;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;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>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ADOQuery.ExecSQL;<br>}<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;end;<br>end;<br><br>procedure TUSB.WndProc(var Msg: TMessage);<br>begin <br>&nbsp; &nbsp;if (Msg.Msg = WM_DEVICECHANGE) then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WMDeviceChange(Msg);<br>&nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Application.HandleException(Self);<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;end else Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);<br>end;<br>// 读移动存储设备名<br>function TUSB.GetUSBStoreDriveName: String;<br>var<br>&nbsp; &nbsp;Reg: TRegistry;<br>&nbsp; &nbsp;KeyItem: WideString;<br>&nbsp; &nbsp;Count: Integer;<br>&nbsp; &nbsp;Value: WideString;<br>begin<br>&nbsp; &nbsp;Result := '';<br>&nbsp; &nbsp;Reg := TRegistry.Create;<br>&nbsp; &nbsp;Reg.Access := KEY_QUERY_VALUE;<br>&nbsp; &nbsp;Reg.RootKey := HKEY_LOCAL_MACHINE;<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; // 取新接入的移动存储设备名键值<br>&nbsp; &nbsp; &nbsp; KeyItem := '/SYSTEM/CurrentControlSet/Services/Disk/Enum';<br>&nbsp; &nbsp; &nbsp; Reg.OpenKey(KeyItem, False);<br>&nbsp; &nbsp; &nbsp; Count := Reg.ReadInteger('Count');<br>&nbsp; &nbsp; &nbsp; Value := Reg.ReadString(IntToStr(Count - 1));<br>&nbsp; &nbsp; &nbsp; // 取新接入的移动存储设备详细值<br>&nbsp; &nbsp; &nbsp; KeyItem := '/SYSTEM/CurrentControlSet/Enum/' + Value;<br>&nbsp; &nbsp; &nbsp; Reg.OpenKey(KeyItem, False);<br>&nbsp; &nbsp; &nbsp; Value := Reg.ReadString('FriendlyName');<br>&nbsp; &nbsp; &nbsp; Result := Value;<br>&nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; Reg.CloseKey;<br>&nbsp; &nbsp; &nbsp; Reg.Free;<br>&nbsp; &nbsp; &nbsp; inherited;<br>&nbsp; &nbsp;end;<br>end;<br><br>// 读移动存储设备序列号<br>function TUSB.GetUSBStoreDriveSerial: String;<br>var<br>&nbsp; &nbsp;Reg: TRegistry;<br>&nbsp; &nbsp;KeyItem: WideString;<br>&nbsp; &nbsp;Count: Integer;<br>&nbsp; &nbsp;Value: WideString;<br>begin<br>&nbsp; &nbsp;Result := '';<br>&nbsp; &nbsp;Reg := TRegistry.Create;<br>&nbsp; &nbsp;Reg.Access := KEY_QUERY_VALUE;<br>&nbsp; &nbsp;Reg.RootKey := HKEY_LOCAL_MACHINE;<br>&nbsp; &nbsp;try<br>&nbsp; &nbsp; &nbsp; // 取新接入的移动存储设备键值<br>&nbsp; &nbsp; &nbsp; KeyItem := '/SYSTEM/CurrentControlSet/Services/usbstor/Enum';<br>&nbsp; &nbsp; &nbsp; if(Reg.OpenKey(KeyItem, False)) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Count := Reg.ReadInteger('Count');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Value := Reg.ReadString(IntToStr(Count - 1));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Result := Value;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp;finally<br>&nbsp; &nbsp; &nbsp; Reg.CloseKey;<br>&nbsp; &nbsp; &nbsp; Reg.Free;<br>&nbsp; &nbsp; &nbsp; inherited;<br>&nbsp; &nbsp;end;<br>end;
 
接受答案了.
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
后退
顶部