You can use the procedure and code examples in this topic to create a complete WMI client application that performs COM initialization, connects to WMI on a remote computer, gets data semisynchronously, and then cleans up. For more information about how to get data from the local computer, see Example: Getting WMI Data from the Local Computer. For more information about how to get the data asynchronously, see Example: Getting WMI Data from the Local Computer Asynchronously.
Note
If you are trying to connect to a remote computer refer to the information in Connecting to WMI Remotely.
The following procedure shows how to execute the WMI application. Steps 1 through 5 contain all of the steps required to set up and connect to WMI, and steps 6 and 7 are where data is queried and received.
To get WMI data from a remote computer
Initialize COM parameters with a call to CoInitializeEx.
Obtain a pointer to IWbemServices for the \\root\cimv2 namespace on a remote computer by calling IWbemLocator::ConnectServer. When connecting to a remote computer, you need to know the computer name, domain, user name, and password of the remote computer you are connecting to. These attributes are all passed into the IWbemLocator::ConnectServer method. Also, ensure the user name on the computer that is trying to connect to the remote computer has the correct access privileges on the remote computer. For more information, see Connecting Through Windows Firewall. To connect to the local computer, see Example: Getting WMI Data from the Local Computer and Creating a Connection to a WMI Namespace.
When handling user names and passwords, it is recommended that the user be prompted for the information, use the information, and then delete the information, so that there is less of a chance of the information being intercepted by an unauthorized user. Step 4 in the example code below uses CredUIPromptForCredentials to get the user name and password, and then uses SecureZeroMemory to get rid of the information after it is used in IWbemLocator::ConnectServer. For more information, see Handling Passwords and Asking the User for Credentials.
Create a COAUTHIDENTITY structure to provide credentials for setting the proxy security.
Use the IWbemServices pointer to make requests of WMI. A query is executed to obtain the name of the operating system and the amount of free physical memory by calling IWbemServices::ExecQuery.
The following WQL query is one of the method arguments.
Get and display the data from the WQL query. The IEnumWbemClassObject pointer is linked to the data objects that the query returned, and the data objects can be retrieved with the IEnumWbemClassObject::Next method. This method links the data objects to an IWbemClassObject pointer that is passed into the method. Use the IWbemClassObject::Get method to get the desired information from the data objects.
The following code example is used to get the Name property from the data object, which provides the name of the operating system.
VARIANT vtProp;
// Get the value of the Name property
hr = pclsObj->Get(L"Name", 0, &vtProp, 0, 0);
wcout << " OS Name : " << vtProp.bstrVal << endl;
Once the value of the Name property is stored in the VARIANT variable vtProp, it can then be displayed to the user.
The following code example shows how the VARIANT variable can be used again to store and display the value of the amount of free physical memory.
This module explains the structure of the namespaces that contain classes and also how to query instances of a class. It covers how to query remote computers by using ad-hoc connections and CIM sessions.