By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,122 Members | 1,641 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,122 IT Pros & Developers. It's quick & easy.

Help with WMI Tutorial

P: 5
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_OnObjectReady is never called and the event type is never printed in debug...
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2. Option Explicit
  3.  
  4. 'This is to allow the variable sink to raise events
  5. Dim WithEvents sink As SWbemSink
  6.  
  7. Private Sub sink_OnObjectReady(ByVal objWbemObject As SWbemObject, ByVal objWbemAsyncContext As SWbemNamedValueSet)
  8.     'Event Type 4 is the code for entering sleep mode
  9.     Debug.Print "EventType: " & objWbemObject.EventType
  10.     If objWbemObject.EventType = 4 Then
  11.         Application.Quit
  12.     End If
  13. End Sub
  14.  
  15. Private Sub Class_Initialize()
  16.     'Creates a new WMI object
  17.     Dim services As SWbemServices
  18.  
  19.     'Creates the callback object
  20.     Set sink = New SWbemSink
  21.  
  22.     'Gets the WMI service. The period in the string is used to reference the local computer.
  23.     'You may use an IP address if you wish to query a remote computer.
  24.     Set services = GetObject("winmgmts:\\.\root\cimv2")
  25.  
  26.     'Executes an asynchronous notification query
  27.     services.ExecNotificationQueryAsync sink, "Select * from Win32_PowerManagementEvent"
  28.  
  29.     Debug.Print "Initialized."
  30. End Sub
Jun 19 '17 #1

✓ answered by NeoPa

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.

Share this Question
Share on Google+
8 Replies


NeoPa
Expert Mod 15k+
P: 31,494
Have you tried tracing (See Debugging in VBA.) through the code to see where it diverges from what you expect for event #4?
Jun 19 '17 #2

P: 5
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
Jun 20 '17 #3

P: 5
Scratch that.... User error :(
Jun 20 '17 #4

NeoPa
Expert Mod 15k+
P: 31,494
Do I understand that to mean you're happy and have everything working as you'd like?
Jun 20 '17 #5

P: 5
Actually... I am struggling with something.

rather than "application.quit" 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.
Jun 20 '17 #6

NeoPa
Expert Mod 15k+
P: 31,494
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.
Jun 20 '17 #7

P: 5
Thanks for the reminder, I think I got it working!

This is a much better user experience for my clients
Jun 20 '17 #8

NeoPa
Expert Mod 15k+
P: 31,494
I'm very pleased to have helped.
Jun 20 '17 #9

Post your reply

Sign in to post your reply or Sign up for a free account.