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.
To access a remote namespace using ADSI
- 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.
- 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, _ DC=<domain>,DC=<company>,DC=com")
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 = 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.
- Use the Active Directory Computer object to access the WMI \cimv2 namespace with the
Visual Basic developers can use the IWMIExtension.GetWMIServices method, as shown in the following example.
C/C++ developers can query COM for a pointer to IWbemServices, as shown in the following example.
- 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 = _ WMIServices.Get("Win32_LogicalDisk.DeviceID=""C:""") 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( L"Win32_LogicalDisk.DeviceID=\"C:\""); hRes = pSvc->Get(strObjPath, NULL, NULL, &pObj); SysFreeString(strObjPath); BSTR strObjText = NULL; hRes = pObj->GetObjectText_(0, &strObjText); wprintf(L"%s\n", strObjText); SysFreeString(strObjText);
Build date: 11/19/2012