C# WMI 中的软盘噪音 - Win32_LogicalDisk 类

发布于 2024-10-31 19:34:30 字数 1095 浏览 1 评论 0原文

我正在尝试使用 WMI 跟踪 Windows 上的 USB 设备插入和 CD/DVD 插入。但是当我使用 Win32_LogicalDisk 类来跟踪这些事件时,软盘开始发出噪音。

我的查询如下所示。第一个用于 USB,第二个用于 CD。

q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceCreationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(USBAdded);
w->Start();

q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceModificationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(LogicalInserted);
w->Start();

事实上,它不会在所有版本上产生噪音。任何想法将不胜感激。

I am trying to track USB device insertions and CD/DVD insertions on Windows by using WMI. However when I use Win32_LogicalDisk class to track those events, floppy starts to make noise.

My queries are like below. First one is for USB and second one is for CD.

q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceCreationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 2 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(USBAdded);
w->Start();

q = gcnew WqlEventQuery();
q->EventClassName = "__InstanceModificationEvent";
q->WithinInterval = TimeSpan(0, 0, 3);
q->Condition = "TargetInstance ISA 'Win32_LogicalDisk' and TargetInstance.DriveType = 5 and TargetInstance.DeviceID <> 'A:' and TargetInstance.DeviceID <> 'B:'";
w = gcnew ManagementEventWatcher(scope, q);
w->EventArrived += gcnew EventArrivedEventHandler(LogicalInserted);
w->Start();

Actually it does not make noise on all versions. Any idea will be greatly appreciated.

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

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

发布评论

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

评论(2

无力看清 2024-11-07 19:34:30

基于此处的 Microsoft WMI 支持消息,我不确定 Win32_LogicalDisk 上的 WMI 查询是否能够在每个轮询间隔启动软盘的情况下运行。我正在尝试自己寻找解决此问题的替代方法;当我使用托管代码时,我正在考虑仅运行一个计时器并通过 DriveInfo.GetDrives 枚举可用驱动器。

更新:当我在 Windows 服务中执行此操作时,并且已经按照此 CodeProject 文章(但具有适当的异常处理和非托管内存清理),我只是添加了 DBT_DEVICEARRIVAL 和 DBT_DEVICEREMOVECOMPLETE 消息的处理程序。 (感谢 Chris Dickson 向我指出那篇文章此处。)我在处理程序中使用 DriveInfo.GetDrives 来确定已插入或删除了哪些设备,因为我发现这比通过 Win32 获取驱动器号更干净、更简单。不涉及定期轮询,没有混乱的 WMI,并且驱动器 A 现在保持良好且安静。

Based on a Microsoft WMI support message here, I'm not certain a WMI query on Win32_LogicalDisk is going to be able to run without firing up the floppies on each polling interval. I'm trying to find an alternate way of solving this issue myself; as I'm working in managed code, I'm considering just running a timer and enumerating the available drives via DriveInfo.GetDrives.

Update: As I was doing this in a Windows service and already had implemented a message handler along the lines described in this CodeProject article (but with proper exception handling and unmanaged memory cleanup), I simply added handlers for the DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages. (Due credit to Chris Dickson for pointing that article out to me here.) I used DriveInfo.GetDrives in the handler to determine which devices had been inserted or removed, as I found that cleaner and simpler than getting at the drive letter via Win32. There's no periodic polling involved, no messy WMI, and drive A now stays nice and quiet.

‖放下 2024-11-07 19:34:30

我从 WMI 创建了一种新方法。

void MyDLPWMIDeviceListener::AddInsertUSBHandler()
{
        WqlEventQuery ^q;
        ManagementEventWatcher ^w;
        ManagementScope ^scope = gcnew ManagementScope("root\\CIMV2");
        scope->Options->EnablePrivileges = true;
        try
        {
            q = gcnew WqlEventQuery();
            q->EventClassName = "__InstanceCreationEvent";
            q->WithinInterval = TimeSpan(0, 0, 3);
        q->Condition = "TargetInstance ISA 'Win32_USBControllerDevice'";
            w = gcnew ManagementEventWatcher(scope, q);
            w->EventArrived += gcnew EventArrivedEventHandler(USBAdded);
            w->Start();
        }
        catch (Exception ^ex)
        {
            if (w != nullptr)
                w->Stop();
        }
}

之后我处理了生成的事件,如下所示:

void MyDLPWMIDeviceListener::USBAdded(Object ^sender, EventArrivedEventArgs ^e)
    {   
        try {

            PropertyData ^pd = e->NewEvent->Properties["TargetInstance"];
            if (pd != nullptr)
            {
                ManagementBaseObject ^mbo = dynamic_cast<ManagementBaseObject ^>(pd->Value);
                if(mbo != nullptr && mbo->Properties["Dependent"] != nullptr
                    && mbo->Properties["Dependent"]->Value != nullptr) {
                    String ^str = (String ^)mbo->Properties["Dependent"]->Value;
                    str = str->Replace("\"","");
                    String ^splitChar = "=";
                    array<String ^> ^strArr = str->Split(splitChar->ToCharArray());

                    WqlObjectQuery ^wqlQuery = gcnew WqlObjectQuery("Select * from Win32_PnPEntity where DeviceID = '"+strArr[1]+"'");
                    ManagementObjectSearcher ^searcher = gcnew ManagementObjectSearcher(wqlQuery);
                    for each (ManagementObject ^usbCont in searcher->Get()) {
                        String ^pnpDeviceID = (String ^)usbCont->Properties["PNPDeviceID"]->Value;
                        splitChar = "\\";
                        array<String ^> ^pnpDeviceIDArr = pnpDeviceID->Split(splitChar->ToCharArray());
                        if(pnpDeviceIDArr->Length == 3) {
                            if(pnpDeviceIDArr[0] == "USB") {
                                WqlObjectQuery ^wqlQueryDisk = gcnew WqlObjectQuery("Select * from Win32_DiskDrive where PNPDeviceID LIKE '%"+pnpDeviceIDArr[2]+"%'");
                                ManagementObjectSearcher ^searcherDisk = gcnew ManagementObjectSearcher(wqlQueryDisk);
                                ManagementObjectCollection ^collectionDisk = searcherDisk->Get();
                                if(collectionDisk->Count == 0)
                                    continue;
                                else if (collectionDisk->Count == 1) {
                                    for each (ManagementObject ^disk in collectionDisk) {

                                    }
                                }
                                else {
                                    return;
                                }
                            } else {
                                return;
                            }
                        } else {
                            return;
                        }
                    }                   
                }
            }       
        } catch (Exception ^ex) {
        } 
    }

I have created a new approach from WMI.

void MyDLPWMIDeviceListener::AddInsertUSBHandler()
{
        WqlEventQuery ^q;
        ManagementEventWatcher ^w;
        ManagementScope ^scope = gcnew ManagementScope("root\\CIMV2");
        scope->Options->EnablePrivileges = true;
        try
        {
            q = gcnew WqlEventQuery();
            q->EventClassName = "__InstanceCreationEvent";
            q->WithinInterval = TimeSpan(0, 0, 3);
        q->Condition = "TargetInstance ISA 'Win32_USBControllerDevice'";
            w = gcnew ManagementEventWatcher(scope, q);
            w->EventArrived += gcnew EventArrivedEventHandler(USBAdded);
            w->Start();
        }
        catch (Exception ^ex)
        {
            if (w != nullptr)
                w->Stop();
        }
}

After that I have handled the generated event like below:

void MyDLPWMIDeviceListener::USBAdded(Object ^sender, EventArrivedEventArgs ^e)
    {   
        try {

            PropertyData ^pd = e->NewEvent->Properties["TargetInstance"];
            if (pd != nullptr)
            {
                ManagementBaseObject ^mbo = dynamic_cast<ManagementBaseObject ^>(pd->Value);
                if(mbo != nullptr && mbo->Properties["Dependent"] != nullptr
                    && mbo->Properties["Dependent"]->Value != nullptr) {
                    String ^str = (String ^)mbo->Properties["Dependent"]->Value;
                    str = str->Replace("\"","");
                    String ^splitChar = "=";
                    array<String ^> ^strArr = str->Split(splitChar->ToCharArray());

                    WqlObjectQuery ^wqlQuery = gcnew WqlObjectQuery("Select * from Win32_PnPEntity where DeviceID = '"+strArr[1]+"'");
                    ManagementObjectSearcher ^searcher = gcnew ManagementObjectSearcher(wqlQuery);
                    for each (ManagementObject ^usbCont in searcher->Get()) {
                        String ^pnpDeviceID = (String ^)usbCont->Properties["PNPDeviceID"]->Value;
                        splitChar = "\\";
                        array<String ^> ^pnpDeviceIDArr = pnpDeviceID->Split(splitChar->ToCharArray());
                        if(pnpDeviceIDArr->Length == 3) {
                            if(pnpDeviceIDArr[0] == "USB") {
                                WqlObjectQuery ^wqlQueryDisk = gcnew WqlObjectQuery("Select * from Win32_DiskDrive where PNPDeviceID LIKE '%"+pnpDeviceIDArr[2]+"%'");
                                ManagementObjectSearcher ^searcherDisk = gcnew ManagementObjectSearcher(wqlQueryDisk);
                                ManagementObjectCollection ^collectionDisk = searcherDisk->Get();
                                if(collectionDisk->Count == 0)
                                    continue;
                                else if (collectionDisk->Count == 1) {
                                    for each (ManagementObject ^disk in collectionDisk) {

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