Floppy disk in C # WMI - Win32_LogicalDisk class

I am trying to track the insertion of USB devices and the insertion of CD / DVD in Windows using WMI. However , when I use the Win32_LogicalDisk class to track these events, the floppy starts to make noise.

My queries are similar to below. The first is for USB, and the second 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(); 

In fact, it does not create noise in all versions. Any idea would be greatly appreciated.

+4
source share
2 answers

Based on the Microsoft WMI support message here , I am not sure that the WMI request in Win32_LogicalDisk will work without firing on floppy disks at each polling interval. I am trying to find an alternative way to solve this problem; since I'm working on managed code, I'm considering starting a timer and listing available drives through DriveInfo.GetDrives.

Update. Since I did this on a Windows service and already implemented a message handler along the lines described in this CodeProject article (but with proper exception handling and unmanaged memory clearing), I just added handlers for DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE messages. (With respect to Chris Dixon, for pointing this article to me here .) I used DriveInfo.GetDrives in the handler to determine which devices were inserted or removed, as I found it to be cleaner and easier than getting the drive letter through Win32. There are no periodic polls, no messy WMI, and drive A now remains pleasant and calm.

+3
source

I 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 processed the generated event as shown 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) { } } 
0
source

Source: https://habr.com/ru/post/1347991/


All Articles