• Welcome to Jose's Read Only Forum 2023.
 

WMIGENX - Generator of templates for WMI classes (WMI API)

Started by José Roca, July 12, 2011, 12:24:38 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca

 
This tool generates templates for WMI classes using the WMI API low-level interfaces.

Code produced for the Win32_BIOS class of the root\CIMV2 namespace.


' ########################################################################################
' WMI Template
' Namespace = root\CIMV2
' WMI Class = Win32_BIOS
' ########################################################################################

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

' ========================================================================================
' Retrieves the values of all of the properties of the Win32_BIOS class
' ========================================================================================
FUNCTION WMI_Win32_BIOS (OPTIONAL BYVAL bstrComputer AS WSTRING) AS LONG

   LOCAL hr AS LONG                                  ' // HRESULT
   LOCAL pLocator AS IWbemLocator                    ' // IWbemLocator interface reference
   LOCAL pService AS IWbemServices                   ' // IWbemServices interface reference
   LOCAL pEnumClassObject AS IEnumWbemClassObject    ' // IEnumWbemClassObject collection reference
   LOCAL pClassObject AS IWbemClassObject            ' // IWbemClassObject interface reference
   LOCAL bstrResource AS WSTRING                     ' // Path of the correct WMI namespace
   LOCAL bstrLanguage AS WSTRING                     ' // Query language
   LOCAL bstrQuery AS WSTRING                        ' // Query filter
   LOCAL uReturned AS DWORD                          ' // Number of returned references

   ' // 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

   ' // Sets the default security values for the process
   hr = CoInitializeSecurity(BYVAL %NULL, -1, BYVAL %NULL, %NULL, %RPC_C_AUTHN_LEVEL_DEFAULT, %RPC_C_IMP_LEVEL_IMPERSONATE, BYVAL %NULL, %EOAC_NONE, %NULL)
   IF hr <> %S_OK THEN FUNCTION = hr : EXIT FUNCTION

   ' // Creates an instance of the IWbemLocator interface
   pLocator = NEWCOM CLSID $CLSID_WbemLocator
   IF ISNOTHING(pLocator) THEN FUNCTION = %E_FAIL: EXIT FUNCTION

   ' // Connects with the specified machine
   IF LEN(bstrComputer) AND RIGHT$(bstrComputer, 1) <> "\" THEN bstrComputer +="\"
   bstrResource = bstrComputer & "root\CIMV2"
   hr = pLocator.ConnectServer(bstrResource, "", "", "", 0, "", NOTHING, pService)
   IF hr <> %S_OK THEN FUNCTION = hr : EXIT FUNCTION

   ' // Sets security levels on the proxy
   hr = CoSetProxyBlanket(pService, %RPC_C_AUTHN_WINNT, %RPC_C_AUTHZ_NONE, %NULL, %RPC_C_AUTHN_LEVEL_CALL, %RPC_C_IMP_LEVEL_IMPERSONATE, %NULL, %EOAC_NONE)
   IF hr <> %S_OK THEN FUNCTION = hr : EXIT FUNCTION

   ' // Executes the query
   bstrLanguage = "WQL"
   bstrQuery = "SELECT * FROM Win32_BIOS"
   hr = pService.ExecQuery(bstrLanguage, bstrQuery, %WBEM_FLAG_FORWARD_ONLY OR %WBEM_FLAG_RETURN_IMMEDIATELY, NOTHING, pEnumClassObject)

   ' // Iterate through the collection of objects
   DO
      ' // Retrieve a reference to the next object in the collection
      hr = pEnumClassObject.Next(%WBEM_INFINITE, 1, pClassObject, uReturned)
      IF uReturned = 0 THEN EXIT DO
      ' // Retrieve the values of the properties
      hr = pClassObject.Get("BiosCharacteristics", 0, vBiosCharacteristics, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("BIOSVersion", 0, vBIOSVersion, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("BuildNumber", 0, vBuildNumber, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("Caption", 0, vCaption, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("CodeSet", 0, vCodeSet, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("CurrentLanguage", 0, vCurrentLanguage, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("Description", 0, vDescription, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("IdentificationCode", 0, vIdentificationCode, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("InstallableLanguages", 0, vInstallableLanguages, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("InstallDate", 0, vInstallDate, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("LanguageEdition", 0, vLanguageEdition, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("ListOfLanguages", 0, vListOfLanguages, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("Manufacturer", 0, vManufacturer, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("Name", 0, vName, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("OtherTargetOS", 0, vOtherTargetOS, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("PrimaryBIOS", 0, vPrimaryBIOS, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("ReleaseDate", 0, vReleaseDate, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("SerialNumber", 0, vSerialNumber, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("SMBIOSBIOSVersion", 0, vSMBIOSBIOSVersion, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("SMBIOSMajorVersion", 0, vSMBIOSMajorVersion, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("SMBIOSMinorVersion", 0, vSMBIOSMinorVersion, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("SMBIOSPresent", 0, vSMBIOSPresent, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("SoftwareElementID", 0, vSoftwareElementID, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("SoftwareElementState", 0, vSoftwareElementState, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("Status", 0, vStatus, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("TargetOperatingSystem", 0, vTargetOperatingSystem, BYVAL %NULL, BYVAL %NULL)
      hr = pClassObject.Get("Version", 0, vVersion, BYVAL %NULL, BYVAL %NULL)
      ' // Release the object
      pClassObject = NOTHING
   LOOP

   ' // Release the enumerator
   pEnumClassObject = NOTHING
   ' // Release the service
   pService = NOTHING
   ' // Release the locator
   pLocator = NOTHING

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

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

   WMI_Win32_BIOS

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