如何系统快速检测当前USB口插入了USB? ( 积分: 100 )

  • 主题发起人 主题发起人 ProLove
  • 开始时间 开始时间
P

ProLove

Unregistered / Unconfirmed
GUEST, unregistred user!
如何系统快速检测当前USB口插入了USB?
 
可以处理WM_DEVICECHANGE消息,具体看看msdn
 
http://www.cnblogs.com/icebird/archive/2005/01/27/98293.html
一个很有用的检测USB设备插入拔出的对象(from Torry's Dephi Pages)
代码:

unit USBUnit;

interface

uses
Windows, SysUtils, Classes, Messages, 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;

TUSB = class(TObject)
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;
destructor Destroy; override;
property OnUSBArrival: TNotifyEvent read FOnUSBArrival write FOnUSBArrival;
property OnUSBRemove: TNotifyEvent read FOnUSBRemove write FOnUSBRemove;
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

var
USB: TUSB;

implementation

{ TUSB }

constructor TUSB.Create;
begin
FWindowHandle := AllocateHWnd(WndProc);
USBRegister;
end;

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

function TUSB.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;

procedure TUSB.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;

procedure TUSB.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;

initialization
if not Assigned(USB) then
USB := TUSB.Create;

finalization
FreeAndNil(USB);

end.
 
首先感谢楼上二位的回答,
其实我的目的是想这样,
系统自动列出当前机器COM口,但是其中某些COM口,是我用USBTORS232线虚拟的一个COM口,
我想知道COM口列表中,哪些COM口是USB口虚拟的?我想表达的是这个意思
 
顺再问下,当U盘插入口后,如果写代码获得他的厂商信息
 
后退
顶部