在 XML 中表示物件
WMI 中的 XML 編碼器元件會產生 物件的 XML 表示法。
在C++中,您可以使用呼叫 IWbemObjectTextSrc.GetText 方法來啟動 XML 編碼器,並指定要以 XML 表示的物件,以及要用於表示法的文字格式。 如需詳細資訊和程式代碼範例,請參閱使用 C/C++ 在 XML 中編碼物件。
在 VBScript 或 Visual Basic 中,若要編碼 XML 實例的數據,請呼叫 SWbemObjectEx.GetText。 如需詳細資訊和程式代碼範例,請參閱使用 VBScript 在 XML 中編碼物件。
本主題將討論下列各節:
- 使用 C 或 C++ 編碼物件
- 使用 VBScript 編碼物件
- 相關主題
使用 C 或 C++ 編碼物件
下列程序說明如何使用 C 或 C++,在 XML 中編碼物件。
使用 C 或 C++ 在 XML 中編碼物件
設定程式以存取 WMI 數據。
由於 WMI 是以 COM 技術為基礎,因此您必須執行對 CoInitializeEx 和 CoInitializeSecurity 函式的必要呼叫,才能存取 WMI。 如需詳細資訊,請參閱 WMI 應用程式初始化 COM。
或者,建立 IWbemContext 物件並加以初始化。
除非您需要變更默認作業,否則不需要建立 IWbemContext 物件。 XML 編碼器會使用內容物件來控制物件 XML 表示中包含的資訊量。
下表列出可以為內容物件指定的選擇性值。
值/類型 意義 “LocalOnly” VT_BOOL 當 TRUE時,只有在此類別中定義的屬性和方法會出現在產生的 XML 中。 預設值 FALSE。 “IncludeQualifiers” VT_BOOL 當 TRUE時,產生的 XML 會包含類別、實例、屬性和方法限定符。 預設值 FALSE。 “ExcludeSystemProperties” VT_BOOL 當 TRUE時,WMI 系統屬性會從輸出中排除。 預設值 FALSE。 “PathLevel” VT_I4 - 0 = 產生 <CLASS> 或 <INSTANCE> 元素。
1 = 產生 <值 NAMEDOBJECT> 元素。
2 = <VALUE,已生成 OBJECTWITHLOCALPATH> 元素。
3 = <VALUE。會產生 OBJECTWITHPATH>。
下列程式代碼範例示範如何初始化內容物件,以包含限定符和排除系統屬性。
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);
取得類別或實例的參考,以 XML 編碼。
初始化 COM 並連接到 WMI 之後,請呼叫 GetObject,以擷取指定類別或實例的參考。 如果您使用 BSTR 來指定類別或實例,請呼叫 SysFreeString,以釋放 SysAllocString所配置的記憶體。
下列程式代碼範例會擷取 Win32_LogicalDisk 實例的參考。
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; }
建立 IWbemObjectTextSrc 物件。
取得物件的參考之後,您必須建立 IWbemObjectTextSrc 物件,並呼叫 CoCreateInstance。 IWbemObjectTextSrc 物件可用來產生實際的 XML 文字。
下列程式代碼範例示範如何呼叫 CoCreateInstance來建立 IWbemObjectTextSrc 物件。
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; }
叫用 IWbemObjectTextSrc.GetText 方法來取得物件的 XML 表示法。
設定物件表示法的內容、取得對象的參考,以及建立 IWbemObjectTextSrc 對象之後,您就可以呼叫 IWbemObjectTextSrc.GetText 方法來產生指定物件的 XML 表示法。
下列C++範例程式代碼會產生 pClass所參考物件的 XML 表示法。 XML 表示法會在 strText 中傳回。 GetText 的第三個參數會指定要用於 XML 的文字格式,而且必須是WMI_OBJ_TEXT_CIM_DTD_2_0(0x1)或WMI_OBJ_TEXT_WMI_DTD_2_0(0x2)。 如需這些值的詳細資訊,請參閱 IWbemObjectTextSrc::GetText 參數值。
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; }
下列C++範例程式代碼包含上一個程式中的所有步驟,並在 XML 中編碼 Win32_LogicalDisk 類別,包括類別、屬性和方法限定符,以及排除系統屬性。
// 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;
}
使用 VBScript 編碼物件
下列程式描述如何使用 VBScript 在 XML 中編碼物件。
使用 VBScript 在 XML 中編碼物件
選擇性地建立 SWbemNamedValueSet 物件,並將其初始化,以便設定 XML 表示所需的內容值。
下列程式碼範例說明數值如何指示 XML 編碼器生成 <VALUE.OBJECTWITHLOCALPATH> 元素,包括所有的限定符,並在構建物件的 XML 表示時排除系統屬性。
' 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 '
擷取物件或類別的實例,以 XML 表示。
下列程式代碼範例會擷取 對象的實例。
' Retrieve the object/class to be represented in XML set myObj = GetObject("winmgmts:\\.\root\cimv2:win32_LogicalDisk")
叫用在上一個步驟中建立之實例的 GetText_ 方法,使用 1 作為參數來指定對應至 CIM DTD 2.0 版的文字格式,或使用 2 來指定對應至 WMI DTD 2.0 版的文字格式。 如果您在步驟 1 中建立 SWbemNamedValueSet 物件,請將它納入參數清單中做為第三個參數。
下列程式代碼範例示範如何叫用 實例的 GetText_ 方法。
' 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)
或者,藉由建立和初始化 XML 檔物件模型 (DOM) 對象,然後將 XML 文字載入其中,以選擇性地驗證步驟 3 中產生的 XML 是格式正確的 XML。
下列程式代碼範例示範如何建立和初始化 XML DOM 物件,並將 XML 文字載入其中。
' 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
輸出物件的 XML 表示。
下列程式代碼範例示範如何使用 wscript 來輸出 XML。
wscript.echo strText
如果您選擇使用 XML DOM 來確認產生的 XML 格式良好,請將上一行取代為下列內容。
wscript.echo xml.xml
下列 VBScript 程式代碼範例包含上述程式中的所有步驟,並在 XML 中編碼 Win32_LogicalDisk 類別,包括類別、屬性和方法限定符,以及排除系統屬性。
' 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