Jump to content

Help with a WMI device query.


AwAke
 Share

Recommended Posts

So I got the following code from some guy on this forum but I cant remember who, if someone mentions him I'll surely give credit where its due.

The following code works fine:

$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

While 1
    $objEvent = $colEvents.NextEvent
    If $objEvent.TargetInstance.DriveType = 2 Then
        Select
            Case $objEvent.Path_.Class()="__InstanceCreationEvent"
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)

            Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
                ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
        EndSelect
    EndIf
WEnd

However if I were to add a GUI to it then it hangs. The GUI code is simply:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Drive Backup", 404, 104, 488, 280)
GUISetState(@SW_SHOW)

$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

While 1
    $objEvent = $colEvents.NextEvent
    ;----- GUI Control Loop -----;
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
    ;----------------------------;

    ;----- Device Control Loop -----;
    If $objEvent.TargetInstance.DriveType = 2 Then
        Select
            Case $objEvent.Path_.Class()="__InstanceCreationEvent"
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)

            Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
                ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
        EndSelect
    EndIf
    ;------------------------------;
WEnd

I have no experience or idea how the event even works I'm merely shooting in the dark here so if it may be something regarding how the event is caught?

After using the, 'Trace: Add trace lines' feature in Scite I see the problem is, '$objEvent = $colEvents.NextEvent' in the GUI script as it hangs there.

Is there anything I should know, or why it does this? Or am I scripting it wrong?

Thanks in advance AwAke.

Link to comment
Share on other sites

So I got the following code from some guy on this forum but I cant remember who, if someone mentions him I'll surely give credit where its due.

The following code works fine:

$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

While 1
    $objEvent = $colEvents.NextEvent
    If $objEvent.TargetInstance.DriveType = 2 Then
        Select
            Case $objEvent.Path_.Class()="__InstanceCreationEvent"
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)

            Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
                ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
        EndSelect
    EndIf
WEnd

However if I were to add a GUI to it then it hangs. The GUI code is simply:

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

$Form1 = GUICreate("Drive Backup", 404, 104, 488, 280)
GUISetState(@SW_SHOW)

$DBT_DEVICEARRIVAL = "0x00008000"
$WM_DEVICECHANGE = 0x0219
$strComputer = "."
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2")

 $colEvents = $objWMIService.ExecNotificationQuery _
    ("Select * From __InstanceOperationEvent Within 5 Where " _
        & "TargetInstance isa 'Win32_LogicalDisk'")

While 1
    $objEvent = $colEvents.NextEvent
    ;----- GUI Control Loop -----;
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
    ;----------------------------;

    ;----- Device Control Loop -----;
    If $objEvent.TargetInstance.DriveType = 2 Then
        Select
            Case $objEvent.Path_.Class()="__InstanceCreationEvent"
                Consolewrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been added." & @CR)

            Case $objEvent.Path_.Class()="__InstanceDeletionEvent"
                ConsoleWrite("Drive " & $objEvent.TargetInstance.DeviceId & "has been removed."& @CR)
        EndSelect
    EndIf
    ;------------------------------;
WEnd

I have no experience or idea how the event even works I'm merely shooting in the dark here so if it may be something regarding how the event is caught?

After using the, 'Trace: Add trace lines' feature in Scite I see the problem is, '$objEvent = $colEvents.NextEvent' in the GUI script as it hangs there.

Is there anything I should know, or why it does this? Or am I scripting it wrong?

Thanks in advance AwAke.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
HotKeySet("{ESC}", "Terminate")
$Form1 = GUICreate("Drive Backup", 404, 104, 10, 150)
GUISetState(@SW_SHOW)
$strComputer = "."
$objWMIServices = ObjGet("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" _
    & $strComputer & "\root\cimv2")

$sink = ObjCreate( _
    "WbemScripting.SWbemSink")
ObjEvent($sink,"SINK_")

 $objWMIServices.ExecNotificationQueryAsync ($sink, _ 
     "SELECT * FROM __InstanceOperationEvent WITHIN 5 WHERE " _
    & "TargetInstance ISA 'Win32_LogicalDisk'")


SplashTextOn("Title", "Connect the Flash Memory to your PC", 600, 50, -1,20, 4, "", 24)
Sleep(3000)
SplashOff()

While 1 
 $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch

Wend 

Func SINK_OnObjectReady($objObject, $objAsyncContext) 
 If $objObject.TargetInstance.DriveType = 2 Then
        Select
            Case $objObject.Path_.Class()="__InstanceCreationEvent"
                Consolewrite("Drive " & $objObject.TargetInstance.DeviceId & "has been added." & @CR)

            Case $objObject.Path_.Class()="__InstanceDeletionEvent"
                ConsoleWrite("Drive " & $objObject.TargetInstance.DeviceId & "has been removed."& @CR)
        EndSelect
    EndIf

EndFunc
    
Func Terminate()
    Exit 0
EndFunc
Edited by wolf9228

صرح السماء كان هنا

 

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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