Representing Objects in XML

The XML encoder component in WMI generates XML representations of objects.

In C++, you can start the XML encoder with a call to the IWbemObjectTextSrc.GetText method, specifying the object to be represented in XML and the text format to use in the representation. For more information and a code example, see To encode an object in XML using C/C++.

In VBScript or Visual Basic, to encode data for an XML instance, call SWbemObjectEx.GetText. For more information and a code example, see To encode an object in XML using VBScript.

The following sections are discussed in this topic:

Encode an Object Using C or C++

The following procedure describes how to encode an object in XML using C or C++.

Aa393065.wedge(en-us,VS.85).gifTo encode an object in XML using C or C++

  1. Set up your program to access WMI data.

    Because WMI is based on COM technology, you must perform the requisite calls to the CoInitializeEx and CoInitializeSecurity functions to access WMI. For more information, see Initializing COM for a WMI Application.

  2. Optionally, create an IWbemContext object and initialize it.

    You do not need to create the IWbemContext object unless you need to change the default operation. The context object is used by the XML encoder to control the amount of information included in the object's XML representation.

    The following table lists the optional values that can be specified for the context object.

    Value/typeMeaning
    "LocalOnly" VT_BOOLWhen TRUE, only properties and methods defined locally in the class are present in the resulting XML. The default value is FALSE.
    "IncludeQualifiers" VT_BOOLWhen TRUE, class, instance, properties, and method qualifiers are included in the resulting XML. The default value is FALSE.
    "ExcludeSystemProperties" VT_BOOLWhen TRUE, WMI system properties are filtered out of the output. The default value is FALSE.
    "PathLevel" VT_I4
    0 = A <CLASS> or <INSTANCE> element is generated.
    1 = A <VALUE.NAMEDOBJECT> element is generated.
    2 = A <VALUE.OBJECTWITHLOCALPATH> element is generated.
    3 = A <VALUE.OBJECTWITHPATH> is generated.

    The default is 0 (zero).

     

    The following code example shows how the context object is initialized to include qualifiers and exclude system properties.

    
    VARIANT vValue;
    IWbemContext *pContext = NULL;
    HRESULT hr = CoCreateInstance (CLSID_WbemContext, 
                           NULL, 
                           CLSCTX_INPROC_SERVER,
                           IID_IWbemContext, 
                           (void**) &pContext);
    if (FAILED(hr))
    {
      printf("Create context failed with %x\n", hr);
      return hr;
    }
    
    // Generate a <VALUE.OBJECTWITHLOCALPATH> element
    VariantInit(&vValue);
    vValue.vt = VT_I4;
    vValue.lVal = 2;
    pContext->SetValue(L"PathLevel", 0, &vValue);
    VariantClear(&vValue);
    
    // Include qualifiers
    VariantInit(&vValue);
    vValue.vt = VT_BOOL;
    vValue.boolVal = VARIANT_TRUE;
    pContext->SetValue(L"IncludeQualifiers", 0, &vValue);
    VariantClear(&vValue);
    
    // Exclude system properties
    VariantInit(&vValue);
    vValue.vt = VT_BOOL;
    vValue.boolVal = VARIANT_TRUE;
    pContext->SetValue(L"ExcludeSystemProperties", 0, &vValue);
    VariantClear(&vValue);
    
    
  3. Get a reference to the class or instance to encode in XML.

    After you have initialized COM and are connected to WMI, call GetObject to retrieve a reference to the specified class or instance. If you used a BSTR to specify the class or instance, call SysFreeString to free up the memory allocated by SysAllocString.

    The following code example retrieves a reference to an Win32_LogicalDisk instance.

    
    HRESULT hr = NULL;
    IWbemClassObject *pClass = NULL;
    BSTR strObj = SysAllocString(L"Win32_LogicalDisk");
    
    hr = pConnection->GetObject(strObj, 
                                0, 
                                NULL, 
                                &pClass, 
                                NULL);
    
    SysFreeString(strObj);
    if (FAILED(hr))
    {
      printf("GetObject failed with %x\n",hr)
      return hr;
    }
    
    
  4. Create an IWbemObjectTextSrc object.

    After you have a reference to an object you must create the IWbemObjectTextSrc object with a call to CoCreateInstance. The IWbemObjectTextSrc object is used to generate the actual XML text.

    The following code example shows how to create an IWbemObjectTextSrc object by calling CoCreateInstance.

    
    HRESULT hr = NULL;
    IWbemObjectTextSrc *pSrc = NULL;
    
    hr = CoCreateInstance (CLSID_WbemObjectTextSrc, 
                           NULL,
                           CLSCTX_INPROC_SERVER,
                           IID_IWbemObjectTextSrc, 
                           (void**) &pSrc);
    
    if (FAILED(hr))
    {
        printf("CoCreateInstance of IWbemObjectTextSrc failed %x\n",hr);
        return hr;
    }
    
    
  5. Invoke the IWbemObjectTextSrc.GetText method to get an XML representation of an object.

    After setting the context for the object's representation, getting a reference to the object, and creating an IWbemObjectTextSrc object, you are ready to generate an XML representation of the specified object by calling the IWbemObjectTextSrc.GetText method.

    The following C++ example code generates an XML representation of the object referenced by pClass. The XML representation is returned in strText. The third parameter of GetText specifies the text format to be used for the XML and must be either WMI_OBJ_TEXT_CIM_DTD_2_0 (0x1) or WMI_OBJ_TEXT_WMI_DTD_2_0 (0x2). For more information about these values, see IWbemObjectTextSrc::GetText Parameter Values.

    
    HRESULT hr = NULL;
    BSTR strText = NULL;
    hr = pSrc->GetText(0, 
                       pClass, 
                       WMI_OBJ_TEXT_CIM_DTD_2_0,
                       pContext, 
                       &strText);
    
    // Perform a task with strText
    SysFreeString(strText);
    
    if (FAILED(hr))
    {
      printf("GetText failed with %x\n", hr);
      return hr;
    }
    
    

The following C++ example code includes all of the steps in the previous procedure and encodes the Win32_LogicalDisk class in XML including class, property, and method qualifiers and excluding system properties.



// The following #define statement is needed so that 
// the proper values are loaded by the #include files.
#define _WIN32_WINNT 0x0500

#include <stdio.h>
#include <wbemcli.h>
# pragma comment(lib, "wbemuuid.lib")

// Initialize the context object
// ---------------------------------------------
void FillUpContext(IWbemContext *pContext)
{
  VARIANT vValue;

  // IncludeQualifiers
  VariantInit(&vValue);
  vValue.vt = VT_BOOL;
  vValue.boolVal = VARIANT_FALSE;
  pContext->SetValue(L"IncludeQualifiers", 0, &vValue);
  VariantClear(&vValue);

  VariantInit(&vValue);
  vValue.vt = VT_I4;
  vValue.lVal = 0;
  pContext->SetValue(L"PathLevel", 0, &vValue);
  VariantClear(&vValue);

  // ExcludeSystemProperties
  VariantInit(&vValue);
  vValue.vt = VT_BOOL;
  vValue.boolVal = VARIANT_TRUE;
  pContext->SetValue(L"ExcludeSystemProperties", 0, &vValue);
  VariantClear(&vValue);
}

// Main method ... drives the program
// ---------------------------------------------
int _cdecl main(int argc, char * argv[])
{
  BSTR strNs = NULL;
  BSTR strObj = NULL;
  BSTR strText = NULL;

  if(FAILED(CoInitialize(NULL)))
    return 1;
  HRESULT hr = E_FAIL;
  IWbemObjectTextSrc *pSrc = NULL;

  IWbemLocator *pL = NULL;
  if(SUCCEEDED(hr = CoCreateInstance (CLSID_WbemLocator, 
                                      NULL, 
                                      CLSCTX_INPROC_SERVER,
                                      IID_IWbemLocator, 
                                      (void**) &pL)))
  {
    // Create a context object
    IWbemContext *pContext = NULL;
    if(SUCCEEDED(hr = CoCreateInstance (CLSID_WbemContext, 
                                        NULL, 
                                        CLSCTX_INPROC_SERVER,
                                        IID_IWbemContext, 
                                        (void**) &pContext)))
    {
      FillUpContext(pContext);
      IWbemServices *pConnection = NULL;
      strNs = SysAllocString(L"root\\cimv2");
      strText = NULL;
      if(SUCCEEDED(hr = pL->ConnectServer(strNs, 
                                          NULL, 
                                          NULL, 
                                          NULL, 
                                          0, 
                                          NULL, 
                                          NULL, 
                                          &pConnection)))
      {
        IWbemClassObject *pClass = NULL;
        strObj = SysAllocString(L"Win32_LogicalDisk");

        if(SUCCEEDED(hr = CoCreateInstance (CLSID_WbemObjectTextSrc, 
                                            NULL,
                                            CLSCTX_INPROC_SERVER,
                                            IID_IWbemObjectTextSrc, 
                                            (void**) &pSrc)))
        {
          // Test for GetObject
          if(SUCCEEDED(hr = pConnection->GetObject(strObj, 
                                                   0, 
                                                   NULL, 
                                                   &pClass, 
                                                   NULL)))
          {
            if(SUCCEEDED(hr = pSrc->GetText(0, 
                                            pClass, 
                                            WMI_OBJ_TEXT_CIM_DTD_2_0,
                                            pContext, 
                                            &strText)))
            {
              printf("GETOBJECT SUCCEEDED\n");
              printf("==========================================\n");
              wprintf(strText);
              pContext->Release();
              pClass->Release();
            }
            else
            {
              printf("GetText failed with %x\n", hr);
              // Free up the object
              pContext->Release();
              pClass->Release();
            }
          }
          else
            printf("GetObject failed with %x\n", hr);
        }
        else
          printf("CoCreateInstance on WbemObjectTextSrc failed with %x\n", hr);
      }
      else
        printf("ConnectServer on root\\cimv2 failed with %x\n", hr);
    }
    else
      printf("CoCreateInstance on Context failed with %x\n", hr);
  }
  else
    printf("CoCreateInstance on Locator failed with %x\n", hr);

// Clean up memory
  if (strNs != NULL)
    SysFreeString(strNs);
  if (strObj != NULL)
    SysFreeString(strObj);
  if (strText != NULL)
    SysFreeString(strText);
  if (pSrc != NULL)
    pSrc->Release();
  if (pL != NULL)
    pL->Release();
  return 0;
}


Encode an Object Using VBScript

The following procedure describes how to encode an object in XML using VBScript.

Aa393065.wedge(en-us,VS.85).gifTo encode an object in XML using VBScript

  1. Optionally, create an SWbemNamedValueSet object and initialize it so as to set the context values required for the XML representation.

    The following code example shows how the values instruct the XML encoder to generate a <VALUE.OBJECTWITHLOCALPATH> element, including all of the qualifiers and excluding system properties when it constructs the XML representation of the object.

    ' Create an optional SWbemNamedValueSet object
    set context = CreateObject("wbemscripting.SWbemNamedValueSet")
    
    ' Initialize the value set object to set the context
    ' Generate a <VALUE.OBJECTWITHLOCALPATH> element
    context.add "PathLevel", 2 
    context.add "IncludeQualifiers", true 
    context.add "ExcludeSystemProperties", true '
    
  2. Retrieve an instance of the object or class to represent in XML.

    The following code example retrieves an instance of the object.

    ' Retrieve the object/class to be represented in XML
    set myObj = GetObject("winmgmts:\\.\root\cimv2:win32_LogicalDisk")
    
  3. Invoke the GetText_ method of the instance created in the preceding step, using 1 as the parameter to specify the text format that corresponds to CIM DTD version 2.0, or 2 to specify the text format that corresponds to WMI DTD version 2.0. If you created the SWbemNamedValueSet object in Step 1, include it in the parameter list as the third parameter.

    The following code example shows how to invoke the GetText_ method of the instance.

    ' Get the XML representation of the object
    strText = myObj.GetText_(2,,context)
    
    ' If you have not used a SWbemNamedValueSet object
    ' enter the following line.
    strText = myObj.GetText_(2)
    
  4. Optionally, validate that the XML generated in Step 3 is well-formed XML, by creating and initializing an XML Document Object Model (DOM) object and then load the XML text into it.

    The following code example shows how to create and initialize an XML DOM object and load the XML text into it.

    ' Create an XMLDOM object
    set xml = CreateObject("Microsoft.xmldom")
    
    ' Initialize the XMLDOM object so results are returned synchronously
    xml.async = false
    
    ' Load the XML into the XMLDOM object
    xml.loadxml strText
    
  5. Output the XML representation of the object.

    The following code example shows how to use wscript to output the XML.

    wscript.echo strText
    

    If you chose to use XML DOM to verify that the XML generated is well-formed, replace the previous line with the following.

    wscript.echo xml.xml
    

The following VBScript code example includes all of the steps in the preceding procedure and encodes the Win32_LogicalDisk class in XML, including class, property, and method qualifiers and excluding system properties.


' Create optional SWbemNamedValueSet object
set context = CreateObject("Wbemscripting.SWbemNamedValueSet")

' Initialize the context object
context.add "PathLevel", 2
context.add "IncludeQualifiers", true
context.add "ExcludeSystemProperties", true

' Retrieve the object/class to be represented in XML
set myObj = GetObject("winmgmts:\\.\root\cimv2:Win32_LogicalDisk")

' Get the XML representation of the object
strText = myObj.GetText_(2,,context)
' If you have not used a SWbemNamedValueSet object 
'   use the following line
'   strText = myObj.GetText(2)

' Print the XML representation
wscript.echo strText

' If you choose to use the XML DOM to verify 
'   that the XML generated is well-formed, replace the last
'   line in the above code example with the following lines:

' Create an XMLDOM object
set xml = CreateObject("Microsoft.xmldom")

' Initialize the XMLDOM object so results are 
'   returned synchronously
xml.async = false

' Load the XML into the XMLDOM object
xml.loadxml strText

' Print the XML representation
wscript.echo xml.xml

Related topics

Using WMI

 

 

Show:
© 2014 Microsoft. All rights reserved.