Home Photos Links SED Editor TypeLib Browser Data Access COM WebBrowser WMI MAPI GDI+ DirectX Scripting VB6 ActiveX Other ActiveX Other Wrappers

 

Windows Management Instrumentation (WMI)

 

 

Originally released in 1998 as an add-on component with Windows NT 4.0 Service Pack 4, WMI (Windows Management Instrumentation) is the core management-enabling technology built into Windows 2000, Windows XP, and the Windows Server 2003 family of operating systems. Based on industry standards overseen by the Distributed Management Task Force (DMTF), WMI is the instrumentation and plumbing through which all—well, almost all—Windows resources can be accessed, configured, managed, and monitored.
 

With WMI you can manage computers both locally and remotely over a network. It uses databases formatted according to the Common Information Model (CIM).

 

Windows XP Professional comes with WBEMTest a general-purpose GUI tool for viewing and modifying WMI classes, instances, and methods during the development of Windows Management Instrumentation (WMI) providers and WMI applications. You can also use WBEMTest to troubleshoot WMI and programs that depend on WMI.

 

The executable file for WBEMTest, Wbemtest.exe, is installed in the WBEM directory of your Windows system directory. On Windows XP systems, this is the %WINDIR%\system32\Wbem folder.

 

 

WBEMTest comes with good documentation. Click the Help button to access to it.

 

Another tool available is the Windows Management Instrumentation command-line (WMIC), that provides you with a simple command-line interface to Windows Management Instrumentation (WMI), so you can take advantage of WMI to manage computers running the Windows Server 2003 family of operating systems. WMIC interoperates with existing shells and utility commands, and can be easily extended by scripts or other administration-oriented applications.

 

To run WMIC in interactive mode click Start, Run, type WMIC, and then press ENTER. You can also use WMIC in non-interactive mode, which is useful if you use WMIC for a batch procedure, or if you only need to execute one WMIC command.

 

WMI Administrative Tools (WMITools.exe) (Microsoft Download Center)

® WMI Tools include: WMI CIM Studio: view and edit classes, properties, qualifiers, and instances in a CIM repository; run selected methods; generate and compile MOF files. WMI Object Browser: view objects, edit property values and qualifiers, and run methods.

 

 

WMIC Documentation: Windows Management Instrumentation Command-line

WMI SDK Documentation: Windows Management Instrumentation

Tutorial: Windows Management Instrumentation Tutorial


Æ For further reading:

 

WMI Scripting Primer: Part 1
WMI Scripting Primer: Part 2

WMI Scripting Primer: Part 3

 

 

WMI low-level wrapper functions

 

The provided include file and examples use the SDK API, calling the methods using direct VTable calls. There is also an example—WmiExAutomation.bas—that uses PB Automation and enumerates the resulting collections with the help of my COM enumerator: TB_ENUM.INC. Since WMI creates dynamic properties, only late binding can be used, so don't bother making a PB interface with the PB COM Browser because it won't work. (Note: The wrapper functions included in the file TB_WMILIB.INC, packaged with the WMI Wrappers Generator tool, make much easier the use of WMI with the PowerBASIC compilers. See examples below.)

 

What is new in this version: Small modifications in some functions, a new include file (WMUtils.inc) and two new examples that show how to call a method and how to enumerate parameters.

 

 

Download

 

Download available in my forum.

 

 

WMI Wrappers Generator (TB_WMIGN)

 

Tool to generate wrapper procedures for WMI classes (for PBWIN 7.x+ and PBCC 3.x+). Includes TB_WMILIB.INC, an include file with wrapper functions to give VB-like GetObject and For Each capabilities to easily connect with WMI and enumerate the collections.

 

 

 

Download

 

Download available in my forum.

 

 

 

' Code produced by TB_WMIGN.EXE for the Win32_BIOS class of the root\CIMV2 namespace.

SUB WMI_Win32_BIOS (OPTIONAL BYVAL strComputer AS STRING)

   LOCAL hr AS LONG                   ' // HRESULT
   LOCAL oServices AS DISPATCH        ' // Services object
   LOCAL vServices AS VARIANT         ' // Services object reference
   LOCAL oItems AS DISPATCH           ' // Generic collection object
   LOCAL vItems AS VARIANT            ' // Generic collection object reference
   LOCAL oItem AS DISPATCH            ' // Generic item object
   LOCAL vItem AS VARIANT             ' // Generic item object reference
   LOCAL penum AS DWORD               ' // Generic collection's enumerator reference
   LOCAL vCount AS VARIANT            ' // Number of items in the collection
   LOCAL vQuery AS VARIANT            ' // Query string
   LOCAL vRes AS VARIANT              ' // General purpose variant
   LOCAL i AS LONG                    ' // Loop counter

   ' // Variants to store the property values
   LOCAL vBiosCharacteristics AS VARIANT   ' // Array - Unsigned 16-bit integer
   LOCAL vBIOSVersion AS VARIANT   ' // Array - String
   LOCAL vBuildNumber AS VARIANT   ' // String
   LOCAL vCaption AS VARIANT   ' // String
   LOCAL vCodeSet AS VARIANT   ' // String
   LOCAL vCurrentLanguage AS VARIANT   ' // String
   LOCAL vDescription AS VARIANT   ' // String
   LOCAL vIdentificationCode AS VARIANT   ' // String
   LOCAL vInstallableLanguages AS VARIANT   ' // Unsigned 16-bit integer
   LOCAL vInstallDate AS VARIANT   ' // Date/time value
   LOCAL vLanguageEdition AS VARIANT   ' // String
   LOCAL vListOfLanguages AS VARIANT   ' // Array - String
   LOCAL vManufacturer AS VARIANT   ' // String
   LOCAL vName AS VARIANT   ' // String
   LOCAL vOtherTargetOS AS VARIANT   ' // String
   LOCAL vPrimaryBIOS AS VARIANT   ' // Boolean value
   LOCAL vReleaseDate AS VARIANT   ' // Date/time value
   LOCAL vSerialNumber AS VARIANT   ' // String
   LOCAL vSMBIOSBIOSVersion AS VARIANT   ' // String
   LOCAL vSMBIOSMajorVersion AS VARIANT   ' // Unsigned 16-bit integer
   LOCAL vSMBIOSMinorVersion AS VARIANT   ' // Unsigned 16-bit integer
   LOCAL vSMBIOSPresent AS VARIANT   ' // Boolean value
   LOCAL vSoftwareElementID AS VARIANT   ' // String
   LOCAL vSoftwareElementState AS VARIANT   ' // Unsigned 16-bit integer
   LOCAL vStatus AS VARIANT   ' // String
   LOCAL vTargetOperatingSystem AS VARIANT   ' // Unsigned 16-bit integer
   LOCAL vVersion AS VARIANT   ' // String

   ' // Connect to WMI using a moniker
   IF LEN(strComputer) = 0 THEN strComputer = "."
   hr = WmiGetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2", vServices)
   IF hr <> %S_OK THEN GOTO Terminate
   SET oServices = vServices
   vServices = EMPTY
   IF ISFALSE ISOBJECT(oServices) THEN GOTO Terminate

   ' // Execute a query to get a reference to the collection of objects
   vQuery = "SELECT * FROM Win32_BIOS"
   OBJECT CALL oServices.ExecQuery(vQuery) TO vItems
   IF ISTRUE OBJRESULT THEN GOTO Terminate
   SET oItems = vItems
   vItems = EMPTY
   IF ISFALSE ISOBJECT(oItems) THEN GOTO Terminate

   ' // Retrieve the number of items in the collection
   OBJECT GET oItems.Count TO vCount

   ' // Retrieve a reference to the collection's enumerator
   hr = Wmi_NewEnum(OBJPTR(oItems), penum)
   IF hr <> %S_OK OR penum = %NULL THEN GOTO Terminate

   ' // Iterate through the collection of objects.
   FOR i = 1 TO VARIANT#(vCount)
      ' // Retrieve a reference to the next object in the collection
      hr = WmiEnum_NextItem(penum, vItem)
      IF hr <> %S_OK THEN EXIT FOR
      SET oItem = vItem
      vItem = EMPTY
      IF ISFALSE ISOBJECT(oItem) THEN EXIT FOR
      ' // Retrieve the values of the properties
      OBJECT GET oItem.BiosCharacteristics TO vBiosCharacteristics
      OBJECT GET oItem.BIOSVersion TO vBIOSVersion
      OBJECT GET oItem.BuildNumber TO vBuildNumber
      OBJECT GET oItem.Caption TO vCaption
      OBJECT GET oItem.CodeSet TO vCodeSet
      OBJECT GET oItem.CurrentLanguage TO vCurrentLanguage
      OBJECT GET oItem.Description TO vDescription
      OBJECT GET oItem.IdentificationCode TO vIdentificationCode
      OBJECT GET oItem.InstallableLanguages TO vInstallableLanguages
      OBJECT GET oItem.InstallDate TO vInstallDate
      OBJECT GET oItem.LanguageEdition TO vLanguageEdition
      OBJECT GET oItem.ListOfLanguages TO vListOfLanguages
      OBJECT GET oItem.Manufacturer TO vManufacturer
      OBJECT GET oItem.Name TO vName
      OBJECT GET oItem.OtherTargetOS TO vOtherTargetOS
      OBJECT GET oItem.PrimaryBIOS TO vPrimaryBIOS
      OBJECT GET oItem.ReleaseDate TO vReleaseDate
      OBJECT GET oItem.SerialNumber TO vSerialNumber
      OBJECT GET oItem.SMBIOSBIOSVersion TO vSMBIOSBIOSVersion
      OBJECT GET oItem.SMBIOSMajorVersion TO vSMBIOSMajorVersion
      OBJECT GET oItem.SMBIOSMinorVersion TO vSMBIOSMinorVersion
      OBJECT GET oItem.SMBIOSPresent TO vSMBIOSPresent
      OBJECT GET oItem.SoftwareElementID TO vSoftwareElementID
      OBJECT GET oItem.SoftwareElementState TO vSoftwareElementState
      OBJECT GET oItem.Status TO vStatus
      OBJECT GET oItem.TargetOperatingSystem TO vTargetOperatingSystem
      OBJECT GET oItem.Version TO vVersion
      ' // Release the object
      SET oItem = NOTHING
   NEXT

   ' // Release the collection enumerator
   WmiRelease penum
   ' // Release the collection object
   IF ISOBJECT(oItems) THEN SET oItems = NOTHING

Terminate: 

   ' // Release the Services object
   IF ISOBJECT(oServices) THEN SET oServices = NOTHING

END SUB

 

 

 

 

' The following example demonstrates how to use the wrapper functions included in the file
' TB_WMILIB.INC together with PB Automation.

' ========================================================================================
' WMI example - Retrieving BIOS information
' SED_PBCC - Use the PBCC compiler
' ========================================================================================

#COMPILE EXE
#DIM ALL
#INCLUDE "TB_WMILIB.INC"    ' // WMI helper functions

' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN

   LOCAL hr AS LONG                   ' // HRESULT
   LOCAL oServices AS DISPATCH        ' // Services object
   LOCAL vServices AS VARIANT         ' // Services object reference
   LOCAL oItems AS DISPATCH           ' // Generic collection object
   LOCAL vItems AS VARIANT            ' // Generic collection object reference
   LOCAL oItem AS DISPATCH            ' // Generic item object
   LOCAL vItem AS VARIANT             ' // Generic item object reference
   LOCAL penum AS DWORD               ' // Collection's enumerator reference
   LOCAL vCount AS VARIANT            ' // Number of items in the collection
   LOCAL vVar AS VARIANT              ' // General purpose variant
   LOCAL vRes AS VARIANT              ' // General purpose variant
   LOCAL i AS LONG                    ' // Loop counter
   LOCAL x AS LONG                    ' // Loop counter
   DIM vArray(0) AS VARIANT           ' // General purpose array of variants

   ' // Connect to WMI using a moniker
   hr = WmiGetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2", vServices)
   IF hr <> %S_OK THEN GOTO Terminate
   SET oServices = vServices
   vServices = EMPTY
   IF ISFALSE ISOBJECT(oServices) THEN GOTO Terminate

   ' // Execute a query to get a reference to the collection of running processes
   vVar = "SELECT * FROM Win32_BIOS"
   OBJECT CALL oServices.ExecQuery(vVar) TO vItems
   IF OBJRESULT THEN GOTO Terminate
   SET oItems = vItems
   vItems = EMPTY
   IF ISFALSE ISOBJECT(oItems) THEN GOTO Terminate

   ' // Retrieve the number of items in the collection
   OBJECT GET oItems.Count TO vCount

   ' // Retrieve a reference to the collection's enumerator
   hr = Wmi_NewEnum(OBJPTR(oItems), penum)
   IF hr <> %S_OK OR penum = %NULL THEN GOTO Terminate

   ' // Iterate throught the collection of objects.
   FOR i = 1 TO VARIANT#(vCount)
      ' // Retrieve a reference to the next object in the collection
      hr = WmiEnum_NextItem(penum, vItem)
      IF hr <> %S_OK THEN EXIT FOR
      SET oItem = vItem
      IF ISFALSE ISOBJECT(oItem) THEN EXIT FOR
      ' // Retrieve information about the BIOS
      ' // BIOS version (array of the complete system BIOS information)
      OBJECT GET oItem.BIOSVersion TO vRes
      vArray() = vRes
      PRINT "BIOS version:"
      FOR x = LBOUND(vArray) TO UBOUND(vArray)
         STDOUT VARIANT$(vArray(x))
      NEXT
      ' // BIOS characteristics (array of words)
      OBJECT GET oItem.BIOSCharacteristics TO vRes
      vArray() = vRes
      PRINT "BIOS characteristics:";
      FOR x = LBOUND(vArray) TO UBOUND(vArray)
         PRINT STR$(VARIANT#(vArray(x)));
      NEXT
      PRINT
      ' // Build number
      OBJECT GET oItem.BuildNumber TO vRes
      PRINT "Build number: " & VARIANT$(vRes)
      ' // Caption
      OBJECT GET oItem.Caption TO vRes
      PRINT "Caption: " & VARIANT$(vRes)
      ' // Code set
      OBJECT GET oItem.CodeSet TO vRes
      PRINT "Code set: " & VARIANT$(vRes)
      ' // Current language
      OBJECT GET oItem.CurrentLanguage TO vRes
      PRINT "Current language: " & VARIANT$(vRes)
      ' // Description
      OBJECT GET oItem.Description TO vRes
      PRINT "Description: " & VARIANT$(vRes)
      ' // Identification code
      OBJECT GET oItem.IdentificationCode TO vRes
      PRINT "Identification code: " & VARIANT$(vRes)
      ' // Installable languages
      OBJECT GET oItem.InstallableLanguages TO vRes
      PRINT "Installable languages: " VARIANT#(vRes)
      ' // Install date
      OBJECT GET oItem.InstallDate TO vRes
      PRINT "Install date: "  & VARIANT$(vRes)
      ' // Language edition
      OBJECT GET oItem.LanguageEdition TO vRes
      PRINT "Language edition: " & VARIANT$(vRes)
      ' // List of languages
      OBJECT GET oItem.ListOfLanguages TO vRes
      vArray() = vRes
      PRINT "List of languages:"
      FOR x = LBOUND(vArray) TO UBOUND(vArray)
         STDOUT VARIANT$(vArray(x))
      NEXT
      ' // Manufacturer
      OBJECT GET oItem.Manufacturer TO vRes
      PRINT "Manufacturer: " & VARIANT$(vRes)
      ' // Name
      OBJECT GET oItem.Name TO vRes
      PRINT "Name: " & VARIANT$(vRes)
      ' // Other target OS
      OBJECT GET oItem.OtherTargetOS TO vRes
      PRINT "Other target OS: " & VARIANT$(vRes)
      ' // Primary BIOS
      OBJECT GET oItem.PrimaryBIOS TO vRes
      PRINT "Primary BIOS: " CINT(VARIANT#(vRes))
      ' // Release date
      OBJECT GET oItem.ReleaseDate TO vRes
      PRINT "Release date: "  & VARIANT$(vRes)
      ' // Serial number
      OBJECT GET oItem.SerialNumber TO vRes
      PRINT "Serial number: " & VARIANT$(vRes)
      ' // BIOS version as reported by SMBIOS
      OBJECT GET oItem.SMBIOSBIOSVersion TO vRes
      PRINT "SMBIOS BIOS version: " & VARIANT$(vRes)
      ' // Major SMBIOS version number
      OBJECT GET oItem.SMBIOSMajorVersion TO vRes
      PRINT "SMBIOS major version: " VARIANT#(vRes)
      ' // Minor SMBIOS version number
      OBJECT GET oItem.SMBIOSMinorVersion TO vRes
      PRINT "SMBIOS minor version: " VARIANT#(vRes)
      ' // SMBIOS present
      OBJECT GET oItem.SMBIOSPresent TO vRes
      PRINT "SMBIOS present: " CINT(VARIANT#(vRes))
      ' // Software element ID
      OBJECT GET oItem.SoftwareElementID TO vRes
      PRINT "Software element ID: " & VARIANT$(vRes)
      ' // Software element state
      OBJECT GET oItem.SoftwareElementState TO vRes
      PRINT "Software element state: " VARIANT#(vRes)
      ' // Status
      OBJECT GET oItem.Status TO vRes
      PRINT "Status : " & VARIANT$(vRes)
      ' // Target operating system
      OBJECT GET oItem.TargetOperatingSystem TO vRes
      PRINT "Target operating system: " VARIANT#(vRes)
      ' // Version
      OBJECT GET oItem.Version TO vRes
      PRINT "Version: " & VARIANT$(vRes)
      ' // Release the object
      SET oItem = NOTHING
   NEXT

   ' // Release the collection enumerator
   WmiRelease penum
   ' // Release the collection object
   IF ISOBJECT(oItems) THEN SET oItems = NOTHING

Terminate:

   IF ISOBJECT(oServices) THEN SET oServices = NOTHING
   WAITKEY$

END FUNCTION
' ========================================================================================

 

 

 

' For faster execution, include in the query string only the names of the properties that
' you want to retrieve and/or the methods that you want to execute. The following example
' queries only for the Name, Drive, VolumeName and MediaLoaded properties of the
' Win32_CDROMDrive class.

#COMPILE EXE
#DIM ALL
#INCLUDE "TB_WMILIB.INC"

FUNCTION PBMAIN

   LOCAL hr AS LONG                   ' // HRESULT
   LOCAL oServices AS DISPATCH        ' // Services object
   LOCAL vServices AS VARIANT         ' // Services object reference
   LOCAL oItems AS DISPATCH           ' // Generic collection object
   LOCAL vItems AS VARIANT            ' // Generic collection object reference
   LOCAL oItem AS DISPATCH            ' // Generic item object
   LOCAL vItem AS VARIANT             ' // Generic item object reference
   LOCAL penum AS DWORD               ' // Generic collection's enumerator reference
   LOCAL vCount AS VARIANT            ' // Number of items in the collection
   LOCAL vQuery AS VARIANT            ' // Query string
   LOCAL vRes AS VARIANT              ' // General purpose variant
   LOCAL i AS LONG                    ' // Loop counter
   LOCAL strComputer AS STRING        ' // Computer name

   ' // Variants to store the property values
   LOCAL vDrive AS VARIANT   ' // String
   LOCAL vName AS VARIANT   ' // String
   LOCAL vVolumeName AS VARIANT   ' // String
   LOCAL vMediaLoaded AS VARIANT   ' // Boolean value

   ' // Connect to WMI using a moniker
   strComputer = "."   ' <-- change as needed
   hr = WmiGetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2", vServices)
   IF hr <> %S_OK THEN GOTO Terminate
   SET oServices = vServices
   vServices = EMPTY
   IF ISFALSE ISOBJECT(oServices) THEN GOTO Terminate

   ' // Execute a query to get a reference to the collection of objects
   vQuery = "SELECT Name, Drive, VolumeName, MediaLoaded FROM Win32_CDROMDrive"
   OBJECT CALL oServices.ExecQuery(vQuery) TO vItems
   IF ISTRUE OBJRESULT THEN GOTO Terminate
   SET oItems = vItems
   vItems = EMPTY
   IF ISFALSE ISOBJECT(oItems) THEN GOTO Terminate

   ' // Retrieve the number of items in the collection
   OBJECT GET oItems.Count TO vCount

   ' // Retrieve a reference to the collection's enumerator
   hr = Wmi_NewEnum(OBJPTR(oItems), penum)
   IF hr <> %S_OK OR penum = %NULL THEN GOTO Terminate

   ' // Iterate through the collection of objects.
   FOR i = 1 TO VARIANT#(vCount)
      ' // Retrieve a reference to the next object in the collection
      hr = WmiEnum_NextItem(penum, vItem)
      IF hr <> %S_OK THEN EXIT FOR
      SET oItem = vItem
      vItem = EMPTY
      IF ISFALSE ISOBJECT(oItem) THEN EXIT FOR
      ' // Retrieve the values of the properties
      OBJECT GET oItem.Drive TO vDrive
      PRINT VARIANT$(vDrive)
      OBJECT GET oItem.NAME TO vName
      PRINT VARIANT$(vName)
      OBJECT GET oItem.VolumeName TO vVolumeName
      PRINT VARIANT$(vVolumeName)
      OBJECT GET oItem.MediaLoaded TO vMediaLoaded
      PRINT CINT(VARIANT#(vMediaLoaded))
      ' // Release the object
      SET oItem = NOTHING
   NEXT

   ' // Release the collection enumerator
   WmiRelease penum
   ' // Release the collection object
   IF ISOBJECT(oItems) THEN SET oItems = NOTHING

Terminate:

   ' // Release the Services object
   IF ISOBJECT(oServices) THEN SET oServices = NOTHING

   WAITKEY$

END FUNCTION

 

 

Home Photos Links SED Editor TypeLib Browser Data Access COM WebBrowser WMI MAPI GDI+ DirectX Scripting VB6 ActiveX Other ActiveX Other Wrappers

 

Page last updated on Friday, 20 July 2007 19:10:48 +0200