Accessing a Remote Namespace

The main purpose of the IWMIExtension interface is to provide developers with access to a remote namespace through ADSI. By using ADSI, developers can query Active Directory for more information about directory structure and computer network location. After Active Directory returns the location of a computer, developers can use the IWMIExtension::GetWMIServices method to access the WMI namespace on that computer. Developers can then perform any task that WMI allows, such as creating an object or querying the Windows Management service.

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

The following procedure describes how to access a remote namespace using ADSI.

Aa384706.wedge(en-us,VS.85).gifTo access a remote namespace using ADSI

  1. Determine the name of the computer that contains the namespace 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.

    The following example shows how to query Active Directory in Microsoft Visual Basic with the GetObject function.

    Set ADSObject = GetObject("LDAP:// _
        CN=<computer name>,CN=Computers, _

    Similarly, a C++ application can use a COM call to query Active Directory. The following example shows how to query Active Directory using the ADsGetObject interface.

    IADsComputer *pComputer;
    wchar_t Obj[80] = L"LDAP://CN=<computer name,CN=Computers,DC=<domain>,DC=<company>,DC=com";
    CoInitialize (NULL);
    HRESULT hRes = ADsGetObject( 
       Obj, IID_IADs, (void**)&pComputer );

    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>
  3. Use the Active Directory Computer object to access the WMI \cimv2 namespace with the GetWMIServices method.

    Visual Basic developers can use the IWMIExtension.GetWMIServices method, as shown in the following example.

    Set WMIServices = ADSObject.GetWMIServices

    C/C++ developers can query COM for a pointer to IWbemServices, as shown in the following example.

    ISWbemServices* pSvc = NULL; 
    IWMIExtension* pWMI = NULL;
    hRes = pComputer->QueryInterface(
         IID_IWMIExtension, (void**)&pWMI);
    hRes = pWMI->GetWMIServices(&pSvc);
  4. Use the namespace to access the methods and objects inside the namespace.

    The following Visual Basic example uses a connection to an SWbemServices object named "WMIServices" to connect to a Win32_LogicalDisk object in the default namespace. The example shows how to display the type of file system that the C:\ hard disk drive contains.

    Set WMIObject = _
    Debug.Print "FileSystem=" + WMIObject.FileSystem

    The following C++ sample also shows how to display the file system type that the "C:" hard disk drive contains.

    ISWbemObject* pObj = NULL;
    BSTR  strObjPath = SysAllocString(
    hRes = pSvc->Get(strObjPath, NULL, NULL, &pObj);
    BSTR strObjText = NULL;
    hRes = pObj->GetObjectText_(0, &strObjText);
    wprintf(L"%s\n", strObjText);

Related topics

Managing Objects on a Network