How to work with WMI services to access Windows advanced
functionality
WMI is a very useful interface accessible through
DesktopX via scripting to retrieve all sort of information from the operating
system. It opens the door for new useful widgets. You can find here the full
MSDN documentation, in particular the introduction: Windows
Management Instrumentation
Write a Free Memory object that uses
WMI.
Create a new text object.
Create a new script.
Declare a global variable like this:
Dim objWMIService
This variable will hold the WMI provider. We
initialize it on Object_OnScriptEnter and delete it on Object_OnScriptEnter.
Since we are making an object that polls through WMI useful information, we
will have the WMI service object always available for the object lifetime. You
can find more information on MSDN about the actual syntax to create an
instance. GetObject("winmgmts:\\.\root\cimv2") just means you are creating a WMI instance to query information in the
cimv2 category for the local machine (\.\).
Sub Object_OnScriptEnter Set objWMIService
= GetObject("winmgmts:\\.\root\cimv2") object.settimer 123,
5000 Object_OnTimer123 'let's update it immediately End Sub
Sub
Object_OnScriptExit 'Let's clean it, as a good practice Set objWMIService
= nothing End Sub
Now it is time to query WMI for getting some
information. We are asking here for available system memory.
Sub Object_OnTimer123 Dim colItems Set
colItems = objWMIService.ExecQuery("Select AvailableMBytes from
Win32_PerfFormattedData_PerfOS_Memory",,48) For Each objItem In
colItems Object.text = "Available memory: " & objItem.AvailableMBytes
& " MB" 'we just really need one result here, so we exit Exit
Sub Next End Sub
You can simply reuse the above structure for
different WMI queries. WMI is organized in classes. Each class contains
properties. Basically, you should insert into the Select statement
(Select AvailableMBytes) the properties you want accessing later (objItem.AvailableMBytes). You use the From statement (from Win32_PerfFormattedData_PerfOS_Memory) to select the class.