Platform SDK: Active Directory, ADSI, and Directory Services |
The IDirectoryObject interface provides clients written in C and C++ with direct access to directory service objects. The interface enables access by means of a direct network protocol, rather than through the ADSI property cache. In place of the properties supported by the IADs interface, IDirectoryObject provides methods that support a critical subset of an object's housekeeping methods and provide access to its attributes. With IDirectoryObject, a client can get or set any number of object attributes with one method call. Unlike the corresponding Automation methods, which are batched, those of IDirectoryObject are executed as soon as they are called. Because methods on this interface do not require creating an instance of an Automation directory object, the performance overhead is very low.
Clients written in languages such as C and C++ should call the methods of IDirectoryObject to optimize performance and take full advantage of native directory service interfaces. Automation clients cannot use IDirectoryObject. Instead, they should call the IADs interface.
The IDirectoryObject::GetObjectAttributes method allows you to retrieve attributes with both single and multiple values. GetObjectAttributes takes a list of requested attributes and returns an ADS_ATTR_INFO structure. ADSI allocates this structure, but you must free it using FreeADsMem.
The order of returned attribute values is not necessarily the same as the order in which you requested the attributes. Therefore, you must compare the attribute names returned from ADSI.
Note The ADS_ATTR_INFO structure will not return all the attributes that you request. Only those attributes that contain values will be part of the returned structure.
The number of attributes returned is determined by the dwNumberAttributes parameter.
Example:
HRESULT hr; ADS_ATTR_INFO *pAttrInfo=NULL; DWORD dwReturn; LPWSTR pAttrNames[]={L"givenName",L"sn", L"otherTelephone" }; DWORD dwNumAttr=sizeof(pAttrNames)/sizeof(LPWSTR); IDirectoryObject *pDirObject=NULL; hr=ADsGetObject(L"LDAP://CN=JohnSmith,OU=Sales, DC=Microsoft,DC=com", IID_IDirectoryObject, (void**) &pDirObject ); hr = pDirObject->GetObjectAttributes(pAttrNames, dwNumAttr, &pAttrInfo, &dwReturn ); if ( SUCCEEDED(hr) ) { for(DWORD idx=0; idx < dwReturn;idx++, pAttrInfo++ ) { if ( _wcsicmp(pAttrInfo->pszAttrName,L"givenName") == 0 ) { printf("First Name: %S\n", pAttrInfo->pADsValues>CaseIgnoreString); } else if ( _wcsicmp(pAttrInfo->pszAttrName, L"sn") == 0 ) { printf("Last Name: %S\n", pAttrInfo->pADsValues->CaseIgnoreString); } else if (_wcsicmp(pAttrInfo->pszAttrName, L"otherTelephone") == 0 ) { //Print out the property with multiple values, "Other Telephones". printf("Other Telephones:\n"); for (DWORD val=0; val < pAttrInfo->dwNumValues; val++, pAttrInfo->pADsValues++) { printf(" %S\n", pAttrInfo->pADsValues->CaseIgnoreString); } } } } pDirObject->Release(); FreeADsMem( pAttrInfo );