检测USB插入拨出[转]

原创 2007年10月08日 08:37:00
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: array [0..255] of char;
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;
fDeviceName:String;
procedure WndProc(var Msg: TMessage);
function USBRegister: Boolean;
protected
procedure WMDeviceChange(var Msg: TMessage); dynamic;
public
property DeviceName:String read FDeviceName;
constructor create(AOwner: TComponent); override;
destructor Destroy; override;
published
property OnUSBArrival: TNotifyEvent read FOnUSBArrival write
FOnUSBArrival;
property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
end;
procedure Register;
implementation

procedure Register; { add this in the implementation section }
begin
RegisterComponents('Samples', [TComponentUSB]);
end;

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 := dbi.dbcc_name;
fDeviceName := String(dbi.dbcc_name);
r := RegisterDeviceNotification(FWindowHandle, @dbi,
DEVICE_NOTIFY_WINDOW_HANDLE
);
if Assigned(r) then Result := True;
end;

end.

在D2006可以生成控件.

D2006例子:

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,U_Usb, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Formcreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
usb:TComponentUSB;
procedure 插入(Sender: TObject);
procedure 拨出(Sender: TObject);

{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.插入(Sender: TObject);
begin
ShowMessage('USB插入');
end;

procedure TForm1.拨出(Sender: TObject);
begin
ShowMessage('USB插入');
end;

procedure TForm1.Formcreate(Sender: TObject);
begin
usb:=TComponentUSB.create(self);
usb.OnUSBArrival := 插入;
usb.OnUSBRemove := 拨出;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeAndNil(usb);
end;

end. 

相关文章推荐

MFC 如何检测到USB设备插入拔出

MFC USB设备插入和拔出事件可以

PyQt4实现USB设备插入到弹出的检测(Windows)

直没怎么看过 QextSerialPort 中windows下枚举串口的具体实现(原因?对win32api不了解,看不懂啊_) 经过一段时间的学习,对QextSerialPort总算理清了一些,于是...
  • swqqcs
  • swqqcs
  • 2012年02月11日 17:53
  • 2541

wap页面点击号码拨出号码 几种方法

手机上网的时候, wml/XHTML 网页上往往有一些连接,点击即可调用手机实现拨号和发短信的功能,比较自然的体现了手机应用的特点。 dig了一下。    Wireless Telephony A...
  • zbz0425
  • zbz0425
  • 2012年09月24日 15:15
  • 1504

LanRoverE Plus拨出配置

  • 2008年01月17日 11:19
  • 2KB
  • 下载

检测USB或SD卡的插入与拔出

检测USB或SD卡的插入与拔出 根据Android文档介绍, 有两种方法检测USB设备插入 第一种, 在Manifest 里面加入 ...>     android:name="android...
  • zyp009
  • zyp009
  • 2013年12月11日 16:42
  • 1794

USB驱动程序代码分析——检测设备插入拔出

我们先来看代码: /*  * drivers\hid\usbhid\usbmouse.c  */ #include #include #include #include #in...

检测USB设备的插入和拔出

检测USB设备的插入和拔出 #include "stdafx.h" #include /* LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARA...
  • greless
  • greless
  • 2017年04月24日 18:33
  • 303

检测USB设备的插入和拔出

1.需求 需要一个DLL,它可以自动检测USB设备插入和拔出,并以回调函数的方法告诉调用DLL的应用程序。   2.预研 2.1参考文档 (1)关于“Registering for Devi...
  • buyicn
  • buyicn
  • 2015年06月21日 23:48
  • 4244
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章: 检测USB插入拨出[转]
举报原因:
原因补充:

(最多只允许输入30个字)