首页 新闻 赞助 找找看

如何监控某种类型的USB设备的插拔?

0
悬赏园豆:50 [已解决问题] 解决于 2009-01-05 09:17

      目前面临的一个问题是:监控某种类型的USB设备的插拔,不管是新增同类型设备,还是拔掉设备,都给与通知?不知道哪位能给提供一下解决办法?

winzheng的主页 winzheng | 大侠五级 | 园豆:8797
提问于:2008-12-30 09:29
< >
分享
最佳答案
0

这个有难度,C#本身是肯定搞不定,我们可以使用C#封装一个事件,但是这个事件也要由消息队列中得到啊,现在就是不知道USB怎么样才能投递消息,这个可能会用到很多C++,c语言的东西,反正我是搞不定,找一个底层开发的地方去问问!哈

PS:这里有点相似的资料,参考一下,我也研究一下

http://www.cnblogs.com/virusswb/archive/2008/08/22/1274085.html

http://topic.csdn.net/t/20050722/17/4162216.html

这样应该可以,都是利用WM_DEVICECHANGE消息的,里面可能有些你需要的参数

GUO Xingwang | 老鸟四级 |园豆:3885 | 2008-12-30 12:06
其他回答(3)
0

调api

West | 园豆:1095 (小虾三级) | 2008-12-30 10:46
0

学习....

Jared.Nie | 园豆:1940 (小虾三级) | 2008-12-30 16:14
0

用System.Managment class 试试

下面是我在网上找到的代码

出处

http://www.eggheadcafe.com/software/aspnet/31850441/c-usb-pluginremoval-h.aspx

代码如下:

 

// This code demonstrates how to monitor the UsbControllerDevice for
// the arrival of creation/operation events
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Management;
class WMIEvent {
public static void Main() {
WMIEvent we = new WMIEvent();
ManagementEventWatcher w= null;
WqlEventQuery q;
ManagementOperationObserver observer = new ManagementOperationObserver();
// Bind to local machine
ManagementScope scope = new ManagementScope("root\\CIMV2");
scope.Options.EnablePrivileges = true; //set required privilege
try {
q = new WqlEventQuery();
q.EventClassName = "__InstanceOperationEvent";
q.WithinInterval = new TimeSpan(0,0,3);
q.Condition = @"TargetInstance ISA 'Win32_DiskDrive' ";
w = new ManagementEventWatcher(scope, q);

w.EventArrived += new EventArrivedEventHandler(we.DiskEventArrived);
w.Start();
Console.ReadLine(); // block main thread for test purposes
}
catch(Exception e) {
Console.WriteLine(e.Message);
}
finally {
w.Stop();
}
}
public void DiskEventArrived(object sender, EventArrivedEventArgs e) {
//Get the Event object and display its properties (all)
foreach(PropertyData pd in e.NewEvent.Properties) {
ManagementBaseObject mbo = null;
if(( mbo = pd.Value as ManagementBaseObject) != null) {
Console.WriteLine("--------------Properties------------------");
foreach(PropertyData prop in mbo.Properties)
Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
}
}
}


}

下面几篇文章也提供给你参考
MSDN 中介绍如何注册驱动的通知消息
http://msdn.microsoft.com/en-us/library/aa363432.aspx

Detecting USB Stick Plug in and Plug out (USB Harddrive)
http://bytes.com/groups/net-c/234006-detecting-usb-stick-plug-plug-out-usb-harddrive



eaglet | 园豆:17139 (专家六级) | 2009-01-03 05:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册