Announcement

Collapse
No announcement yet.

WMI (Windows Management Instrumentation)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • WMI (Windows Management Instrumentation)

    Windows Management Instrumentation (WMI) is the Microsoft implementation of Web-based Enterprise Management (WBEM), which is an industry initiative to develop a standard technology for accessing management information in an enterprise environment. WMI uses the Common Information Model (CIM) industry standard to represent systems, applications, networks, devices, and other managed components. CIM is developed and maintained by the Distributed Management Task Force (DMTF).

    I'm providing two tools to browse the installed WMI classes and generate templates for PowerBASIC:

    WMIGEN - Generator of templates for WMI classes (DISPATCH)
    http://www.powerbasic.com/support/pb...ad.php?t=49645

    WMIGENX - Generator of templates for WMI classes (WMI API)
    http://www.powerbasic.com/support/pb...ad.php?t=49646

    The templates generated by WMIGEN are easier to use thanks to the wrapper function WmiGetObject that allows to use monikers to connect with WMI.

    To extract the values of the retrieved properties, I'm providing the wrapper function AfxVarToStr, available in AfxVarToStr.inc, although it requires Windows XP SP2 or superior.

    AfxVarToStr extracts the value of a variant structure to a newly-allocated string. If no value can be extracted, then an empty string is returned. When the variant contains an array, each element of the array is appended to the resulting string separated with a semicolon and a space, making it suitable to be used with PARSECOUNT, PARSE$ and PARSE.

    To decode the result returned when a variant contains a WMI date/time value, it can be decoded using the WmiDate2Udt wrapper function.

    Error codes are returned by OBJRSULT, and you can get a description of the error by calling WmiGetErrorCodeText(OBJRESULT).

    The following example retrieves all the available information from the Win32_BIOS class. The code has been generated with the WMIGEN.EXE tool, the wrapper function AfxVarToStr has been used to display the results and the wraper function WmiDate2Udt has been used to decode the WMI date/time returned by the call to the release property.

    Code:
    ' ########################################################################################
    ' WMI Template
    ' Namespace = root\CIMV2
    ' WMI Class = Win32_BIOS
    ' ########################################################################################
    
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WMI.INC"
    #INCLUDE "AfxVarToStr.INC"
    
    ' ========================================================================================
    ' Retrieves the values of all of the properties of the Win32_BIOS class
    ' ========================================================================================
    SUB WMI_Win32_BIOS (OPTIONAL BYVAL bstrComputer AS WSTRING)
    
       LOCAL hr AS LONG                                  ' // HRESULT
       LOCAL pService AS ISWbemServices                  ' // Services object
       LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
       LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
       LOCAL bstrDisplayName AS WSTRING                  ' // Display name
       LOCAL bstrQuery AS WSTRING                        ' // Query string
       LOCAL oItem AS DISPATCH                           ' // Generic object variable
       LOCAL vItem AS VARIANT                            ' // Generic object variant
       LOCAL vRes AS VARIANT                             ' // General purpose variant
    
       ' // 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(bstrComputer) = 0 THEN bstrComputer = "."
       bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\" & bstrComputer & "\root\CIMV2"
       pService = WmiGetObject(bstrDisplayName)
       IF ISNOTHING(pService) THEN EXIT SUB
    
       ' // Execute a query to get a reference to the collection of objects
       bstrQuery = "SELECT * FROM Win32_BIOS"
       pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
       IF ISNOTHING(pObjectSet) THEN EXIT SUB
       ' // Retrieve a reference to the collection's enumerator
       pEnum = pObjectSet.NewEnum_
       IF ISNOTHING(pEnum) THEN EXIT SUB
       ' // Iterate through the collection of objects
       DO
          ' // Retrieve a reference to the next object in the collection
          hr = pEnum.Next(1, vItem, BYVAL %NULL)
          IF hr <> %S_OK THEN EXIT DO
          ' // Assign the VT_DISPATCH variant to the object variable
          oItem = vItem : vItem = EMPTY
          IF ISNOTHING(oItem) THEN EXIT DO
          ' // 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
          ' // Display the retrieved values
          ? "Bios characteristics: " & AfxVarToStr(vBiosCharacteristics)
          ? "Bios version " & AfxVarToStr(vBIOSVersion)
          ? "Build number: " & AfxVarToStr(vBuildNumber)
          ? "Caption: " & AfxVarToStr(vCaption)
          ? "Code set: " & AfxVarToStr(vCodeSet)
          ? "Current language: " & AfxVarToStr(vCurrentLanguage)
          ? "Description: " & AfxVarToStr(vDescription)
          ? "Identification code: " & AfxVarToStr(vIdentificationCode)
          ? "Installable language: " & AfxVarToStr(vInstallableLanguages)
          ? "Install date: " & AfxVarToStr(vInstallDate)
          ? "Language edition: " & AfxVarToStr(vLanguageEdition)
          ? "List of languages: " & AfxVarToStr(vListOfLanguages)
          ? "Manufacturer: " & AfxVarToStr(vManufacturer)
          ? "Name: " & AfxVarToStr(vName)
          ? "Other target OS: " & AfxVarToStr(vOtherTargetOS)
          ? "Primary Bios: " & AfxVarToStr(vPrimaryBIOS)
          ? "Release date: " & AfxVarToStr(vReleaseDate)
          ? "Serial number: " & AfxVarToStr(vSerialNumber)
          ? "SMBIOS Bios version: " & AfxVarToStr(vSMBIOSBIOSVersion)
          ? "SMBIOS major version: " & AfxVarToStr(vSMBIOSMajorVersion)
          ? "SMBIOS minor version: " & AfxVarToStr(vSMBIOSMinorVersion)
          ? "SMBIOS present: " & AfxVarToStr(vSMBIOSPresent)
          ? "Software element ID: " & AfxVarToStr(vSoftwareElementID)
          ? "Software element state: " & AfxVarToStr(vSoftwareElementState)
          ? "Status: " & AfxVarToStr(vStatus)
          ? "Target operating version: " & AfxVarToStr(vTargetOperatingSystem)
          ? "Version: " & AfxVarToStr(vVersion)
          ' // Decode the release date value
          LOCAL tDate AS WMI_DATE
          TYPE SET tDate = WmiDate2Udt(vReleaseDate)
          ? "Year: " & STR$(tDate.Year)
          ? "Month " & STR$(tDate.Month)
          ? "Day " & STR$(tDate.Day)
          ? "Hours " & STR$(tDate.Hours)
          ? "Minutes " & STR$(tDate.Minutes)
          ? "Seconds " & STR$(tDate.Seconds)
          ? "Microseconds " & STR$(tDate.Microseconds)
          ? "UTC " & STR$(tDate.UTC)
          ' // Release the object
          oItem = NOTHING
       LOOP
    
       ' // Release the enumerator
       pEnum = NOTHING
       ' // Release the collection
       pObjectSet = NOTHING
       ' // Release the service
       pService = NOTHING
    
    END SUB
    ' ========================================================================================
    
    ' ========================================================================================
    ' Main
    ' ========================================================================================
    FUNCTION PBMAIN
    
       WMI_Win32_BIOS
       #IF %DEF(%PB_CC32)
          WAITKEY$
       #ENDIF
    
    END FUNCTION
    ' ========================================================================================
    The output will look something like:

    Code:
    Bios characteristics: 4; 7; 9; 10; 11; 12; 14; 15; 16; 17; 19; 23; 24; 25; 26; 2
    7; 28; 29; 30; 32; 33; 36; 37; 40; 42; 51; 56; 57; 58; 59; 64; 65; 66; 67; 68; 6
    9; 70; 71; 72; 73; 74; 75; 76; 77; 78; 79
    Bios version HPQOEM - 20100128; BIOS Date: 01/28/10 15:16:52 Ver: 5.17
    Build number:
    Caption: BIOS Date: 01/28/10 15:16:52 Ver: 5.17
    Code set:
    Current language: en|US|iso8859-1
    Description: BIOS Date: 01/28/10 15:16:52 Ver: 5.17
    Identification code:
    Installable language: 1
    Install date:
    Language edition:
    List of languages: en|US|iso8859-1
    Manufacturer: American Megatrends Inc.
    Name: BIOS Date: 01/28/10 15:16:52 Ver: 5.17
    Other target OS:
    Primary Bios: -1
    Release date: 20100128000000.000000+000
    Serial number: CZC00737C1
    SMBIOS Bios version: 5.17
    SMBIOS major version: 2
    SMBIOS minor version: 5
    SMBIOS present: -1
    Software element ID: BIOS Date: 01/28/10 15:16:52 Ver: 5.17
    Software element state: 3
    Status: OK
    Target operating version: 0
    Version: HPQOEM - 20100128
    Year:  2010
    Month  1
    Day  28
    Hours  0
    Minutes  0
    Seconds  0
    Microseconds  0
    UTC  0
    To interpret results like

    Code:
    Bios characteristics: 4; 7; 9; 10; 11; 12; 14; 15; 16; 17; 19; 23; 24; 25; 26; 2
    7; 28; 29; 30; 32; 33; 36; 37; 40; 42; 51; 56; 57; 58; 59; 64; 65; 66; 67; 68; 6
    9; 70; 71; 72; 73; 74; 75; 76; 77; 78; 79
    look at the documentation for the class.

    Win32_BIOS class
    http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

    Code:
    BiosCharacteristics
    
        Data type: uint16 array
        Access type: Read-only
    
        Array of BIOS characteristics supported by the system as defined by the System Management BIOS Reference Specification.
    
        Value	Meaning
    
        0    Reserved
    
        1    Reserved
    
        2    Unknown
    
        3    BIOS Characteristics Not Supported
    
        4    ISA is supported
    
        5    MCA is supported
    
        6    EISA is supported
    
        7    PCI is supported
    
        8    PC Card (PCMCIA) is supported
    
        9    Plug and Play is supported
    
        10    APM is supported
    
        11    BIOS is Upgradable (Flash)
    
        12    BIOS shadowing is allowed
    
        13    VL-VESA is supported
    
        14    ESCD support is available
    
        15    Boot from CD is supported
    
        16    Selectable Boot is supported
    
        17    BIOS ROM is socketed
    
        18    Boot From PC Card (PCMCIA) is supported
    
        19    EDD (Enhanced Disk Drive) Specification is supported
    
        20    Int 13h - Japanese Floppy for NEC 9800 1.2mb (3.5, 1k Bytes/Sector, 360 RPM) is supported
    
        21    Int 13h - Japanese Floppy for Toshiba 1.2mb (3.5, 360 RPM) is supported
    
        22    Int 13h - 5.25 / 360 KB Floppy Services are supported
    
        23    Int 13h - 5.25 /1.2MB Floppy Services are supported
    
        24    Int 13h - 3.5 / 720 KB Floppy Services are supported
    
        25    Int 13h - 3.5 / 2.88 MB Floppy Services are supported
    
        26    Int 5h, Print Screen Service is supported
    
        27    Int 9h, 8042 Keyboard services are supported
    
        28    Int 14h, Serial Services are supported
    
        29    Int 17h, printer services are supported
    
        30    Int 10h, CGA/Mono Video Services are supported
    
        31    NEC PC-98
    
        32    ACPI is supported
    
        33    USB Legacy is supported
    
        34    AGP is supported
    
        35    I2O boot is supported
    
        36    LS-120 boot is supported
    
        37    ATAPI ZIP Drive boot is supported
    
        38    1394 boot is supported
    
        39    Smart Battery is supported
    
        40:47    Reserved for BIOS vendor
    
        48:63    Reserved for system vendor
    Last edited by José Roca; 15 Feb 2012, 08:40 PM.
    Forum: http://www.jose.it-berater.org/smfforum/index.php

  • #2
    Hello,

    i tried the sample as shown here, but unfortunatly the compiler breaks with

    PowerBASIC 6 Console Compiler
    Copyright (c) 1998-2011 PowerBasic Inc.
    Englewood, Florida USA
    All Rights Reserved

    Error 524 in C:\PB_LIBRARY_10\JOSEINC\WINAPI_III\OCIDL.INC(331:020): Undefined TYPE
    Line 331: BYREF ppTI AS ITypeInfo _ ' __out ITypeInfo **ppTI

    Comment


    • #3
      The example works fine, using my includes alone. You surely have the path to my includes mixed with the path to the PB includes in your PB Ide.

      You must also be aware that these headers are not extensions to the ones provided with the compiler, but a full replacement. Therefore, you must not mix them with the PowerBASIC include files in any way, neither directly (via #INCLUDE), nor indirectly (via the include path in the IDE).
      Forum: http://www.jose.it-berater.org/smfforum/index.php

      Comment


      • #4
        WMI: CallMethod Example

        Demonstrates how to pass values to a method, how to call it and how to retrieve return values. Uses the "StdRegProv" class to retrieve the ProductName in the HKEY_LOCAL_MACHINE "Software\Microsoft\Windows NT\CurrentVersion" in the registry.

        Code:
        ' ========================================================================================
        ' Demonstrates how to pass values to a method, how to call it and how to retrieve return
        ' values. Uses the "StdRegProv" class to retrieve the ProductName in the
        ' HKEY_LOCAL_MACHINE "Software\Microsoft\Windows NT\CurrentVersion" in the registry.
        ' ========================================================================================
        
        ' SED_PBCC - Use the PBCC compiler
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WMI.INC"
        
        ' ========================================================================================
        ' Main
        ' ========================================================================================
        FUNCTION PBMAIN
        
           LOCAL oReg AS DISPATCH                      ' // Dispatch object variable
           LOCAL bstrDisplayName AS WSTRING            ' // Display name
           LOCAL vDefKey AS VARIANT                    ' // Registry tree
           LOCAL vPath AS VARIANT                      ' // Registry path
           LOCAL vValue AS VARIANT                     ' // Key value
           LOCAL vResult AS VARIANT                    ' // Result
           LOCAL vRes AS VARIANT                       ' // General purpose variant
        
           ' // Connect to WMI using a moniker
           bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv"
           oReg = WmiGetObject(bstrDisplayName)
           IF ISNOTHING(oReg) THEN EXIT FUNCTION
        
           ' %HKEY_LOCAL_MACHINE - The value must be specified as an string and in decimal, not hexadecimal.
           vDefKey = "2147483650"
           vPath = "Software\Microsoft\Windows NT\CurrentVersion"
           vValue = "ProductName"
        
           TRY
              OBJECT CALL oReg.GetStringValue(vDefKey, vPath, vValue, vResult) TO vRes
              AfxShowMsg(VARIANT$(vResult))
           CATCH
              AfxShowMsg("Error " & HEX$(OBJRESULT) & $CRLF & WmiGetErrorCodeText(OBJRESULT))
           END TRY
        
        END FUNCTION
        ' ========================================================================================
        Same as above, but using the low-level API:

        Code:
        ' ========================================================================================
        ' Demonstrates how to pass values to a method, how to call it and how to retrieve return
        ' values. Uses the "StdRegProv" class to retrieve the ProductName in the
        ' HKEY_LOCAL_MACHINE "Software\Microsoft\Windows NT\CurrentVersion" in the registry.
        ' ========================================================================================
        
        #COMPILE EXE
        #DIM ALL
        #INCLUDE "WMI.INC"
        
        FUNCTION PBMAIN
        
           LOCAL hr AS LONG                                     ' // HRESULT
           LOCAL pIWbemLocator AS IWbemLocator                  ' // IWbemLocator interface reference
           LOCAL pIWbemServices AS IWbemServices                ' // IWbemServices interface reference
           LOCAL bstrNetworkResource AS WSTRING                 ' // WMI namespace path
           LOCAL bstrObjectPath AS WSTRING                      ' // Object path
           LOCAL pIWbemClassObject AS IWbemClassObject          ' // Class object
           LOCAL wszMethodName AS WSTRINGZ * 260                ' // Method name
           LOCAL wszParamName AS WSTRINGZ * 260                 ' // Parameter name
           LOCAL pInParamsDefinition AS IWbemClassObject        ' // IWbemClassObject for IN parameters definition
           LOCAL pOutParamsDefinition AS IWbemClassObject       ' // IWbemClassObject for OUT parameters definition
           LOCAL pIWbemClassObjectInstance AS IWbemClassObject  ' // Object instance
           LOCAL vValue AS VARIANT                              ' // Variant value
           LOCAL vReturnValue AS VARIANT                        ' // Return value
        
           ' -------------------------------------------------------------------------------------
           ' Set the default security values for the process
           ' -------------------------------------------------------------------------------------
           hr = CoInitializeSecurity( _
                BYVAL %NULL, _                   ' // PSECURITY_DESCRIPTOR
                -1, _                            ' // COM authentication
                BYVAL %NULL, _                   ' // Authentication services
                %NULL, _                         ' // Reserved
                %RPC_C_AUTHN_LEVEL_DEFAULT, _    ' // Default authentication
                %RPC_C_IMP_LEVEL_IMPERSONATE, _  ' // Default Impersonation
                BYVAL %NULL, _                   ' // Authentication info
                %EOAC_NONE, _                    ' // Additional capabilities
                %NULL _                          ' // Reserved
                )
           IF hr <> %S_OK THEN
              AfxShowMsg("CoInitializeSecurity failed with an error code of &H" & HEX$(hr))
              EXIT FUNCTION
           END IF
           ' -------------------------------------------------------------------------------------
        
           ' -------------------------------------------------------------------------------------
           ' Create an instance of the IWbemLocator interface
           ' -------------------------------------------------------------------------------------
           pIWbemLocator = NEWCOM CLSID $CLSID_WbemLocator
           IF ISNOTHING(pIWbemLocator) THEN EXIT FUNCTION
           ' -------------------------------------------------------------------------------------
        
           ' -------------------------------------------------------------------------------------
           ' Connect with the specified machine.
           ' For  local access to the default namespace, use a simple object path:
           ' "root\default" or "\\.\root\default". For access to the default namespace
           ' on a remote computer include the computer name: "\\myserver\root\default".
           ' The computer name can also be a DNS name or IP address.
           ' -------------------------------------------------------------------------------------
           bstrNetworkResource = "root\default"
           hr = pIWbemLocator.ConnectServer( _
                bstrNetworkResource, _            ' // Object path of WMI namespace
                "", _                             ' // User name. NULL value = current user
                "", _                             ' // User password. NULL value  = current
                "", _                             ' // Locale. NULL value indicates current
                0, _                              ' // Security flags
                "", _                             ' // Authority (e.g. Kerberos)
                NOTHING, _                        ' // Context object
                pIWbemServices _                  ' // pointer to IWbemServices proxy
                )
           IF hr <> %S_OK THEN
              AfxShowMsg("IWbemLocator.ConnectServer failed with an error code of &H" & HEX$(hr) & $CRLF & WmiGetErrorCodeText(hr))
              EXIT FUNCTION
           END IF
           ' -------------------------------------------------------------------------------------
        
           ' -------------------------------------------------------------------------------------
           ' Retrieve a reference to the WMI "StdRegProv" class object
           ' -------------------------------------------------------------------------------------
           bstrObjectPath = "StdRegProv"
           hr = pIWbemServices.GetObject(bstrObjectPath, 0, NOTHING, pIWbemClassObject, NOTHING)
           IF hr <> %S_OK THEN EXIT FUNCTION
           ' -------------------------------------------------------------------------------------
        
           ' -------------------------------------------------------------------------------------
           ' Retrieve references to the IN and OUT parameters of the GetStringValue method
           ' -------------------------------------------------------------------------------------
           wszMethodName = "GetStringValue"
           hr = pIWbemClassObject.GetMethod(wszMethodName, 0, pInParamsDefinition, pOutParamsDefinition)
           IF hr <> %S_OK THEN EXIT FUNCTION
           ' -------------------------------------------------------------------------------------
        
           ' -------------------------------------------------------------------------------------
           ' Create a new instance of the class and set the values of the parameters
           ' -------------------------------------------------------------------------------------
           IF ISOBJECT(pInParamsDefinition) THEN
              hr = pInParamsDefinition.SpawnInstance(0, pIWbemClassObjectInstance)
              IF hr = %S_OK AND ISOBJECT(pIWbemClassObjectInstance) THEN
                 ' // Set the HKEY_LOCAL_MACHINE key
                 wszParamName = "hDefKey"
                 vValue = "2147483650"   ' must be decimal, not hex
                 hr = pIWbemClassObjectInstance.Put(wszParamName, 0, vValue, %CIM_UINT32)
                 ' // Set the subkey name
                 wszParamName = "sSubKeyName"
                 vValue = "Software\Microsoft\Windows NT\CurrentVersion"
                 hr = pIWbemClassObjectInstance.Put(wszParamName, 0, vValue, %CIM_STRING)
                 ' // Set the requested value
                 wszParamName = "sValueName"
                 vValue = "ProductName"
                 hr = pIWbemClassObjectInstance.Put(wszParamName, 0, vValue, %CIM_STRING)
              END IF
           END IF
           ' -------------------------------------------------------------------------------------
        
           ' -------------------------------------------------------------------------------------
           ' Execute the method
           ' -------------------------------------------------------------------------------------
           hr = pIWbemServices.ExecMethod(bstrObjectPath, wszMethodName, _
                0, NOTHING, pIWbemClassObjectInstance, pOutParamsDefinition, NOTHING)
           IF hr <> %S_OK THEN AfxShowMsg("ExecMethod hr = " & HEX$(hr))
           IF hr = %S_OK AND ISOBJECT(pOutParamsDefinition) THEN
              wszParamName = "sValue"
              hr = pOutParamsDefinition.Get(wszParamName, 0, vReturnValue, BYVAL %NULL, BYVAL %NULL)
              IF hr THEN
                 AfxShowMsg("IWbemClassObject.Get sValue Error - hr = " & HEX$(hr) & $CRLF & WmiGetErrorCodeText(hr))
              ELSE
                 AfxShowMsg("sValue = " & VARIANT$(vReturnValue))
              END IF
           END IF
           ' -------------------------------------------------------------------------------------
        
        END FUNCTION
        Forum: http://www.jose.it-berater.org/smfforum/index.php

        Comment


        • #5
          WMI: Determine whether a computer has a DVD drive

          Determines whether a computer has a DVD drive.

          Code:
          ' ========================================================================================
          ' Determine whether a computer has a DVD drive
          ' ========================================================================================
          
          #COMPILE EXE
          #DIM ALL
          #INCLUDE "WMI.INC"
          
          ' ========================================================================================
          ' Main
          ' ========================================================================================
          FUNCTION PBMAIN
          
             LOCAL hr AS LONG                            ' // HRESULT
             LOCAL pServices AS ISWbemServices           ' // ISWbemServices interface
             LOCAL pObjectSet AS ISWbemObjectSet         ' // ISWbemObjectSet interface
             LOCAL pEnum AS IEnumVARIANT                 ' // IEnumVARIANT interface
             LOCAL bstrDisplayName AS WSTRING            ' // Display name
             LOCAL bstrQuery AS WSTRING                  ' // Query filter
             LOCAL vItem AS VARIANT                      ' // Collection's item
             LOCAL oItem AS DISPATCH                     ' // Collection's item
             LOCAL vRes AS VARIANT                       ' // Variant result
          
             ' Connect to WMI using a moniker
             bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
             pServices = WmiGetObject(bstrDisplayName)
             IF ISNOTHING(pServices) THEN EXIT FUNCTION
          
             TRY
                ' Execute a query to get a reference to the collection of running processes
                bstrQuery = "SELECT DeviceID, Description, Name FROM Win32_CDROMDrive"
                pObjectSet = pServices.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                IF ISNOTHING(pObjectSet) THEN EXIT TRY
                ' Retrieve a reference to the collection's enumerator
                pEnum = pObjectSet.NewEnum_
                IF ISNOTHING(pEnum) THEN EXIT TRY
                ' Iterate through the collection of objects (WMI collections are 1 based).
                DO
                   ' Retrieve a reference to the next object in the collection
                   hr = pEnum.Next(1, vItem, BYVAL %NULL)
                   IF hr <> %S_OK THEN EXIT DO
                   ' Assign the VT_DISPATCH variant to the object variable
                   oItem = vItem : vItem = EMPTY
                   IF ISNOTHING(oItem) THEN EXIT DO
                   ' Retrieve the properties
                   OBJECT GET oItem.DeviceID TO vRes
                   ? "Device ID = " & VARIANT$$(vRes)
                   OBJECT GET oItem.Description TO vRes
                   ? "Description = " & VARIANT$$(vRes)
                   OBJECT GET oItem.Name TO vRes
                   ? "Name = " & VARIANT$$(vRes)
                   ' Release the object
                   oItem = NOTHING
                LOOP
             CATCH
                AfxShowMsg("Error " & HEX$(OBJRESULT) & $CRLF & WmiGetErrorCodeText(OBJRESULT))
             FINALLY
                ' Release the enumerator
                pEnum = NOTHING
                ' Release the collection
                pObjectSet = NOTHING
             END TRY
          
             #IF %DEF(%PB_CC32)
                WAITKEY$
             #ENDIF
          
          END FUNCTION
          ' ========================================================================================
          Same as above, but using the low-level API:

          Code:
          ' ========================================================================================
          ' Determine whether a computer has a DVD drive
          ' ========================================================================================
          
          #COMPILE EXE
          #DIM ALL
          #INCLUDE "WMI.INC"
          
          ' ========================================================================================
          ' Main
          ' ========================================================================================
          FUNCTION PBMAIN
          
             LOCAL hr AS LONG                                     ' // HRESULT
             LOCAL pIWbemLocator AS IWbemLocator                  ' // IWbemLocator interface reference
             LOCAL pIWbemServices AS IWbemServices                ' // IWbemServices interface reference
             LOCAL pIEnumWbemClassObject AS IEnumWbemClassObject  ' // IEnumWbemClassObject collection reference
             LOCAL pIWbemClassObject AS IWbemClassObject          ' // IWbemClassObject interface reference
             LOCAL bstrNetworkResource AS WSTRING                 ' // WMI namespace path
             LOCAL bstrLanguage AS WSTRING                        ' // Query language
             LOCAL bstrQuery AS WSTRING                           ' // Query filter
             LOCAL wszName AS WSTRINGZ * 260                      ' // Name of the Property
             LOCAL puReturned AS DWORD                            ' // Number of returned references
             LOCAL vRes AS VARIANT                                ' // General purpose variamt
          
             ' -------------------------------------------------------------------------------------
             ' Set the default security values for the process
             ' -------------------------------------------------------------------------------------
             hr = CoInitializeSecurity( _
                  BYVAL %NULL, _                   ' // PSECURITY_DESCRIPTOR
                  -1, _                            ' // COM authentication
                  BYVAL %NULL, _                   ' // Authentication services
                  %NULL, _                         ' // Reserved
                  %RPC_C_AUTHN_LEVEL_DEFAULT, _    ' // Default authentication
                  %RPC_C_IMP_LEVEL_IMPERSONATE, _  ' // Default Impersonation
                  BYVAL %NULL, _                   ' // Authentication info
                  %EOAC_NONE, _                    ' // Additional capabilities
                  %NULL _                          ' // Reserved
                  )
             IF hr <> %S_OK THEN
                AfxShowMsg("CoInitializeSecurity failed with an error code of &H" & HEX$(hr))
                EXIT FUNCTION
             END IF
             ' -------------------------------------------------------------------------------------
          
             ' -------------------------------------------------------------------------------------
             ' Create an instance of the IWbemLocator interface
             ' -------------------------------------------------------------------------------------
             pIWbemLocator = NEWCOM CLSID $CLSID_WbemLocator
             IF ISNOTHING(pIWbemLocator) THEN EXIT FUNCTION
             ' -------------------------------------------------------------------------------------
          
             ' -------------------------------------------------------------------------------------
             ' Connect with the specified machine
             ' -------------------------------------------------------------------------------------
             bstrNetworkResource = "root\cimv2"
             hr = pIWbemLocator.ConnectServer( _
                  bstrNetworkResource, _            ' // Object path of WMI namespace
                  "", _                             ' // User name. NULL value = current user
                  "", _                             ' // User password. NULL value = current
                  "", _                             ' // Locale. NULL value indicates current
                  0, _                              ' // Security flags
                  "", _                             ' // Authority (e.g. Kerberos)
                  NOTHING, _                        ' // Context object
                  pIWbemServices _                  ' // pointer to IWbemServices proxy
                  )
             IF hr <> %S_OK THEN
                AfxShowMsg("IWbemLocator.ConnectServer failed with an error code of &H" & HEX$(hr) & $CRLF & WmiGetErrorCodeText(hr))
                EXIT FUNCTION
             END IF
             ' -------------------------------------------------------------------------------------
          
             ' -------------------------------------------------------------------------------------
             ' Set security levels on the proxy
             ' -------------------------------------------------------------------------------------
             hr = CoSetProxyBlanket( _
                  pIWbemServices, _                ' // Indicates the proxy to set
                  %RPC_C_AUTHN_WINNT, _            ' // RPC_C_AUTHN_xxx
                  %RPC_C_AUTHZ_NONE, _             ' // RPC_C_AUTHZ_xxx
                  %NULL, _                         ' // Server principal name
                  %RPC_C_AUTHN_LEVEL_CALL, _       ' // RPC_C_AUTHN_LEVEL_xxx
                  %RPC_C_IMP_LEVEL_IMPERSONATE, _  ' // RPC_C_IMP_LEVEL_xxx
                  %NULL, _                         ' // client identity
                  %EOAC_NONE _                     ' // proxy capabilities
                  )
             IF hr <> %S_OK THEN
                AfxShowMsg("CoSetProxyBlanket failed with an error code of &H" & HEX$(hr))
                EXIT FUNCTION
             END IF
             ' -------------------------------------------------------------------------------------
          
             ' -------------------------------------------------------------------------------------
             ' Execute the query
             ' -------------------------------------------------------------------------------------
             bstrLanguage = "WQL"
             bstrQuery = "SELECT DeviceID, Description, Name FROM Win32_CDROMDrive"
             hr = pIWbemServices.ExecQuery( _
                bstrLanguage, _                    ' // Query language
                bstrQuery, _                       ' // Query string
                %WBEM_FLAG_FORWARD_ONLY OR _       ' // Forward-only enumerator
                %WBEM_FLAG_RETURN_IMMEDIATELY, _
                NOTHING, _                         ' // Typically NULL
                pIEnumWbemClassObject)             ' // Pointer to the enumerator
             IF hr <> %S_OK THEN
                AfxShowMsg("IWbemServices.ExecQuery failed with an error code of &H" & HEX$(hr) & $CRLF & WmiGetErrorCodeText(hr))
                EXIT FUNCTION
             END IF
             ' -------------------------------------------------------------------------------------
          
             ' -------------------------------------------------------------------------------------
             ' Enumerate the collection
             ' -------------------------------------------------------------------------------------
             DO
          
                hr = pIEnumWbemClassObject.Next( _
                     %WBEM_INFINITE, _               ' // Timeout
                     1, _                            ' // Number of requested objects
                     pIWbemClassObject, _            ' // Returned interface pointer
                     puReturned _                    ' // number of objects returned
                     )
                IF hr <> %S_OK THEN EXIT DO
          
                ' // Get the device identifier
                wszName = "DeviceID"
                hr = pIWbemClassObject.Get( _
                     wszName, _                      ' // Name of the desired property
                     0, _                            ' // Reserved. Must be 0.
                     vRes, _                         ' // Returned value for the qualifier
                     BYVAL %NULL, _                  ' // CIM type of the property
                     BYVAL %NULL _                   ' // Origin of the property
                     )
                IF hr <> %S_OK THEN
                   AfxShowMsg(WmiGetErrorCodeText(hr))
                   EXIT DO
                END IF
                AfxShowMsg("Device ID = " & VARIANT$(vRes))
          
                ' // Get the description
                wszName = "Description"
                hr = pIWbemClassObject.Get( _
                     wszName, _                      ' // Name of the desired property
                     0, _                            ' // Reserved. Must be 0.
                     vRes, _                         ' // Returned value for the qualifier
                     BYVAL %NULL, _                  ' // CIM type of the property
                     BYVAL %NULL _                   ' // Origin of the property
                     )
                IF hr <> %S_OK THEN
                   AfxShowMsg(WmiGetErrorCodeText(hr))
                   EXIT DO
                END IF
                AfxShowMsg("Description = " & VARIANT$(vRes))
          
                ' // Get the Name
                wszName = "Name"
                hr = pIWbemClassObject.Get( _
                     wszName, _                      ' // Name of the desired property
                     0, _                            ' // Reserved. Must be 0.
                     vRes, _                         ' // Returned value for the qualifier
                     BYVAL %NULL, _                  ' // CIM type of the property
                     BYVAL %NULL _                   ' // Origin of the property
                     )
                IF hr <> %S_OK THEN
                   AfxShowMsg(WmiGetErrorCodeText(hr))
                   EXIT DO
                END IF
                AfxShowMsg("Name = " & VARIANT$(vRes))
          
                ' // Release the collection's object
                pIWbemClassObject = NOTHING
          
             LOOP
             ' -------------------------------------------------------------------------------------
          
          END FUNCTION
          ' ========================================================================================
          Forum: http://www.jose.it-berater.org/smfforum/index.php

          Comment


          • #6
            WMI: Determine what kind of device is plugged into a USB port

            Determines what kind of device is plugged into a USB port.

            Code:
            ' ========================================================================================
            ' Determine what kind of device is plugged into a USB port
            ' ========================================================================================
            
            ' SED_PBCC - Use the PBCC compiler
            #COMPILE EXE
            #DIM ALL
            #INCLUDE "WMI.INC"
            
            ' ========================================================================================
            ' Main
            ' ========================================================================================
            FUNCTION PBMAIN
            
               LOCAL hr AS LONG                            ' // HRESULT
               LOCAL pServices AS ISWbemServices           ' // ISWbemServices interface
               LOCAL pObjectSet AS ISWbemObjectSet         ' // ISWbemObjectSet interface
               LOCAL pEnum AS IEnumVARIANT                 ' // IEnumVARIANT interface
               LOCAL bstrDisplayName AS WSTRING            ' // Display name
               LOCAL bstrQuery AS WSTRING                  ' // Query filter
               LOCAL vItem AS VARIANT                      ' // Collection's item
               LOCAL oItem AS DISPATCH                     ' // Collection's item
               LOCAL vRes AS VARIANT                       ' // Variant result
            
               ' Connect to WMI using a moniker
               bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2"
               pServices = WmiGetObject(bstrDisplayName)
               IF ISNOTHING(pServices) THEN EXIT FUNCTION
            
               TRY
                  ' Execute a query to get a reference to the collection of running processes
                  bstrQuery = "SELECT DeviceID, Description FROM Win32_USBHub"
                  pObjectSet = pServices.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                  IF ISNOTHING(pObjectSet) THEN EXIT TRY
                  ' Retrieve a reference to the collection's enumerator
                  pEnum = pObjectSet.NewEnum_
                  IF ISNOTHING(pEnum) THEN EXIT TRY
                  ' Iterate through the collection of objects (WMI collections are 1 based).
                  DO
                     ' Retrieve a reference to the next object in the collection
                     hr = pEnum.Next(1, vItem, BYVAL %NULL)
                     IF hr <> %S_OK THEN EXIT DO
                     ' Assign the VT_DISPATCH variant to the object variable
                     oItem = vItem : vItem = EMPTY
                     IF ISNOTHING(oItem) THEN EXIT DO
                     ' Retrieve the properties
                     OBJECT GET oItem.DeviceID TO vRes
                     STDOUT "PNP Device ID = " & VARIANT$(vRes)
                     OBJECT GET oItem.Description TO vRes
                     STDOUT "Description = " & VARIANT$(vRes)
                     ' Release the object
                     oItem = NOTHING
                  LOOP
               CATCH
                  STDOUT "Error " & HEX$(OBJRESULT)
                  STDOUT WmiGetErrorCodeText(OBJRESULT)
               FINALLY
                  ' Release the enumerator
                  pEnum = NOTHING
                  ' Release the collection
                  pObjectSet = NOTHING
               END TRY
            
               ' Cleanup
               pServices = NOTHING
            
               WAITKEY$
            
            END FUNCTION
            ' ========================================================================================
            Forum: http://www.jose.it-berater.org/smfforum/index.php

            Comment


            • #7
              WMI: Retrieve the address width of the processor

              Retrieves the address width of the processor. On a 32-bit operating system, the value is 32 and on a 64-bit operating system it is 64. This function can be used to determine if the processor is 32 or 64 bit.

              Code:
              ' ========================================================================================
              ' Retrieves the address width of the processor. On a 32-bit operating system, the value is
              ' 32 and on a 64-bit operating system it is 64.
              ' ========================================================================================
              FUNCTION AfxGetAddressWidth () AS WORD
              
                 LOCAL hr AS LONG                                  ' // HRESULT
                 LOCAL pService AS ISWbemServices                  ' // Services object
                 LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
                 LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
                 LOCAL bstrDisplayName AS WSTRING                  ' // Display name
                 LOCAL bstrQuery AS WSTRING                        ' // Query string
                 LOCAL oItem AS DISPATCH                           ' // Generic object variable
                 LOCAL vItem AS VARIANT                            ' // Generic object variant
                 LOCAL wAddressWidth AS WORD                       ' // uint16
              
                 ' // Connect to WMI using a moniker
                 bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2"
                 pService = WmiGetObject(bstrDisplayName)
                 IF ISNOTHING(pService) THEN EXIT FUNCTION
              
                 ' // Execute a query to get a reference to the collection of objects
                 bstrQuery = "SELECT AddressWidth FROM Win32_Processor"
                 pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                 IF ISNOTHING(pObjectSet) THEN EXIT FUNCTION
              
                 ' // Retrieve a reference to the collection's enumerator
                 pEnum = pObjectSet.NewEnum_
                 IF ISNOTHING(pEnum) THEN EXIT FUNCTION
                 hr = pEnum.Next(1, vItem, BYVAL %NULL)
                 IF hr <> %S_OK THEN EXIT FUNCTION
                 oItem = vItem
                 vItem = EMPTY
                 IF ISNOTHING(oItem) THEN EXIT FUNCTION
              
                 ' // Retrieve the value of the property
                 OBJECT GET oItem.AddressWidth TO wAddressWidth
                 FUNCTION = wAddressWidth
              
              END FUNCTION
              ' ========================================================================================
              Forum: http://www.jose.it-berater.org/smfforum/index.php

              Comment


              • #8
                WMI: Retrieve the system running on the Windows-based computer

                Code:
                ' ========================================================================================
                ' Retrieves the system running on the Windows-based computer. The following list identifies
                ' the returned value: "X86-based PC", "MIPS-based PC", "Alpha-based PC", "Power PC",
                ' "SH-x PC", "StrongARM PC", "64-bit Intel PC", "64-bit Alpha PC", "Unknown", "X86-Nec98 PC".
                ' ========================================================================================
                FUNCTION AfxGetSystemType () AS WSTRING
                
                   LOCAL hr AS LONG                                  ' // HRESULT
                   LOCAL pService AS ISWbemServices                  ' // Services object
                   LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
                   LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
                   LOCAL bstrDisplayName AS WSTRING                  ' // Display name
                   LOCAL bstrQuery AS WSTRING                        ' // Query string
                   LOCAL oItem AS DISPATCH                           ' // Generic object variable
                   LOCAL vItem AS VARIANT                            ' // Generic object variant
                   LOCAL bstrSystemType AS WSTRING                   ' // System type
                
                   ' // Connect to WMI using a moniker
                   bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2"
                   pService = WmiGetObject(bstrDisplayName)
                   IF ISNOTHING(pService) THEN EXIT FUNCTION
                
                   ' // Execute a query to get a reference to the collection of objects
                   bstrQuery = "SELECT SystemType FROM Win32_ComputerSystem"
                   pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                   IF ISNOTHING(pObjectSet) THEN EXIT FUNCTION
                
                   ' // Retrieve a reference to the collection's enumerator
                   pEnum = pObjectSet.NewEnum_
                   IF ISNOTHING(pEnum) THEN EXIT FUNCTION
                   hr = pEnum.Next(1, vItem, BYVAL %NULL)
                   IF hr <> %S_OK THEN EXIT FUNCTION
                   oItem = vItem
                   vItem = EMPTY
                   IF ISNOTHING(oItem) THEN EXIT FUNCTION
                
                   ' // Retrieve the value of the property
                   OBJECT GET oItem.SystemType TO bstrSystemType
                   FUNCTION = bstrSystemType
                
                END FUNCTION
                ' ========================================================================================
                Forum: http://www.jose.it-berater.org/smfforum/index.php

                Comment


                • #9
                  WMI: Retrieve the OS architecture

                  Code:
                  ' ========================================================================================
                  ' Retrieves the architecture of the operating system, as opposed to the processor.
                  ' Example: 32-bit. Windows Server 2003, Windows 2000, Windows NT 4.0, Windows XP, and
                  ' Windows Me/98/95: This property is not available.
                  ' ========================================================================================
                  FUNCTION AfxGetOSArchitecture () AS WSTRING
                  
                     LOCAL hr AS LONG                                  ' // HRESULT
                     LOCAL pService AS ISWbemServices                  ' // Services object
                     LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
                     LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
                     LOCAL bstrDisplayName AS WSTRING                  ' // Display name
                     LOCAL bstrQuery AS WSTRING                        ' // Query string
                     LOCAL oItem AS DISPATCH                           ' // Generic object variable
                     LOCAL vItem AS VARIANT                            ' // Generic object variant
                     LOCAL bstrArchitecture AS WSTRING                 ' // Architecture
                  
                     ' // Connect to WMI using a moniker
                     bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2"
                     pService = WmiGetObject(bstrDisplayName)
                     IF ISNOTHING(pService) THEN EXIT FUNCTION
                  
                     ' // Execute a query to get a reference to the collection of objects
                     bstrQuery = "SELECT OSArchitecture FROM Win32_OperatingSystem"
                     pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                     IF ISNOTHING(pObjectSet) THEN EXIT FUNCTION
                  
                     ' // Retrieve a reference to the collection's enumerator
                     pEnum = pObjectSet.NewEnum_
                     IF ISNOTHING(pEnum) THEN EXIT FUNCTION
                     hr = pEnum.Next(1, vItem, BYVAL %NULL)
                     IF hr <> %S_OK THEN EXIT FUNCTION
                     oItem = vItem
                     vItem = EMPTY
                     IF ISNOTHING(oItem) THEN EXIT FUNCTION
                  
                     ' // Retrieve the value of the property
                     OBJECT GET oItem.OSArchitecture TO bstrArchitecture
                     FUNCTION = bstrArchitecture
                  
                  END FUNCTION
                  ' ========================================================================================
                  Forum: http://www.jose.it-berater.org/smfforum/index.php

                  Comment


                  • #10
                    WMI: Retrieve the type of the computer in use

                    Code:
                    ' ========================================================================================
                    ' Retrieves the type of the computer in use, such as laptop, desktop, or Tablet.
                    ' Windows Server 2003, Windows XP, Windows 2000, Windows NT 4.0, and Windows Me/98/95:
                    ' This property is not available.
                    ' Value   Meaning
                    ' ------- --------------------------------------------¡
                    ' 0 (&H0) Unspecified
                    ' 1 (&H1) Desktop
                    ' 2 (&H2) Mobile
                    ' 3 (&H3) Workstation
                    ' 4 (&H4) Enterprise Server
                    ' 5 (&H5) Small Office and Home Office (SOHO) Server
                    ' 6 (&H6) Appliance PC
                    ' 7 (&H7) Performance Server
                    ' 8 (&H8) Maximum
                    ' ========================================================================================
                    FUNCTION AfxGetPCSystemType () AS WORD
                    
                       LOCAL hr AS LONG                                  ' // HRESULT
                       LOCAL pService AS ISWbemServices                  ' // Services object
                       LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
                       LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
                       LOCAL bstrDisplayName AS WSTRING                  ' // Display name
                       LOCAL bstrQuery AS WSTRING                        ' // Query string
                       LOCAL oItem AS DISPATCH                           ' // Generic object variable
                       LOCAL vItem AS VARIANT                            ' // Generic object variant
                       LOCAL wPCSystemType AS WORD                       ' // uint16
                    
                       ' // Connect to WMI using a moniker
                       bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2"
                       pService = WmiGetObject(bstrDisplayName)
                       IF ISNOTHING(pService) THEN EXIT FUNCTION
                    
                       ' // Execute a query to get a reference to the collection of objects
                       bstrQuery = "SELECT PCSystemType FROM Win32_ComputerSystem"
                       pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                       IF ISNOTHING(pObjectSet) THEN EXIT FUNCTION
                    
                       ' // Retrieve a reference to the collection's enumerator
                       pEnum = pObjectSet.NewEnum_
                       IF ISNOTHING(pEnum) THEN EXIT FUNCTION
                       hr = pEnum.Next(1, vItem, BYVAL %NULL)
                       IF hr <> %S_OK THEN EXIT FUNCTION
                       oItem = vItem
                       vItem = EMPTY
                       IF ISNOTHING(oItem) THEN EXIT FUNCTION
                    
                       ' // Retrieve the value of the property
                       OBJECT GET oItem.PCSystemType TO wPCSystemType
                       FUNCTION = wPCSystemType
                    
                    END FUNCTION
                    ' ========================================================================================
                    Forum: http://www.jose.it-berater.org/smfforum/index.php

                    Comment


                    • #11
                      WMI: Check if the computer is part of a domain

                      Code:
                      ' ========================================================================================
                      ' Checks if the computer is part of a domain.
                      ' Return value: If TRUE, the computer is part of a domain. If FALSE, the computer is not
                      ' in a domain or the status is unknown. If you unjoin the computer from a domain, the
                      ' value becomes false.
                      ' ========================================================================================
                      FUNCTION AfxIsPartOfDomain () AS BYTE
                      
                         LOCAL hr AS LONG                                  ' // HRESULT
                         LOCAL pService AS ISWbemServices                  ' // Services object
                         LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
                         LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
                         LOCAL bstrDisplayName AS WSTRING                  ' // Display name
                         LOCAL bstrQuery AS WSTRING                        ' // Query string
                         LOCAL oItem AS DISPATCH                           ' // Generic object variable
                         LOCAL vItem AS VARIANT                            ' // Generic object variant
                         LOCAL bPartOfDomain AS BYTE                       ' // boolean
                      
                         ' // Connect to WMI using a moniker
                         bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2"
                         pService = WmiGetObject(bstrDisplayName)
                         IF ISNOTHING(pService) THEN EXIT FUNCTION
                      
                         ' // Execute a query to get a reference to the collection of objects
                         bstrQuery = "SELECT PartOfDomain FROM Win32_ComputerSystem"
                         pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                         IF ISNOTHING(pObjectSet) THEN EXIT FUNCTION
                      
                         ' // Retrieve a reference to the collection's enumerator
                         pEnum = pObjectSet.NewEnum_
                         IF ISNOTHING(pEnum) THEN EXIT FUNCTION
                         hr = pEnum.Next(1, vItem, BYVAL %NULL)
                         IF hr <> %S_OK THEN EXIT FUNCTION
                         oItem = vItem
                         vItem = EMPTY
                         IF ISNOTHING(oItem) THEN EXIT FUNCTION
                      
                         ' // Retrieve the value of the property
                         OBJECT GET oItem.PartOfDomain TO bPartOfDomain
                         FUNCTION = bPartOfDomain
                      
                      END FUNCTION
                      ' ========================================================================================
                      Forum: http://www.jose.it-berater.org/smfforum/index.php

                      Comment


                      • #12
                        WMI: Retrieve the name of the domain or workgroup to which a computer belongs

                        Code:
                        ' ========================================================================================
                        ' Name of the domain or workgroup to which a computer belongs.
                        ' If the computer is not part of a domain, then the name of the workgroup is returned.
                        ' If you need to run in both domain-based and workgroup-based environments, you might
                        ' encounter problems using the %USERDOMAIN% environment variable because if the computer
                        ' is not part of a domain, it returns the name of the local computer instead of the
                        ' name of the workgroup.
                        ' ========================================================================================
                        FUNCTION AfxGetDomainOrWorkGroup () AS WSTRING
                        
                           LOCAL hr AS LONG                                  ' // HRESULT
                           LOCAL pService AS ISWbemServices                  ' // Services object
                           LOCAL pObjectSet AS ISWbemObjectSet               ' // ISWbemObjectSet interface
                           LOCAL pEnum AS IEnumVariant                       ' // Generic collection's enumerator reference
                           LOCAL bstrDisplayName AS WSTRING                  ' // Display name
                           LOCAL bstrQuery AS WSTRING                        ' // Query string
                           LOCAL oItem AS DISPATCH                           ' // Generic object variable
                           LOCAL vItem AS VARIANT                            ' // Generic object variant
                           LOCAL bstrDomainOrWorkGroup AS WSTRING            ' // Domain or workgroup name
                        
                           ' // Connect to WMI using a moniker
                           bstrDisplayName = "winmgmts:{impersonationLevel=impersonate}!\\.\root\CIMV2"
                           pService = WmiGetObject(bstrDisplayName)
                           IF ISNOTHING(pService) THEN EXIT FUNCTION
                        
                           ' // Execute a query to get a reference to the collection of objects
                           bstrQuery = "SELECT Domain FROM Win32_ComputerSystem"
                           pObjectSet = pService.ExecQuery(bstrQuery, "WQL", %wbemFlagReturnImmediately)
                           IF ISNOTHING(pObjectSet) THEN EXIT FUNCTION
                        
                           ' // Retrieve a reference to the collection's enumerator
                           pEnum = pObjectSet.NewEnum_
                           IF ISNOTHING(pEnum) THEN EXIT FUNCTION
                           hr = pEnum.Next(1, vItem, BYVAL %NULL)
                           IF hr <> %S_OK THEN EXIT FUNCTION
                           oItem = vItem
                           vItem = EMPTY
                           IF ISNOTHING(oItem) THEN EXIT FUNCTION
                        
                           ' // Retrieve the value of the property
                           OBJECT GET oItem.Domain TO bstrDomainOrWorkGroup
                           FUNCTION = bstrDomainOrWorkGroup
                        
                        END FUNCTION
                        ' ========================================================================================
                        Forum: http://www.jose.it-berater.org/smfforum/index.php

                        Comment

                        Working...
                        X