Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have created a new approach from WMI.</p> <pre><code>void MyDLPWMIDeviceListener::AddInsertUSBHandler() { WqlEventQuery ^q; ManagementEventWatcher ^w; ManagementScope ^scope = gcnew ManagementScope("root\\CIMV2"); scope-&gt;Options-&gt;EnablePrivileges = true; try { q = gcnew WqlEventQuery(); q-&gt;EventClassName = "__InstanceCreationEvent"; q-&gt;WithinInterval = TimeSpan(0, 0, 3); q-&gt;Condition = "TargetInstance ISA 'Win32_USBControllerDevice'"; w = gcnew ManagementEventWatcher(scope, q); w-&gt;EventArrived += gcnew EventArrivedEventHandler(USBAdded); w-&gt;Start(); } catch (Exception ^ex) { if (w != nullptr) w-&gt;Stop(); } } </code></pre> <p>After that I have handled the generated event like below:</p> <pre><code>void MyDLPWMIDeviceListener::USBAdded(Object ^sender, EventArrivedEventArgs ^e) { try { PropertyData ^pd = e-&gt;NewEvent-&gt;Properties["TargetInstance"]; if (pd != nullptr) { ManagementBaseObject ^mbo = dynamic_cast&lt;ManagementBaseObject ^&gt;(pd-&gt;Value); if(mbo != nullptr &amp;&amp; mbo-&gt;Properties["Dependent"] != nullptr &amp;&amp; mbo-&gt;Properties["Dependent"]-&gt;Value != nullptr) { String ^str = (String ^)mbo-&gt;Properties["Dependent"]-&gt;Value; str = str-&gt;Replace("\"",""); String ^splitChar = "="; array&lt;String ^&gt; ^strArr = str-&gt;Split(splitChar-&gt;ToCharArray()); WqlObjectQuery ^wqlQuery = gcnew WqlObjectQuery("Select * from Win32_PnPEntity where DeviceID = '"+strArr[1]+"'"); ManagementObjectSearcher ^searcher = gcnew ManagementObjectSearcher(wqlQuery); for each (ManagementObject ^usbCont in searcher-&gt;Get()) { String ^pnpDeviceID = (String ^)usbCont-&gt;Properties["PNPDeviceID"]-&gt;Value; splitChar = "\\"; array&lt;String ^&gt; ^pnpDeviceIDArr = pnpDeviceID-&gt;Split(splitChar-&gt;ToCharArray()); if(pnpDeviceIDArr-&gt;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-&gt;Get(); if(collectionDisk-&gt;Count == 0) continue; else if (collectionDisk-&gt;Count == 1) { for each (ManagementObject ^disk in collectionDisk) { } } else { return; } } else { return; } } else { return; } } } } } catch (Exception ^ex) { } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload