zoukankan      html  css  js  c++  java
  • delphi 获取USB口拔出和插入的状态

    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.

  • 相关阅读:
    day84:luffy:优惠活动策略&用户认证&购物车商品的勾选/结算
    day83:luffy:添加购物车&导航栏购物车数字显示&购物车页面展示
    day82:luffy:课程详情页面显示&章节和课时显示&视频播放组件&CKEditor富文本编辑器
    day81:luffy:课程分类页面&课程信息页面&指定分类显示课程信息&分页显示课程信息
    day80:luffy:短信sdk接入&点击获取验证码&注册功能的实现&Celery实现短信发送功能
    day79:luffy:注册之对手机号的验证&实现基本的注册功能逻辑&点击获取验证码&redis
    day78:luffy:前端对于token的认证&滑动验证码的实现
    day77:luffy:导航栏的实现&DjangoRestFramework JWT&多条件登录
    day76:luffy:项目前端环境搭建&轮播图的实现
    day75:luffy:路飞学城项目后端环境搭建&Git相关知识点
  • 原文地址:https://www.cnblogs.com/zhangzhifeng/p/5680966.html
Copyright © 2011-2022 走看看