• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

delphi获取USB口拔出和插入的状态

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

unit USBDeviceNotify;
//USB Device arrival or remove
interface

uses
  Windows, Messages, SysUtils, Classes, Forms;

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   class

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

  TUSBDeviceNotify = class(TComponent)
  private
    FWindowHandle: HWND;
    FOnUSBArrival: TUSBDeviceEvent;
    FOnUSBRemove:  TUSBDeviceEvent;
    procedure WndProc(var Msg: TMessage);
    function USBRegister: Boolean;
  protected
    procedure WMDeviceChange(var Msg: TMessage); dynamic;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property OnUSBArrival: TUSBDeviceEvent read FOnUSBArrival write FOnUSBArrival;
    property OnUSBRemove:  TUSBDeviceEvent read FOnUSBRemove  write FOnUSBRemove;
  end;

implementation

constructor TUSBDeviceNotify.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FWindowHandle := AllocateHWnd(WndProc);
  USBRegister;
end;

destructor TUSBDeviceNotify.Destroy;
begin
  DeallocateHWnd(FWindowHandle);
  inherited Destroy;
end;

procedure TUSBDeviceNotify.WndProc(var Msg: TMessage);
begin
  if (Msg.Msg = WM_DEVICECHANGE) then
  begin
    try
      WMDeviceChange(Msg);
    except
      Application.HandleException(Self);
    end;
  end
  else
    Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.wParam, Msg.lParam);
end;

procedure TUSBDeviceNotify.WMDeviceChange(var Msg: TMessage);
var
  devType: Integer;
  Datos:   PDevBroadcastHdr;
  pData:   PDevBroadcastDeviceInterface;
begin
  if (Msg.wParam = DBT_DEVICEARRIVAL) or (Msg.wParam = DBT_DEVICEREMOVECOMPLETE) then
  begin
    Datos := PDevBroadcastHdr(Msg.lParam);
    devType := Datos^.dbch_devicetype;
    if devType = DBT_DEVTYP_DEVICEINTERFACE then
    begin   //   USB   Device
      pData := PDevBroadcastDeviceInterface(Msg.LParam);
      if Msg.wParam = DBT_DEVICEARRIVAL then
      begin
        if Assigned(FOnUSBArrival) then
          FOnUSBArrival(Self, pData);
      end
      else
      begin
        if Assigned(FOnUSBRemove) then
          FOnUSBRemove(Self, pData);
      end;
    end;
  end;
end;

function TUSBDeviceNotify.USBRegister: Boolean;
var
  dbi:  DEV_BROADCAST_DEVICEINTERFACE;
  Size: Integer;
  r:    Pointer;
begin
  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) then
    Result := True;
end;

end.


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Delphi2009中string与Char的改变发布时间:2022-07-18
下一篇:
Delphi正则表达式起步发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap