软盘噪音c# WMI - Win32_LogicalDisk类



我试图通过使用WMI跟踪USB设备插入和CD/DVD插入在Windows上。然而当我使用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();

实际上它在所有版本上都不会发出噪音。任何意见都将不胜感激。

基于微软WMI支持消息,我不确定Win32_LogicalDisk上的WMI查询是否能够在每个轮询间隔上运行而不触发软盘。我正试图找到另一种方法来解决这个问题;当我在托管代码中工作时,我正在考虑只是运行计时器并通过DriveInfo.GetDrives枚举可用驱动器。

Update:由于我是在Windows服务中这样做的,并且已经按照本文所描述的行实现了一个消息处理程序(但是使用了适当的异常处理和非托管内存清理),所以我只添加了DBT_DEVICEARRIVAL和DBT_DEVICEREMOVECOMPLETE消息的处理程序。(感谢克里斯·迪克森(Chris Dickson)在这里向我指出了这篇文章。)我用的是DriveInfo。GetDrives中的处理程序来确定哪些设备已插入或删除,因为我发现这比通过Win32获取驱动器号更干净,更简单。没有周期性轮询,没有混乱的WMI,驱动器A现在保持良好和安静。

我已经从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) {
        } 
    }

相关内容

  • 没有找到相关文章

最新更新