500分:高难度问题-如何得到USB设备插入或者拔出消息!

ss 2002-10-10 08:10:48
假如以优盘为例,大家看看怎么做!

500分,决不食言!
...全文
121 10 打赏 收藏 举报
写回复
10 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
smhpnuaa 2002-10-10
嗯,ly_liuyang(Liu Yang) 同志确实提供了很好的方案,抄过来研究研究!

How do I detect that a USB device has been plugged in or disconnected?

If you are using version 5.2 or later of WinDriver, you can use the special WinDriver Plug and Play (PnP) and power management API to listen to specific PnP notifications from the OS, including notifications of device insertion/removal, and implement a relevant call-back in your application to handle such situations. Please refer to the WinDriver User's Manual for a detailed description of WinDriver's PnP API.

In earlier versions of WinDriver (until version 5.05) this API is not available.
WD_UsbScanDevice() detects the devices which are connected to the computer at the time of the function call. If another device is later connected to the computer, or a device is disconnected, these changes will not automatically be detected. WD_UsbScanDevice() must be called again in order to detect the changes.
However, since WinDriver USB drivers are user mode WIN32 applications, they can get a notification of insertion/removal. Windows sends all applications a WM_DEVICECHANGE message. Once the application receives this message it should call WD_UsbScanDevice() to check if the device was inserted/removed (this is only good for Windows). You should therefore be able to implement PnP device insertion/removal support with earlier versions of WinDriver as well, using the Win32 API.
You can also send a GET_DESCRIPTOR request to the device (using WD_UsbTransfer() to verify if the device is connected.

Please note that in order to handle the device after it has been disconnected, you must first call WD_UsbDeviceUnregister() to free the previous handle to the device, then re-scan the USB bus to locate the device (using WD_UsbDeviceScan()), get the device's configuration information (by calling WD_UsbGetConfiguration() and re-register the device by calling WD_UsbDeviceRegister().
  • 打赏
  • 举报
回复
ss 2002-10-10
楼上的同志:能详细点吗?钱不是问题,谢谢你,谢谢大家
  • 打赏
  • 举报
回复
cnsuyong 2002-10-10
好象“Kingron(单身走我路……)”提供的代码不管是俺的U盘插入和拔出它都说“有了U盘”耶???
  • 打赏
  • 举报
回复
ly_liuyang 2002-10-10
USB设备插入或者拔出消息可不一定是U盘,还有DC,USB Printer,USB CDRW,USB Mouse/keyboard,USB CableModem等的,只是WM_DEVICECHANGE消息是不够的。

http://www.jungo.com/support/tech_docs/td86.html(方案)
不过这东西要¥的
http://www.jungo.com/dnload.html(下载)
  • 打赏
  • 举报
回复
smhpnuaa 2002-10-10
jedi组织有个hid设备控制组件,可以枚举所有的usb设备,我找到一些代码,供参考。

Declaration

TJvHidDeviceController = class(TComponent)

The main purpose of a TJvHidDeviceController object is to handle TJvHidDevice objects. For each plugged HID device the controller creates a TJvHidDevice object to represent the device. The controller monitors device plugs and unplugs. On device plug a new TJvHidDevice object is created. On unplug the object is not destroyed but is signalled and goes to an unplugged state.
The methods of TJvHidDeviceController are mainly for handing out TJvHidDevice objects by various criteria.
There is no need to instantiate more than one TJVHidDeviceController per program.

TJvHidDevice = class(TObject)

A TJvHidDevice object represents a physical HID device. All static informations of the device have been read into properties of the object. The object is created at runtime by a TJvHidDeviceController which reigns all TJVHidDevice object.

The HID component gives you complete access to all HID devices of Windows 98, Windows 98 SE and Windows 2000. A HID device is a USB device which you can interact with. Most of the USB devices are HID. Keyboard, mice or scanners are definitely HID. A USB Hub is not HID. There is no need to touch it to make it work. Some non USB devices are added to HID by a legacy driver.
The main feature of USB is the hotplugging of the devices. Consequently the HID component is a controller component which handles all the HID device plugs and unplugs. You will therefore only need a single instance of the HID component in your program. Each individual HID device is represented by an instance of a HID device object. The HID controller holds a list of all HID device objects. When a HID device is plugged Windows sends a WM_DEVICECHANGE event. The HID component catches this event and adds a new instance of a HID device object to its list of HID devices.

Now you can ask the HID component to hand out one of its HID device objects. With this HID device object you can then access the individual device. When you are finished with the device hand back the HID device object to the HID component.
  • 打赏
  • 举报
回复
zmcant 2002-10-10
upup
  • 打赏
  • 举报
回复
cnsuyong 2002-10-10
DRV_LOAD 和 DRV_REMOVE

你可以参阅Delphi附带的:
Win32 SDK
Multimedia Programmer's Reference
Installable Drivers
那里讲得非常详尽了。
GOOD LUCK!

  • 打赏
  • 举报
回复
Kingron 2002-10-10
private
{ Private declarations }
procedure wmdevicechange(var msg:TMessage);message WM_DEVICECHANGE;


procedure TForm1.wmdevicechange(var msg: TMessage);
begin
if (Msg.WParam=7) and (Msg.LPARAM=0) then
ShowMessage('有了优盘!');
end;
  • 打赏
  • 举报
回复
smhpnuaa 2002-10-10
好像系统自己能智能识别的,不然怎么叫“即插即用”,呵呵,猜测
  • 打赏
  • 举报
回复
blazingfire 2002-10-10
哈哈,这问题和检测机器装没有装光驱一样费神!
  • 打赏
  • 举报
回复
相关推荐
发帖
Delphi
加入

5091

社区成员

Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
申请成为版主
帖子事件
创建了帖子
2002-10-10 08:10
社区公告
暂无公告