Platform SDK: Active Directory, ADSI, and Directory Services

IDirectoryObject::GetObjectAttributes

The IDirectoryObject::GetObjectAttributes method gets one or more specified attributes of the directory service object, as defined in the ADS_ATTR_INFO structure.

HRESULT GetObjectAttributes(
  LPWSTR * pAttributeNames,  
  DWORD dwNumberAttributes,       
  PADS_ATTR_INFO * ppAttributeEntries,  
  DWORD * pdwNumAttributesReturned  
);

Parameters

pAttributeNames
[in] Specifies an array of names of the attributes that are requested. If NULL, all of the object's attributes are requested and the dwNumberAttribute parameter is assigned a value of -1.
dwNumberAttributes
[in] Specifies the size of the pAttributeNames array. If -1, all of the object's attributes are requested.
ppAttributeEntries
[out] Specifies the address of a pointer to an array of ADS_ATTR_INFO structures containing the requested attributes. If NULL, no attributes could be obtained from the directory service object.
pdwNumAttributesReturned
[out] Specifies the address of a DWORD variable that contains the number of attributes obtained by the GetObjectAttributes method.

Return Values

This method returns the standard values, as well as the following:

S_OK
The attribute definitions were obtained successfully.
E_ADS_PROPERTY_NOT_FOUND
One or more requested attributes cannot be found.

For other return values, see ADSI Error Codes.

Remarks

ADSI allocates the memory for the array of ADS_ATTR_INFO, as pointed to by ppAttributeEntries. However, the caller must call FreeADsMem to free the array. The order of attributes returned in ppAttributeEntries may not necessarily be the same as requested in pAttributeName.

Example Code [C++]

The following C++ code snippet shows how the IDirectoryObject::GetObjectAttribute method can be used.

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=Neil Smith,OU=Sales,DC=Microsoft,DC=com",
                     IID_IDirectoryObject, 
                     (void**) &pDirObject );
 
/////////////////////////////////////////
// Get attribute values requested
// Note: The order is not necessarily the 
// same as requested using pAttrNames.
///////////////////////////////////////////
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 multi-valued property, "Other Telephones".
           printf("Other Telephones:");
           for (DWORD val=0; val < pAttrInfo->dwNumValues; val++, pAttrInfo->pADsValues++) 
           {
                printf("  %S\n", pAttrInfo->pADsValues->CaseIgnoreString);
           }
       }
   }
}
 
   pDirObject->Release();
   /////////////////////////////////////////////////////////////
   // Use FreeADsMem for all memory obtained from the ADSI call. 
   /////////////////////////////////////////////////////////////
   FreeADsMem( pAttrInfo );

Requirements

  Windows NT/2000: Requires Windows 2000 (or Windows NT 4.0 with DSClient).
  Windows 95/98: Requires Windows 95 or later (with DSClient).
  Header: Declared in Iads.h.

See Also

IDirectoryObject, ADS_ATTR_INFO, FreeADsMem, ADSI Error Codes