Skip to content

Latest commit

 

History

History
77 lines (55 loc) · 2.41 KB

example-monitoring-multiple-event-queries-simultaneously.md

File metadata and controls

77 lines (55 loc) · 2.41 KB
description ms.assetid ms.tgt_platform title ms.topic ms.date topic_type api_name api_type api_location
The following script example shows asynchronous event notification, making use of the SWbemServices.ExecNotificationQueryAsync method and SWbemSink object.
8beb9804-088e-4dd1-adc8-a29ca450a220
multiple
Example: Monitoring Multiple Event Queries Simultaneously
article
05/31/2018
kbArticle

Example: Monitoring Multiple Event Queries Simultaneously

The following script example shows asynchronous event notification, making use of the SWbemServices.ExecNotificationQueryAsync method and SWbemSink object.

Note

Because the callback to the sink might not be returned at the same authentication level as the client requires, it is recommended that you use semisynchronous instead of asynchronous communication. For more information, see Calling a Method.

 

Dim WithEvents sink As SWbemSink

Private Sub ExecNoteAsync_Click()

    Dim services As SWbemServices
    Dim strQuery As String
    Dim cntxt As SWbemNamedValueSet
    Set sink = New SWbemSink
    Set services = GetObject( _
        "winmgmts:{impersonationLevel=impersonate,(security)}")
    strQuery = "SELECT * FROM __InstanceCreationEvent "& _
                "WHERE TargetInstance ISA 'Win32_NTLogEvent'"
    Set cntxt = New SWbemNamedValueSet
    cntxt.Add "sinkname", "ExecNoteAsync"
    services.Security_.Privileges.Add (wbemPrivilegeSecurity)
    services.ExecNotificationQueryAsync sink, strQuery, , , , cntxt
    MsgBox "This program will asynchronously " _
        & "process Win32_NTLogEvent events."
        
End Sub

Private Sub sink_OnObjectReady( _
             ByVal objWbemObject As WbemScripting.ISWbemObject, 
             ByVal objWbemAsyncContext As _
                 WbemScripting.ISWbemNamedValueSet)
  On Error GoTo ErrorHandler
    Dim cntxt_item As WbemScripting.SWbemNamedValue

    Set cntxt_item = objWbemAsyncContext.Item("sinkname")
        
    If cntxt_item.Value = "ExecNoteAsync" Then
        MsgBox " Got event: " & objWbemObject.TargetInstance.Message
    End If

    Set objWbemObject = Nothing

    Exit Sub                                   ' Exit to avoid handler

ErrorHandler:
    MsgBox "Error number: " & Str(Err.Number) & vbNewLine & _
    "Description: " & Err.Description, vbCritical
End Sub