如何监控USB呢 怎么知道谁把USB插拔上去呢 ( 积分: 100 )

  • 主题发起人 主题发起人 goddy
  • 开始时间 开始时间
G

goddy

Unregistered / Unconfirmed
GUEST, unregistred user!
如何监控USB呢 怎么知道谁把USB插拔上去呢
 
Delphibox上有USB控件
 
楼上的,是什么控件,话不能说一半啊
 
unit&nbsp;USBUnit;<br><br>interface<br><br>uses<br>&nbsp;&nbsp;Windows,&nbsp;SysUtils,&nbsp;Classes,&nbsp;Messages,&nbsp;Forms;<br><br>type<br>&nbsp;&nbsp;PDevBroadcastHdr&nbsp;=&nbsp;^DEV_BROADCAST_HDR;<br>&nbsp;&nbsp;DEV_BROADCAST_HDR&nbsp;=&nbsp;packed&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;dbch_size:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;dbch_devicetype:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;dbch_reserved:&nbsp;DWORD;<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;PDevBroadcastDeviceInterface&nbsp;=&nbsp;^DEV_BROADCAST_DEVICEINTERFACE;<br>&nbsp;&nbsp;DEV_BROADCAST_DEVICEINTERFACE&nbsp;=&nbsp;record<br>&nbsp;&nbsp;&nbsp;&nbsp;dbcc_size:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;dbcc_devicetype:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;dbcc_reserved:&nbsp;DWORD;<br>&nbsp;&nbsp;&nbsp;&nbsp;dbcc_classguid:&nbsp;TGUID;<br>&nbsp;&nbsp;&nbsp;&nbsp;dbcc_name:&nbsp;short;<br>&nbsp;&nbsp;end;<br><br>&nbsp;&nbsp;TUSB&nbsp;=&nbsp;class(TObject)<br>&nbsp;&nbsp;private<br>&nbsp;&nbsp;&nbsp;&nbsp;FWindowHandle:&nbsp;HWND;<br>&nbsp;&nbsp;&nbsp;&nbsp;FOnUSBArrival:&nbsp;TNotifyEvent;<br>&nbsp;&nbsp;&nbsp;&nbsp;FOnUSBRemove:&nbsp;TNotifyEvent;<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;WndProc(var&nbsp;Msg:&nbsp;TMessage);<br>&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;USBRegister:&nbsp;Boolean;<br>&nbsp;&nbsp;protected<br>&nbsp;&nbsp;&nbsp;&nbsp;procedure&nbsp;WMDeviceChange(var&nbsp;Msg:&nbsp;TMessage);&nbsp;dynamic;<br>&nbsp;&nbsp;public<br>&nbsp;&nbsp;&nbsp;&nbsp;constructor&nbsp;Create;<br>&nbsp;&nbsp;&nbsp;&nbsp;destructor&nbsp;Destroy;&nbsp;override;<br>&nbsp;&nbsp;&nbsp;&nbsp;property&nbsp;OnUSBArrival:&nbsp;TNotifyEvent&nbsp;read&nbsp;FOnUSBArrival&nbsp;write&nbsp;FOnUSBArrival;<br>&nbsp;&nbsp;&nbsp;&nbsp;property&nbsp;OnUSBRemove:&nbsp;TNotifyEvent&nbsp;read&nbsp;FOnUSBRemove&nbsp;write&nbsp;FOnUSBRemove;<br>&nbsp;&nbsp;end;<br><br>const<br>&nbsp;&nbsp;GUID_DEVINTERFACE_USB_DEVICE:&nbsp;TGUID&nbsp;=&nbsp;'{A5DCBF10-6530-11D2-901F-00C04FB951ED}';<br>&nbsp;&nbsp;DBT_DEVICEARRIVAL&nbsp;=&nbsp;$8000;&nbsp;//&nbsp;system&nbsp;detected&nbsp;a&nbsp;new&nbsp;device<br>&nbsp;&nbsp;DBT_DEVICEREMOVECOMPLETE&nbsp;=&nbsp;$8004;&nbsp;//&nbsp;device&nbsp;is&nbsp;gone<br>&nbsp;&nbsp;DBT_DEVTYP_DEVICEINTERFACE&nbsp;=&nbsp;$00000005;&nbsp;//&nbsp;device&nbsp;interface&nbsp;class<br><br>var<br>&nbsp;&nbsp;USB:&nbsp;TUSB;<br><br>implementation<br><br>{&nbsp;TUSB&nbsp;}<br><br>constructor&nbsp;TUSB.Create;<br>begin<br>&nbsp;&nbsp;FWindowHandle&nbsp;:=&nbsp;AllocateHWnd(WndProc);<br>&nbsp;&nbsp;USBRegister;<br>end;<br><br>destructor&nbsp;TUSB.Destroy;<br>begin<br>&nbsp;&nbsp;DeallocateHWnd(FWindowHandle);<br>&nbsp;&nbsp;inherited&nbsp;Destroy;<br>end;<br><br>function&nbsp;TUSB.USBRegister:&nbsp;Boolean;<br>var<br>&nbsp;&nbsp;dbi:&nbsp;DEV_BROADCAST_DEVICEINTERFACE;<br>&nbsp;&nbsp;Size:&nbsp;Integer;<br>&nbsp;&nbsp;r:&nbsp;Pointer;<br>begin<br>&nbsp;&nbsp;Result&nbsp;:=&nbsp;False;<br>&nbsp;&nbsp;Size&nbsp;:=&nbsp;SizeOf(DEV_BROADCAST_DEVICEINTERFACE);<br>&nbsp;&nbsp;ZeroMemory(@dbi,&nbsp;Size);<br>&nbsp;&nbsp;dbi.dbcc_size&nbsp;:=&nbsp;Size;<br>&nbsp;&nbsp;dbi.dbcc_devicetype&nbsp;:=&nbsp;DBT_DEVTYP_DEVICEINTERFACE;<br>&nbsp;&nbsp;dbi.dbcc_reserved&nbsp;:=&nbsp;0;<br>&nbsp;&nbsp;dbi.dbcc_classguid&nbsp;&nbsp;:=&nbsp;GUID_DEVINTERFACE_USB_DEVICE;<br>&nbsp;&nbsp;dbi.dbcc_name&nbsp;:=&nbsp;0;<br>&nbsp;&nbsp;r&nbsp;:=&nbsp;RegisterDeviceNotification(FWindowHandle,&nbsp;@dbi,&nbsp;DEVICE_NOTIFY_WINDOW_HANDLE);<br>&nbsp;&nbsp;if&nbsp;Assigned(r)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;Result&nbsp;:=&nbsp;True;<br>end;<br><br>procedure&nbsp;TUSB.WMDeviceChange(var&nbsp;Msg:&nbsp;TMessage);<br>var<br>&nbsp;&nbsp;devType:&nbsp;Integer;<br>&nbsp;&nbsp;Datos:&nbsp;PDevBroadcastHdr;<br>begin<br>&nbsp;&nbsp;if&nbsp;(Msg.wParam&nbsp;=&nbsp;DBT_DEVICEARRIVAL)&nbsp;or&nbsp;(Msg.wParam&nbsp;=&nbsp;DBT_DEVICEREMOVECOMPLETE)&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;Datos&nbsp;:=&nbsp;PDevBroadcastHdr(Msg.lParam);<br>&nbsp;&nbsp;&nbsp;&nbsp;devType&nbsp;:=&nbsp;Datos^.dbch_devicetype;<br>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;devType&nbsp;=&nbsp;DBT_DEVTYP_DEVICEINTERFACE&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;begin&nbsp;//&nbsp;USB&nbsp;Device<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Msg.wParam&nbsp;=&nbsp;DBT_DEVICEARRIVAL&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Assigned(FOnUSBArrival)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FOnUSBArrival(Self);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;Assigned(FOnUSBRemove)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FOnUSBRemove(Self);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end;<br>end;<br><br>procedure&nbsp;TUSB.WndProc(var&nbsp;Msg:&nbsp;TMessage);<br>begin<br>&nbsp;&nbsp;if&nbsp;(Msg.Msg&nbsp;=&nbsp;WM_DEVICECHANGE)&nbsp;then<br>&nbsp;&nbsp;begin<br>&nbsp;&nbsp;&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WMDeviceChange(Msg);<br>&nbsp;&nbsp;&nbsp;&nbsp;except<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Application.HandleException(Self);<br>&nbsp;&nbsp;&nbsp;&nbsp;end;<br>&nbsp;&nbsp;end<br>&nbsp;&nbsp;else<br>&nbsp;&nbsp;&nbsp;&nbsp;Msg.Result&nbsp;:=&nbsp;DefWindowProc(FWindowHandle,&nbsp;Msg.Msg,&nbsp;Msg.wParam,&nbsp;Msg.lParam);<br>end;<br><br>initialization<br>&nbsp;&nbsp;if&nbsp;not&nbsp;Assigned(USB)&nbsp;then<br>&nbsp;&nbsp;&nbsp;&nbsp;USB&nbsp;:=&nbsp;TUSB.Create;<br><br>finalization<br>&nbsp;&nbsp;FreeAndNil(USB);<br><br>end.
 
接受答案了.
 
后退
顶部