AwAke Posted October 4, 2009 Share Posted October 4, 2009 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: expandcollapse popup#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 More sharing options...
wolf9228 Posted October 5, 2009 Share Posted October 5, 2009 (edited) 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: expandcollapse popup#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. expandcollapse popup#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 October 5, 2009 by wolf9228 صرح السماء كان هنا Link to comment Share on other sites More sharing options...
AwAke Posted October 5, 2009 Author Share Posted October 5, 2009 Thank you Wolf! Works perfectly. =] Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now