Jump to content

Recommended Posts

Posted

Hi,

I won't trust that at all. There are cheap USBdrives which do not follow the standard. They do not have those numbers or they are all the same.

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Posted

After seeing Rover's solution, I have to say, I like it somewhat. I didn't know you could do multiple Async queries on one Sink. Very nice.

Having said that, the use of the loop just to watch for when to display that info didn't appeal to me. So, using the technique I alluded to earlier, plus his sync, I managed to make it where it doesn't need to continuously poll for when to output, it outputs when the device is inserted. I like how responsive it turned out, makes instant info/decisions as needed. Here it is:

;WMI monitoring
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20

Dim $strComputer, $SINK, $objWMIService
$strComputer = "127.0.0.1"
$SINK = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($SINK, "SINK_")
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
If Not @error Then
    $objWMIService.ExecNotificationQueryAsync ($SINK, "SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND (TargetInstance.DriveType = 2 Or TargetInstance.DriveType = 3)")
EndIf
ConsoleWrite("In monitoring mode. Press Ctrl+C to exit." & @CRLF)
While 1
    Sleep(10000)
WEnd
;******************************************************************************
Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    ;Trap asynchronous events.
    $output = "DeviceID: " & $objLatestEvent.TargetInstance.DeviceID & @CRLF
    $output &= "Description: " & $objLatestEvent.TargetInstance.Description & @CRLF
    $output &= "FileSystem: " & $objLatestEvent.TargetInstance.FileSystem & @CRLF
    $output &= "VolumeName: " & $objLatestEvent.TargetInstance.VolumeName & @CRLF
    $output &= "Model: " & _GetDriveModel($objLatestEvent.TargetInstance) & @CRLF
    
    ToolTip($output, (@DesktopWidth - 250), (@DesktopHeight - 200), "USB Connected")
EndFunc   ;==>SINK_OnObjectReady

Func _GetDriveModel($objObject)
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $ans = ""
    $oq_part = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_LogicalDisk.DeviceID=""" & $objObject.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($oq_part) Then
        For $obj_part in $oq_part
            $oq_disk = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & $obj_part.DeviceID & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
            If IsObj($oq_disk) Then
                For $obj_disk in $oq_disk
                    $ans = $ans & $obj_disk.Model
                Next
            EndIf
        Next
    EndIf
    
    Return $ans
EndFunc
Posted

the use of the loop just to watch for when to display that info didn't appeal to me

I wasn't too happy with that workaround either

it was an an attempt to capture additional drive letters from the sink for partitioned external USB drives

instead of just the first drive letter along with the model and other info.

this attempt is much better using your code with adlibenable

(or new Timers UDF, but an include and a workaround with AutoItWinSetTitle/WinGetHandle is needed if not using a GUI)

tooltip is cleared after 15 seconds

tooltip display is additive: if a new drive is added before tooltip timeout, the timeout is reset and new drive info is added to tooltip

;WMI monitoring
$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20

Dim $strComputer, $SINK, $objWMIService, $output, $i
$strComputer = "127.0.0.1"
$SINK = ObjCreate("WbemScripting.SWbemSink")
ObjEvent($SINK, "SINK_")
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
If Not @error Then
    $objWMIService.ExecNotificationQueryAsync($SINK, "SELECT * FROM __InstanceCreationEvent" & _
    " WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk' AND" & _
    " (TargetInstance.DriveType = 2 Or TargetInstance.DriveType = 3)")
EndIf
ConsoleWrite("In monitoring mode. Press Ctrl+C to exit." & @CRLF)

AdlibEnable("_Timer", 1000)
While 1
    Sleep(10000)
WEnd

Func _Timer() ; clear tooltip
    If StringLen($output) Then
        $i += 1
    EndIf
    If $i = 15 Then
        $i = 0
        $output = ""
        ToolTip("")
    EndIf
EndFunc

;******************************************************************************
Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    ;Trap asynchronous events.
    $model = _GetDriveModel($objLatestEvent.TargetInstance)
     ; prevents model duplication when additional drive letters added (partitoned USB drives)
    Switch StringInStr($output, $model)
        Case 0
            $output &= $model & @CRLF
        Case Else
    EndSwitch
    $output &= $objLatestEvent.TargetInstance.DeviceId & " " & _
    $objLatestEvent.TargetInstance.Description & "  " & _
    $objLatestEvent.TargetInstance.FileSystem & " VOL: " & _
    $objLatestEvent.TargetInstance.VolumeName & @CRLF
    $i = 0
    ToolTip($output, (@DesktopWidth - 250), (@DesktopHeight - 200), "USB Drive Connected")
EndFunc   ;==>SINK_OnObjectReady

Func _GetDriveModel($objObject)
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")
    $ans = ""
    $oq_part = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_LogicalDisk.DeviceID=""" & _
    $objObject.DeviceID & """} WHERE AssocClass = Win32_LogicalDiskToPartition", "WQL", _
    $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    If IsObj($oq_part) Then
        For $obj_part in $oq_part
            $oq_disk = $objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & _
            $obj_part.DeviceID & """} WHERE AssocClass = Win32_DiskDriveToDiskPartition", "WQL", _
            $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
            If IsObj($oq_disk) Then
                For $obj_disk in $oq_disk
                    $ans &= $obj_disk.Model
                Next
            EndIf
        Next
    EndIf
    Return $ans
EndFunc

I see fascists...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...