Directory Services

Example Code for Searching for Attributes

The following code example shows how to use the ADS_SEARCHPREF_ATTRIBTYPES_ONLY search preference to retrieve only the names of attributes to which values have been assigned. The example initializes an ADS_SEARCHPREF_INFO structure and sets the search preference by calling the SetSearchPreference method of the IDirectorySearch interface. The example then calls the ExecuteSearch method to perform the search.

// Setting the search preference.
// m_pSearch is a valid pointer to an IDirectorySearch interface.
ADS_SEARCHPREF_INFO prefInfo[1];
LPWSTR pszColumn = NULL;

// Set the search preference to indicate attribute names only.
prefInfo[0].dwSearchPref = ADS_SEARCHPREF_ATTRIBTYPES_ONLY;
prefInfo[0].vValue.dwType = ADSTYPE_BOOLEAN;
prefInfo[0].vValue.Integer = TRUE;
hr = m_pSearch->SetSearchPreference(prefInfo, 1);

// Execute the search.
hr = m_pSearch->ExecuteSearch(
				L"(|(objectCategory=domainDNS)(objectCategory=organizationalUnit))", 
				NULL, -1, &hSearch );

if(FAILED(hr))
{
   return;
}

// Retrieve the column name.
hr = m_pSearch->GetNextRow(hSearch);

if(FAILED(hr))
{
   return;
}

while( m_pSearch->GetNextColumnName( hSearch, &pszColumn ) != S_ADS_NOMORE_COLUMNS )
{
   wprintf(L"%S ", pszColumn );
   FreeADsMem( pszColumn );
}