473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

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_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.

8 1319
NeoPa
32,556 Expert Mod 16PB
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
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
Scratch that.... User error :(
Jun 20 '17 #4
NeoPa
32,556 Expert Mod 16PB
Do I understand that to mean you're happy and have everything working as you'd like?
Jun 20 '17 #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
32,556 Expert Mod 16PB
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
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
32,556 Expert Mod 16PB
I'm very pleased to have helped.
Jun 20 '17 #9

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

Similar topics

0
by: Knigh7 | last post by:
Hi, Could anyone provide me with a sample code, tutorial, or any other direction in which i can upload a CSV file to a mysql databse; its for a small db where the user uploads the CSV with new...
4
by: Jane Withnolastname | last post by:
I am trying to re-work an old site by replacing the html with css. On the main page, I have a logo image which I needed centred on the initial screen. I found the solution here:...
4
by: Bob hotmail.com> | last post by:
Everyone I have been spending weeks looking on the web for a good tutorial on how to use regular expressions and other methods to satisfy my craving for learning how to do FAST c-style syntax...
1
by: Lyners | last post by:
Is there an example or a guide/standard on how to create an application/help tutorial for an ASP.NET (VB.NET) application? We would like to design an animated help that would show the user how to...
42
by: Mr. x | last post by:
Hello, My previous post was in vain, so I am sending this post again. I need a good help/tutorial for VB, please (I preffer *.chm help file). Thanks :)
11
by: Magnus Lycka | last post by:
While the official Python Tutorial has served its purpose well, keeping it up to date is hardly anyones top priority, and there are others who passionately create really good Python tutorials on...
0
by: Julius Lucks | last post by:
Does anyone know of Help docs/tutorials that explain Eric3's plethora of features? http://www.die-offenbachs.de/detlev/eric3.html Thanks, Julius
31
by: Antoon Pardon | last post by:
The following is part of the explanation on slices in the tutorial: The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the...
1
by: tvance929 | last post by:
Can someone tell me, is there a way to format the text within the coding side of the rich text box? In other words, when someone clicks a button I send a message to a rich text box. In that...
1
by: wlee1970 | last post by:
Hi, I am trying to add columns to datagridview and able to insert information from my textboxs also combobox with date. Also using Microsoft Access database at the sametime. Is there any tutorials...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.