Detect USB Device Insert and Removal
2013-05-31 23:12
435 查看
http://www.delphibasics.info/home/delphibasicssnippets/detectusbdeviceinsertandremoval-uusbbytestest
unit uUsb; //Author: testest interface uses Windows, Messages, Classes; type PDevBroadcastHdr = ^DEV_BROADCAST_HDR; DEV_BROADCAST_HDR = packed record dbch_size: DWORD; dbch_devicetype: DWORD; dbch_reserved: DWORD; end; TDevBroadcastHdr = DEV_BROADCAST_HDR; type PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE; DEV_BROADCAST_DEVICEINTERFACE = record dbcc_size: DWORD; dbcc_devicetype: DWORD; dbcc_reserved: DWORD; dbcc_classguid: TGUID; dbcc_name: Char; end; TDevBroadcastDeviceInterface = DEV_BROADCAST_DEVICEINTERFACE; const GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}'; DBT_DEVICEARRIVAL = $8000; DBT_DEVICEREMOVECOMPLETE = $8004; DBT_DEVTYP_DEVICEINTERFACE = $00000005; type TUsbNotifyProc = procedure(Sender: TObject; const DeviceName: String) of Object; TUsbNotifier = class private FWindowHandle: HWND; FNotificationHandle: Pointer; FOnUsbArrival: TUsbNotifyProc; FOnUsbRemoval: TUsbNotifyProc; procedure WndProc(var Msg: TMessage); public constructor Create; property OnUsbArrival: TUsbNotifyProc read FOnUsbArrival write FOnUsbArrival; property OnUsbRemoval: TUsbNotifyProc read FOnUsbRemoval write FOnUsbRemoval; destructor Destroy; override; end; implementation constructor TUsbNotifier.Create; var Size: Cardinal; Dbi: TDevBroadcastDeviceInterface; begin inherited; FWindowHandle := AllocateHWnd(WndProc); Size := SizeOf(Dbi); ZeroMemory(@Dbi, Size); Dbi.dbcc_size := Size; Dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE; Dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE; FNotificationHandle := RegisterDeviceNotification(FWindowHandle, @Dbi, DEVICE_NOTIFY_WINDOW_HANDLE); end; procedure TUsbNotifier.WndProc(var Msg: TMessage); var Dbi: PDevBroadcastDeviceInterface; begin with Msg do if (Msg = WM_DEVICECHANGE) and ((WParam = DBT_DEVICEARRIVAL) or (WParam = DBT_DEVICEREMOVECOMPLETE)) then try Dbi := PDevBroadcastDeviceInterface(LParam); if Dbi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE then begin if WParam = DBT_DEVICEARRIVAL then begin if Assigned(FOnUsbArrival) then FOnUsbArrival(Self, PChar(@Dbi.dbcc_name)); end else begin if Assigned(FOnUsbRemoval) then FOnUsbRemoval(Self, PChar(@Dbi.dbcc_name)); end; end; except Result := DefWindowProc(FWindowHandle, Msg, WParam, LParam); end else Result := DefWindowProc(FWindowHandle, Msg, WParam, LParam); end; destructor TUsbNotifier.Destroy; begin UnregisterDeviceNotification(FNotificationHandle); DeallocateHWnd(FWindowHandle); inherited; end; end.
相关文章推荐
- Mac OS X 启动时出现 “No bootable device — insert boot disk and press any key”
- reboot and select proper boot derice or insert boot media in selected boot device and press a key
- HP笔记本 no bootable device insert boot disk and press any key
- What is a Windows USB device path and how is it formatted?
- 重写mbr后 no bootable device insert boot disk and press any key
- under ubuntu, using usb and adb connect device
- Insert Boot Media inselected Boot device and press a key解决
- reboot and select proper boot device r Insert boot Media in selected boot device and press a key解决方法
- os X 的恢复(No bootable device-insert boot disk and press any key)
- 华硕笔记本 reboot and select proper boot device or Insert boot media in select boot device
- 关于“reset high speed USB device using ehci_hcd and address 2”
- check cable connection PXE-M0F: Exiting intel PXE ROM no bootable device-- insert boot disk and pre
- sysfs and /proc/bus/usb/device
- 电脑开机 No bootable device-insert boot disk and press any key
- Insert boot media in selected boot device and press a key
- sysfs and /proc/bus/usb/device
- 开机的时候显示Reboot and select proper boot device or Insert Boot Media in selected Boot
- sysfs and /proc/bus/usb/device
- 电脑启动不起来,英文提示Reboot and select proper boot device or Insert Boot Media in selected Boot