Jump to content
Sign in to follow this  
wraithdu

SWbemSink working in Vista, not XP

Recommended Posts

wraithdu

Does anyone have an idea why this would work in Vista and not in XP? MSDN says all the methods and classes are supported in XP. The events should be triggered on addition and removal of volumes, such as USB disk devices. Any help is appreciated.

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

$Obj_WMIService = ObjGet('winmgmts:\\localhost\root\cimv2')

If Not @error Then
    $obj_WMIService.ExecNotificationQueryAsync($wmiSink, "SELECT * FROM Win32_VolumeChangeEvent")
    ConsoleWrite("Ready and waiting for changes" & @CRLF)
EndIf

While 1
    Sleep(1000)
WEnd

Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    ConsoleWrite("Sink event." & @CRLF)
    ; Events:
    ; 1 = Configuration Changed
    ; 2 = Device Arrival
    ; 3 = Device Removal
    ; 4 = Docking
    ConsoleWrite($objLatestEvent.DriveName & @CRLF)
    ConsoleWrite($objLatestEvent.EventType & @CRLF)
EndFunc   ;==>SINK_OnObjectReady

Share this post


Link to post
Share on other sites
arcker

don't say it's not working :mellow:

this class is for notification event. Don't think its for async purpose.

maybe us the class win32_diskdrive with this query : SELECT * FROM __InstanceOperationEVENT WITHIN 1 WHERE TargetInstance ISA 'Win32_DiskDrive' and adapt the output.

Edited by arcker

-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post


Link to post
Share on other sites
wraithdu

Right, it's an event notification class, and I'm using ExecNotificationQueryAsync to get async notifications. It works perfectly on Vista, just not XP. MSDN says nothing about the classes being different or operating differently under XP vs Vista.

I'll have to check out your alternative later. What kind of query are you using with that? ExecQuery, ExecNotificationQuery, ...?

Edited by wraithdu

Share this post


Link to post
Share on other sites
arcker

the query i've given if for ExecNotificationQueryAsync.

DiskDrive Class can return DevicdID, but then you have to get the drive letter by using functions that you used in your eject usb script :mellow: ( or use wmi, but that's really slower ).

I think a monitoring is possible by questionning the device manager every second, as wmi do.


-- Arck System _ Soon -- Ideas make everything

"La critique est facile, l'art est difficile"

Projects :

[list] [*]Au3Service : Run your exe as service V3 / Updated 29/07/2013 Get it Here [/list]

Share this post


Link to post
Share on other sites
wraithdu

@arcker

Thanks for the direction. Here's a working script for Vista and XP.

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

$Obj_WMIService = ObjGet('winmgmts:\\localhost\root\cimv2')

If Not @error Then
    $obj_WMIService.ExecNotificationQueryAsync($wmiSink, "SELECT * FROM __InstanceOperationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LogicalDisk'")
    ConsoleWrite("Ready and waiting for changes" & @CRLF)
EndIf

While 1
    Sleep(1000)
WEnd

Func SINK_OnObjectReady($objLatestEvent, $objAsyncContext)
    Switch $objLatestEvent.Path_.Class
        Case "__InstanceCreationEvent"
            ConsoleWrite("->==========================================" & @CRLF)
            ConsoleWrite("> Creation Event" & @CRLF)
            
        Case "__InstanceDeletionEvent"
            ConsoleWrite("->==========================================" & @CRLF)
            ConsoleWrite("> Deletion Event" & @CRLF)
            
    EndSwitch
    ; Access:
    ; 0 - Unknown
    ; 1 - Readable
    ; 2 - Writable
    ; 3 - Read / Write Supported
    ; 4 - Write Once
    Switch $objLatestEvent.Path_.Class
        Case "__InstanceCreationEvent", "__InstanceDeletionEvent"
            ConsoleWrite("Access: " & $objLatestEvent.TargetInstance.Access & @CRLF)
            ConsoleWrite("Caption: " & $objLatestEvent.TargetInstance.Caption & @CRLF)
            ConsoleWrite("Description: " & $objLatestEvent.TargetInstance.Description & @CRLF)
            ConsoleWrite("DeviceID: " & $objLatestEvent.TargetInstance.DeviceID & @CRLF)
            ConsoleWrite("DriveType: " & $objLatestEvent.TargetInstance.DriveType & @CRLF)
            ConsoleWrite("FileSystem: " & $objLatestEvent.TargetInstance.FileSystem & @CRLF)
            ConsoleWrite("FreeSpace: " & Int($objLatestEvent.TargetInstance.FreeSpace / 1000000) & " MB" & @CRLF)
            ConsoleWrite("Name: " & $objLatestEvent.TargetInstance.Name & @CRLF)
            ConsoleWrite("Size: " & Int($objLatestEvent.TargetInstance.Size / 1000000) & " MB" & @CRLF)
            ConsoleWrite("VolumeSerialNumber: " & $objLatestEvent.TargetInstance.VolumeSerialNumber & @CRLF)
            ConsoleWrite("->==========================================" & @CRLF)
    EndSwitch
EndFunc   ;==>SINK_OnObjectReady

Share this post


Link to post
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
Sign in to follow this  

×