Help with WMI Tutorial

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • reguita
    New Member
    • Jun 2017
    • 5

    Help with WMI Tutorial

    This question is linked to the article Windows Management Instrumentation (WMI) Tutorial.

    I was not able to get this code to work. Or more precisely I could not get it to identify event 4. I could get events 7 and 18 but not 4.

    I was particularly interested in closing out the application if the computer goes into sleep mode.

    I copied the code exactly but I added debug.print so I could know when it is initialized and the power eventtype.

    The sub sink_OnObjectRe ady is never called and the event type is never printed in debug...
    Code:
    Option Compare Database
    Option Explicit
     
    'This is to allow the variable sink to raise events
    Dim WithEvents sink As SWbemSink
     
    Private Sub sink_OnObjectReady(ByVal objWbemObject As SWbemObject, ByVal objWbemAsyncContext As SWbemNamedValueSet)
        'Event Type 4 is the code for entering sleep mode
        Debug.Print "EventType: " & objWbemObject.EventType
        If objWbemObject.EventType = 4 Then
            Application.Quit
        End If
    End Sub
     
    Private Sub Class_Initialize()
        'Creates a new WMI object
        Dim services As SWbemServices
     
        'Creates the callback object
        Set sink = New SWbemSink
     
        'Gets the WMI service. The period in the string is used to reference the local computer.
        'You may use an IP address if you wish to query a remote computer.
        Set services = GetObject("winmgmts:\\.\root\cimv2")
     
        'Executes an asynchronous notification query
        services.ExecNotificationQueryAsync sink, "Select * from Win32_PowerManagementEvent"
        
        Debug.Print "Initialized."
    End Sub
    Last edited by NeoPa; Jun 19 '17, 10:29 PM. Reason: With further testing I was able to get the code to work but not return event 4. {NeoPa} Moved to Answers forum and converted to a question.
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    Have you tried tracing (See Debugging in VBA.) through the code to see where it diverges from what you expect for event #4?

    Comment

    • reguita
      New Member
      • Jun 2017
      • 5

      #3
      I know the code I posted does not reflect this... But in other code I've used to just record the events (no matter which) it never gets event 4. I sleep the machine but it never gets an event 4. It gets the wake up events though

      Comment

      • reguita
        New Member
        • Jun 2017
        • 5

        #4
        Scratch that.... User error :(

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Do I understand that to mean you're happy and have everything working as you'd like?

          Comment

          • reguita
            New Member
            • Jun 2017
            • 5

            #6
            Actually... I am struggling with something.

            rather than "application.qu it" I am having it close down all open forms (including the one that initializes the class and then it opens the login form. after loging in it opens the form that initializes...

            But it never captures another power event unless I close the application and start all over again.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Remember that all objects are unloaded once they go out of scope. So, if you close an object (Form) that has a class variable (Instance) stored locally then once you close that Form object then everything related to that Class instance will be lost.

              Consider storing all items in their appropriate places. If it's an object whose scope is across the whole project and which should stay instantiated until the project itself is closed, then store it as Public in a Standard Module. Whatever scope it is you need then store it in a place that provides that scope.

              Comment

              • reguita
                New Member
                • Jun 2017
                • 5

                #8
                Thanks for the reminder, I think I got it working!

                This is a much better user experience for my clients

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  I'm very pleased to have helped.

                  Comment

                  Working...