问题描述
||
我正在尝试使用WMI跟踪Windows上的USB设备插入和CD / DVD插入。但是,当我使用Win32_Logicaldisk类跟踪这些事件时,软盘开始发出噪音。
我的查询如下。第一个用于USB,第二个用于CD。
q = gcnew WqlEventQuery();
q->EventClassName = \"__InstanceCreationEvent\";
q->WithinInterval = TimeSpan(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,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();
实际上,它不会在所有版本上产生噪音。任何想法将不胜感激。
解决方法
根据此处的Microsoft WMI支持消息,我不确定Win32_LogicalDisk上的WMI查询是否能够在不启动每个轮询间隔的软盘的情况下运行。我正在尝试寻找自己解决该问题的另一种方法;在使用托管代码时,我正在考虑仅运行一个计时器并通过DriveInfo.GetDrives枚举可用的驱动器。
更新:在Windows服务中进行此操作时,并且已经按照此CodeProject文章中的描述实现了消息处理程序(但具有适当的异常处理和非托管内存清理功能),我只是为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,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) {
}
}