Accessing a Remote WMI Win32_ComputerSystem Object

Accessing a remote namespace provides all of the capabilities most developers need. However, there may be times when accessing an object directly is more efficient. For example, a Directory Services query may return an enumerated list of computer objects that a developer wishes to manage. To avoid the additional step of accessing multiple namespaces, the IWMIExtension interface provides the GetWMIObject method.

Note  For more information about support and installation of this component on a specific operating system, see Operating System Availability of WMI Components.

The GetWMIObject method returns an instance of a WMI Win32_ComputerSystem object that Directory Services maps to a Directory Services Computer object. Due to the structure of the Directory Services, the ADSI extension currently supports the GetWMIObject method only on Directory Services Computer objects. After GetWMIObject returns the Win32_ComputerSystem object, a developer can access the object like any other WMI object.

The following procedure describes how to access a remote Win32_ComputerSystem object.

Aa384713.wedge(en-us,VS.85).gifTo access a remote Win32_ComputerSystem object

  1. Determine the name of the computer containing the Win32_ComputerSystem object you wish to access.

    You need the name of the computer to locate the computer on the enterprise. The most common way to locate a computer is to query Active Directory. Active Directory uses the current Directory Services security settings for the user. Make sure the user has the correct level of access before attempting to use Active Directory.

  2. Retrieve the Active Directory Computer object that represents the computer.

    Visual Basic provides this service using the GetObject function. C/C++ developers should make a call to the ADsGetObject function.

    Directory Services uses the current DS user security settings to access Active Directory.

  3. Use the GetWMIObject method to retrieve the Win32_ComputerSystem instance associated with the computer.

    Directory Services uses the current WMI user security settings to access the Win32_ComputerSystem object. Therefore, ensure the current user has the correct security clearance.

  4. Access the Win32_ComputerSystem object as you would a normal WMI object.

The following example shows how to access a Win32_ComputerSystem object using a call to GetWMIObject in Visual Basic.

Dim ADSObject As IADs
Dim WMIObject As SWbemObject

' Query Active Directory for your computer.
Set ADSObject = GetObject("LDAP://CN=<computer _
   name>,CN=Computers,DC=<domain>,DC=<company>,DC=com")

' Use GetWMIObject on the Directory Services
Computer object to retrieve the WMI object
Set WMIObject = ADSObject.GetWMIObject   

' Now use any properties 
'   or methods of the WMI object.
Debug.Print "Status=" + WMIObject.Status

The following example shows how to access a Win32_ComputerSystem object using a call to GetWMIObject in Visual C/C++.

IADs  *pComputer = NULL;
IWMIExtension   *pWMI = NULL;
ISWbemObject    *pObj = NULL;

   wchar_t Obj[80] = L"LDAP://CN=<computer name,CN=Computers,DC=<domain>,DC=<company>,DC=com";

   CoInitialize (NULL);
   
   //Query Active Directory to get your computer.
   HRESULT hRes = ADsGetObject( 
      Obj, IID_IADs, (void**)&pComputer );

   //Get the WMI ADSI extension interface on this object.    
   hRes = pComputer->QueryInterface( 
      IID_IWMIExtension, (void**)&pWMI );

   //Use the extension to get the WMI object.
   hRes = pWMI->GetWMIObject(&pObj);

   //Print the object.
   BSTR strObjText;
   hRes = pObj->GetObjectText_(0, &strObjText);
   wprintf(L"%s\n", strObjText);
   SysFreeString(strObjText);

   //Clean up.
   pObj->Release();
   pWMI->Release();
   pComputer->Release();

The C++ code requires the following references and #include statements to compile correctly.

# pragma comment(lib, \"wbemuuid.lib\")
#include <iostream>
using namespace std;
#include <Iads.h>
#include <Wbemads.h>
#include <Adshlp.h>

Managing Objects on a Network