Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 如何检测USB设备何时连接/断开?_Delphi_Usb_Device_Device Driver - Fatal编程技术网

Delphi 如何检测USB设备何时连接/断开?

Delphi 如何检测USB设备何时连接/断开?,delphi,usb,device,device-driver,Delphi,Usb,Device,Device Driver,我觉得用Delphi使用USB设备并不舒服,而且对编写设备驱动程序的细节几乎一无所知(尽管我在学习GoASM汇编时接触过一些) 该设备可以是usb调制解调器或usb打印机 我需要的是一个方向和处理该主题的示例代码。这是从 您可以使用一些shell脚本来执行此操作。 当设备已连接时,命令lsusb将显示此信息,并在设备断开连接后从列表中删除。 因此,在shell脚本中,您需要找到旧列表和当前列表之间的差异。 最后,您需要将此脚本作为一个cron作业运行,该作业以频繁的间隔进行检查 问候,, Bar

我觉得用Delphi使用USB设备并不舒服,而且对编写设备驱动程序的细节几乎一无所知(尽管我在学习GoASM汇编时接触过一些)

该设备可以是usb调制解调器或usb打印机

我需要的是一个方向和处理该主题的示例代码。

这是从


您可以使用一些shell脚本来执行此操作。 当设备已连接时,命令lsusb将显示此信息,并在设备断开连接后从列表中删除。 因此,在shell脚本中,您需要找到旧列表和当前列表之间的差异。 最后,您需要将此脚本作为一个cron作业运行,该作业以频繁的间隔进行检查

问候,,
Barun Parichha

试试这个@PresleyDias:你能把你的评论迁移成anwser并详细说明一下,让我接受吗?@PresleyDias,但你链接中的帖子是关于USB设备连接/断开通知的。我想这个问题是关于“确定USB设备连接了吗?”@TLama:这就是我要求详细说明的原因。我怀疑执行按需
USBRegister
(注册设备验证…)和相应的清洁代码(如适用)就可以了。重构代码的提示是受欢迎的。如果您知道设备的某些特性,您可以使用普通WinAPI枚举USB总线(不需要驱动程序代码),并在连接的设备列表中查找您的设备。我做到了,不幸的是,我的代码太长,无法作为答案发送。@谢谢!这将是一个良好的开端,我现在可以清楚地看到前进的方向。好的,很好,很高兴为您提供帮助欢迎来到stackoverflow。谢谢你的回答。它确实很有价值,但不适用于以下情况:在Windows环境中使用Delphi检测usb。
 unit U_Usb;

 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

 TComponentUSB = class(TComponent)
   private
   FWindowHandle: HWND;
   FOnUSBArrival: TNotifyEvent;
   FOnUSBRemove: TNotifyEvent;
   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: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;
    property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
  end;

  implementation

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

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

 procedure TComponentUSB.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 TComponentUSB.WMDeviceChange(var Msg: TMessage);
   var
   devType: Integer;
     Datos: PDevBroadcastHdr;
   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
     if Msg.wParam = DBT_DEVICEARRIVAL then
     begin
      if Assigned(FOnUSBArrival) then
       FOnUSBArrival(Self);
       end
        else
      begin
     if Assigned(FOnUSBRemove) then
      FOnUSBRemove(Self);
   end;
  end;
   end;
  end;

  function TComponentUSB.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.