4

我正在尝试使用 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();

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

4

2 回答 2

3

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

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

于 2011-04-15T16:11:52.830 回答
0

我从 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) {
        } 
    }
于 2011-04-17T10:01:52.340 回答