Esempio: recupero di dati WMI dal computer locale
È possibile utilizzare la procedura e gli esempi di codice in questo argomento per creare un'applicazione client WMI completa che esegue l'inizializzazione COM, connettersi a WMI nel computer locale, recuperare i dati in modo semisyncincrono e quindi eseguire la pulizia. In questo esempio viene visualizzato il nome del sistema operativo nel computer locale. Per recuperare dati da un computer remoto, vedere Esempio: Recupero di dati WMI da un computer remoto. Per ottenere i dati in modo asincrono, vedere Esempio: Recupero di dati WMI dal computer locale in modo asincrono.
La procedura seguente viene utilizzata per eseguire l'applicazione WMI. I passaggi da 1 a 5 contengono tutti i passaggi necessari per configurare e connettersi a WMI e i passaggi 6 e 7 sono la posizione in cui i dati vengono sottoposti a query e ricevuti.
Inizializzare i parametri COM con una chiamata a CoInitializeEx.
Per altre informazioni, vedere inizializzazione di COM per un'applicazione WMI.
Inizializzare la sicurezza del processo COM chiamando CoInitializeSecurity.
Per altre informazioni, vedere Impostazione del livello di sicurezza del processo predefinito tramite C++.
Ottenere il localizzatore iniziale a WMI chiamando CoCreateInstance.
Per altre informazioni, vedere Creazione di una connessione a uno spazio dei nomi WMI.
Ottenere un puntatore a IWbemServices per lo spazio dei nomi root\cimv2 nel computer locale chiamando IWbemLocator::ConnectServer. Per connettersi a un computer remoto, vedere esempio: Recupero di dati WMI da un computer remoto.
Per altre informazioni, vedere Creazione di una connessione a uno spazio dei nomi WMI.
Impostare IWbemServices la sicurezza proxy in modo che il servizio WMI possa rappresentare il client chiamando CoSetProxyBlanket.
Per altre informazioni, vedere Impostazione dei livelli di sicurezza in una connessione WMI.
Usare il puntatoreIWbemServicesper effettuare richieste di WMI. In questo esempio viene eseguita una query per il nome del sistema operativo chiamando IWbemServices::ExecQuery.
La query WQL seguente è uno degli argomenti del metodo .
SELECT * FROM Win32_OperatingSystem
Il risultato di questa query viene archiviato in un puntatore IEnumWbemClassObject. In questo modo, gli oggetti dati della query possono essere recuperati in modo semisincrono con l'interfaccia IEnumWbemClassObject. Per altre informazioni, vedere Enumerating WMI. Per ottenere i dati in modo asincrono, vedere Esempio: Recupero di dati WMI dal computer locale in modo asincrono.
Per altre informazioni sull'esecuzione di richieste a WMI, vedere Modifica delle informazioni sulle classi e sulle istanze, l'esecuzione di query su WMIe chiamata di un metodo.
Ottenere e visualizzare i dati dalla query WQL. Il puntatore IEnumWbemClassObject è collegato agli oggetti dati restituiti dalla query e gli oggetti dati possono essere recuperati con il metodo IEnumWbemClassObject::Next. Questo metodo collega gli oggetti dati a un puntatoreIWbemClassObjectpassato al metodo . Usare il metodo IWbemClassObject::Get per ottenere le informazioni desiderate dagli oggetti dati.
L'esempio di codice seguente viene usato per ottenere la proprietà Name dall'oggetto dati, che fornisce il nome del sistema operativo.
VARIANT vtProp; VariantInit(&vtProp); // Get the value of the Name property hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
Dopo che il valore della proprietà Name viene archiviato nell'VARIANT variabile vtProp, può quindi essere visualizzato all'utente.
Per altre informazioni, vedere Enumerating WMI.
Nell'esempio di codice seguente vengono recuperati i dati WMI in modo semisynchrono da un computer locale.
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>
#pragma comment(lib, "wbemuuid.lib")
int main(int argc, char **argv)
{
HRESULT hres;
// Step 1: --------------------------------------------------
// Initialize COM. ------------------------------------------
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1; // Program has failed.
}
// Step 2: --------------------------------------------------
// Set general COM security levels --------------------------
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 3: ---------------------------------------------------
// Obtain the initial locator to WMI -------------------------
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1; // Program has failed.
}
// Step 4: -----------------------------------------------------
// Connect to WMI through the IWbemLocator::ConnectServer method
IWbemServices *pSvc = NULL;
// Connect to the root\cimv2 namespace with
// the current user and obtain pointer pSvc
// to make IWbemServices calls.
hres = pLoc->ConnectServer(
_bstr_t(L"ROOT\\CIMV2"), // Object path of WMI namespace
NULL, // User name. NULL = current user
NULL, // User password. NULL = current
0, // Locale. NULL indicates current
NULL, // Security flags.
0, // Authority (for example, Kerberos)
0, // Context object
&pSvc // pointer to IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
// Step 5: --------------------------------------------------
// Set security levels on the proxy -------------------------
hres = CoSetProxyBlanket(
pSvc, // 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 (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 6: --------------------------------------------------
// Use the IWbemServices pointer to make requests of WMI ----
// For example, get the name of the operating system
IEnumWbemClassObject* pEnumerator = NULL;
hres = pSvc->ExecQuery(
bstr_t("WQL"),
bstr_t("SELECT * FROM Win32_OperatingSystem"),
WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
NULL,
&pEnumerator);
if (FAILED(hres))
{
cout << "Query for operating system name failed."
<< " Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1; // Program has failed.
}
// Step 7: -------------------------------------------------
// Get the data from the query in step 6 -------------------
IWbemClassObject *pclsObj = NULL;
ULONG uReturn = 0;
while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);
if(0 == uReturn)
{
break;
}
VARIANT vtProp;
VariantInit(&vtProp);
// Get the value of the Name property
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << " OS Name : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
pclsObj->Release();
}
// Cleanup
// ========
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
CoUninitialize();
return 0; // Program successfully completed.
}