古詩詞大全網 - 成語查詢 - delphi usb拔除的是哪個guid

delphi usb拔除的是哪個guid

delphi 獲取USB口拔出和插入的狀態

unit USBDeviceNotify

//USB Device arrival or remove

interface

use

Windows, Messages, SysUtils, Classes, Form

type

PDevBroadcastHdr = ^DEV_BROADCAST_HDR

DEV_BROADCAST_HDR = packed record

dbch_size: DWORD

dbch_devicetype: DWORD

dbch_reserved: DWORD

end

PDevBroadcastDeviceInterface = ^DEV_BROADCAST_DEVICEINTERFACE

DEV_BROADCAST_DEVICEINTERFACE = record

dbcc_size: DWORD

dbcc_devicetype: DWORD

dbcc_reserved: DWORD

dbcc_classguid: TGUID

dbcc_name: short

end

const

GUID_DEVINTERFACE_USB_DEVICE: TGUID = '{A5DCBF10-6530-11D2-901F-00C04FB951ED}'

DBT_DEVICEARRIVAL = $8000; // system detected a new device

DBT_DEVICEREMOVECOMPLETE = $8004; // device is gone

DBT_DEVTYP_DEVICEINTERFACE = $00000005; // device interface cla

type

TUSBDeviceEvent = procedure(Sender: TObject; pDeviceData: PDevBroadcastDeviceInterface) of object

TUSBDeviceNotify = class(TComponent)

rivate

FWindowHandle: HWND

FOnUSBArrival: TUSBDeviceEvent

FOnUSBRemove: TUSBDeviceEvent

rocedure WndProc(var Msg: TMessage)

function USBRegister: Boolea

rotected

rocedure WMDeviceChange(var Msg: TMessage); dynamic

ublic

constructor Create(AOwner: TComponent); override

destructor Destroy; override

ublished

roperty OnUSBArrival: TUSBDeviceEvent read FOnUSBArrival write FOnUSBArrival

roperty OnUSBRemove: TUSBDeviceEvent read FOnUSBRemove write FOnUSBRemove

end

implementatio

constructor TUSBDeviceNotify.Create(AOwner: TComponent)

egi

inherited Create(AOwner)

FWindowHandle := AllocateHWnd(WndProc)

USBRegister

end

destructor TUSBDeviceNotify.Destroy

egi

DeallocateHWnd(FWindowHandle)

inherited Destroy

end

rocedure TUSBDeviceNotify.WndProc(var Msg: TMessage)

egi

if (Msg.Msg = WM_DEVICECHANGE) the

egi

try

WMDeviceChange(Msg)

except

Application.HandleException(Self)

end

end

else

Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam)

end

rocedure TUSBDeviceNotify.WMDeviceChange(var Msg: TMessage)

var

devType: Integer

Datos: PDevBroadcastHdr

Data: PDevBroadcastDeviceInterface

egi

if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) the

egi

Datos := PDevBroadcastHdr(Msg.lParam)

devType := Datos^.dbch_devicetype

if devType = DBT_DEVTYP_DEVICEINTERFACE the

egin // USB Device

Data := PDevBroadcastDeviceInterface(Msg.LParam)

if Msg.wParam = DBT_DEVICEARRIVAL the

egi

if Assigned(FOnUSBArrival) the

FOnUSBArrival(Self, pData)

end

else

egi

if Assigned(FOnUSBRemove) the

FOnUSBRemove(Self, pData)

end

end

end

end

function TUSBDeviceNotify.USBRegister: Boolea

var

dbi: DEV_BROADCAST_DEVICEINTERFACE

Size: Integer

r: Pointer

egi

Result := False

Size := SizeOf(DEV_BROADCAST_DEVICEINTERFACE)

ZeroMemory(@dbi, Size)

dbi.dbcc_size := Size

dbi.dbcc_devicetype := DBT_DEVTYP_DEVICEINTERFACE

dbi.dbcc_reserved := 0

dbi.dbcc_classguid := GUID_DEVINTERFACE_USB_DEVICE

dbi.dbcc_name := 0

r := RegisterDeviceNotification(FWindowHandle, @dbi,

DEVICE_NOTIFY_WINDOW_HANDLE

if Assigned(r) the

Result := True

end

end.