WMI 和 Win32_DeviceChangeEvent - 返回了错误的事件类型?

发布于 2024-07-04 05:55:40 字数 1549 浏览 4 评论 0 原文

我正在尝试使用 WMI 注册“设备添加/设备删除”事件。 当我说设备时 - 我的意思是 Disk-On-Key 或任何其他带有我可以访问的文件的设备中的某些东西...

我正在注册该事件,并且引发该事件,但是 EventType财产与我期望看到的不同。

文档(MSDN)指出:1-配置更改,2- 添加设备,3- 删除设备 4- 对接。 由于某种原因,我总是得到 1 的值。

有什么想法吗?

这是示例代码:

public class WMIReceiveEvent
{
    public WMIReceiveEvent()
    {
        try
        {
            WqlEventQuery query = new WqlEventQuery(
                "SELECT * FROM Win32_DeviceChangeEvent");

            ManagementEventWatcher watcher = new ManagementEventWatcher(query);
            Console.WriteLine("Waiting for an event...");

            watcher.EventArrived += 
                new EventArrivedEventHandler(
                HandleEvent);

            // Start listening for events
            watcher.Start();

            // Do something while waiting for events
            System.Threading.Thread.Sleep(10000);

            // Stop listening for events
            watcher.Stop();
            return;
        }
        catch(ManagementException err)
        {
            MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
        }
    }

    private void HandleEvent(object sender,
        EventArrivedEventArgs e)
    {
        Console.WriteLine(e.NewEvent.GetPropertyValue["EventType"]);
    }

    public static void Main()
    {
        WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
        return;
    }
}

I am trying to register to a "Device added/ Device removed" event using WMI. When I say device - I mean something in the lines of a Disk-On-Key or any other device that has files on it which I can access...

I am registering to the event, and the event is raised, but the EventType propery is different from the one I am expecting to see.

The documentation (MSDN) states : 1- config change, 2- Device added, 3-Device removed 4- Docking. For some reason I always get a value of 1.

Any ideas ?

Here's sample code :

public class WMIReceiveEvent
{
    public WMIReceiveEvent()
    {
        try
        {
            WqlEventQuery query = new WqlEventQuery(
                "SELECT * FROM Win32_DeviceChangeEvent");

            ManagementEventWatcher watcher = new ManagementEventWatcher(query);
            Console.WriteLine("Waiting for an event...");

            watcher.EventArrived += 
                new EventArrivedEventHandler(
                HandleEvent);

            // Start listening for events
            watcher.Start();

            // Do something while waiting for events
            System.Threading.Thread.Sleep(10000);

            // Stop listening for events
            watcher.Stop();
            return;
        }
        catch(ManagementException err)
        {
            MessageBox.Show("An error occurred while trying to receive an event: " + err.Message);
        }
    }

    private void HandleEvent(object sender,
        EventArrivedEventArgs e)
    {
        Console.WriteLine(e.NewEvent.GetPropertyValue["EventType"]);
    }

    public static void Main()
    {
        WMIReceiveEvent receiveEvent = new WMIReceiveEvent();
        return;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦在深巷 2024-07-11 05:55:40

哦! 是的,我已经经历过这种情况,但前段时间使用原始 Windows API 调用,同时开发了一个检测任何类型媒体插入的 ActiveX 控件。 我将尝试从我的备份中挖掘代码,看看是否可以告诉您我是如何解决它的。 我会订阅 RSS,以防万一有人先到达那里。

Oh! Yup, I've been through that, but using the raw Windows API calls some time ago, while developing an ActiveX control that detected the insertion of any kind of media. I'll try to unearth the code from my backups and see if I can tell you how I solved it. I'll subscribe to the RSS just in case somebody gets there first.

东风软 2024-07-11 05:55:40

好吧,

你可以尝试win32_逻辑磁盘类并将其绑定到__Instancecreationevent。
您可以轻松获取所需信息

Well,

u can try win32_logical disk class and bind it to the __Instancecreationevent.
You can easily get the required info

窗影残 2024-07-11 05:55:40

我在我的系统上尝试了这个,最终得到了正确的代码。 只需要一段时间。 我收到大约十几个事件,其中之一是设备连接代码。

I tried this on my system and I eventually get the right code. It just takes a while. I get a dozen or so events, and one of them is the device connect code.

无悔心 2024-07-11 05:55:40

嗯,我找不到代码。 尝试了我的旧 RAC 帐户,没有任何结果。 我的旧备份中没有任何内容。 去搞清楚。 但我尝试弄清楚我是如何做到的,并且我认为这是正确的顺序(我的很多内容都基于此 文章):

  1. 获取所有盘符和缓存
    他们。
  2. 等待 WM_DEVICECHANGE
    消息,并启动一个计时器
    1 秒超时(这样做是为了
    避免很多杂散
    启动 WM_DEVICECHANGE 消息
    一旦插入就开始
    USB 密钥/其他设备仅结束
    当驱动器“稳定”时)。
  3. 将驱动器盘符与
    旧缓存并检测新缓存。
  4. 获取这些设备的设备信息。

我知道还有其他方法,但事实证明这是唯一可以在不同版本的 Windows 中一致工作的方法,并且我们需要这种方法,因为我的客户在网页上使用 ActiveX 控件,该控件从您插入的任何类型的设备上传图像(我认为他们生产了某种打印亭)。

Well, I couldn't find the code. Tried on my old RAC account, nothing. Nothing in my old backups. Go figure. But I tried to work out how I did it, and I think this is the correct sequence (I based a lot of it on this article):

  1. Get all drive letters and cache
    them.
  2. Wait for the WM_DEVICECHANGE
    message, and start a timer with a
    timeout of 1 second (this is done to
    avoid a lot of spurious
    WM_DEVICECHANGE messages that start
    as start as soon as you insert the
    USB key/other device and only end
    when the drive is "settled").
  3. Compare the drive letters with the
    old cache and detect the new ones.
  4. Get device information for those.

I know there are other methods, but that proved to be the only one that would work consistently in different versions of windows, and we needed that as my client used the ActiveX control on a webpage that uploaded images from any kind of device you inserted (I think they produced some kind of printing kiosk).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文