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.

delphi 获取USB口拔出和插入的状态的更多相关文章

  1. Delphi在Android下实现BroadcastReceiver功能(举例在Delphi下获取USB外设拔插消息)

    在Android里,用java通过实现BroadcastReceiver接口,就可以获得Intent消息.可是Delphi程序不能直接实现JBroadcastReceiver,如何能够实现类似Java ...

  2. iOS 检测耳机插入/拔出

    http://www.verydemo.com/demo_c134_i28481.html 开发过程中录音和播放这块碰到了一些问题,麻烦的主要有三个: 检测是否有声音输入设备 当有多个声音输出设备时, ...

  3. Delphi 自动检测U盘插入、拔出及获取U盘盘符!

    http://qqhack8.blog.163.com/blog/static/1141479852012102133475/     Delphi 自动检测U盘插入.拔出及获取U盘盘符! u盘的 插 ...

  4. 广播监听USB插入与拔出

    package com.joy.usbbroadcastreceiver; import android.content.BroadcastReceiver; import android.conte ...

  5. c# 获取移动硬盘信息、监听移动设备的弹出与插入事件

    原文 http://www.cnblogs.com/coolkiss/p/3328825.html 备忘一下改功能,主要通过WMI来实现,对于监听外接设备的弹出和插入事件一开始使用IntPtr Wnd ...

  6. IOS 判断耳机插入/拔出

    一. 方式 1.注册监听 //注册监听耳机设备的插入/拔出 AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChang ...

  7. iOS检测耳机插入拔出

    首先,需要导入两个框架 然后,注册通知检测耳机的插入与拔出操作 [[NSNotificationCenter defaultCenter] addObserver:self selector:@sel ...

  8. C# 上位机的USB设备拔插检测

    我们做USB通信时,通信成功后,往往要检测USB设备的拔插状态,这里就USB拔插进行一下说明. 参考:https://www.imooc.com/article/17438 先说明一下,我这里只是用C ...

  9. usb口外接了Com设备,U盘识别不了问题

    就如本题,当我usb口外接了Com设备时候,再插入U盘会出现识别不了的问题. 解决方法非常的简单,只要拨出这个com设备的usb就可以使用U盘了^_^

随机推荐

  1. Swift中的类型属性(静态变量)

    http://blog.haohtml.com/archives/15098 Swift中的类型属性(静态变量) Posted on 2014/06/13 类型属性语法 在 C 或 Objective ...

  2. (接口自动化)Python3操作MySQL数据库

    基础语法: import pymysql #导入模块 conn = pymysql.connect(host='localhost',user='root', passwd='123456', db= ...

  3. mapper配置文件中的动态SQL

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  4. Linux下f命令配置

    一.工具 f 的配置 使用 ========== ========== ========== ========== ========== ==========  ==== 一.配置方法: 首先在lin ...

  5. node中通过orm2链接mysql的一个坑

    代码是orm上的例子,出现如下错误: ORMError: Connection protocol not supported - have you installed the database dri ...

  6. Scanner类的个人分析

    Scanner类读取键盘输入(java中Scanner类nextLine()和next()的区别和使用方法&&java 中的Scanner(非常详细不看后悔)): 2017/3/18 ...

  7. ThinkPHP的静态化页面方法

    原来ThinkPHP自带了生成静态页的函数buildHtml,使用起来很方便!最新的手册里没写这个方法,向大家介绍一下.     protected function buildHtml($htmlf ...

  8. sublime text3中使用Emmet部分标签无法闭合

    转载自:http://geek100.com/2490/ 不过很早就发现br,input, img在sublime text中是没有闭合标签 / 的. 我一般都是手动补上的, 今天突然想起这个问题, ...

  9. radio和label关联问题,点击label改变颜色

    $(function () { $("#fangan :radio[name='price']").bind('click', function (event) { //$(thi ...

  10. bisect二分查找模块使用

    import bisectL = [1, 3, 3, 6, 8, 12, 15]x = 5x_insert_point = bisect.bisect_left(L, x)# 在L中查找x,x存在时返 ...