Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Searching for Attributes

The following function creates an array of ADS_SEARCHPREF_INFO structures and sets the specified search preferences. An application can use the returned array to call IDirectorySearch.

HRESULT SetSearchPreferences(
    DWORD dwScope,            //-1 means use default: subtree
    DWORD dwOverallTimeOut,   // 0 means use default: no time-out
    DWORD dwOverallSizeLimit, // 0 means use default: no size limit
    DWORD dwOverallTimeLimit, // 0 means use default: no time limit
    BOOL bCacheResult,        // True means use default.
    BOOL bIsAsynchronous,     // False means use default
    DWORD dwPageSize,         // 0 means use default
    DWORD dwPageTimeLimit,    // 0 means use default
    DWORD dwChaseReferral,    // 0 means use default
    LPOLESTR szSortKey,       // NULL means don't sort.
    BOOL bIsDescending,
    BOOL bReturnAttributeNamesOnly, //False means use default.
    ADS_SEARCHPREF_INFO **ppSearchPref, // Returns array of search preferences.
    DWORD *pdwSearchPrefCount
    )
{
HRESULT hr = S_OK;
DWORD dwCountPref = 0L;
 
//Determine size of preferences array.
DWORD dwTotal = 11L;
 
if(dwScope==-1)
  dwTotal--;
if(dwOverallTimeOut<=0)
  dwTotal--;
if(dwOverallSizeLimit<=0)
  dwTotal--;
if(dwOverallTimeLimit<=0)
  dwTotal--;
if(bCacheResult)
  dwTotal--;
if(!bIsAsynchronous)
  dwTotal--;
if(dwPageSize<=0)
  dwTotal--;
if(dwPageTimeLimit<=0)
  dwTotal--;
if(dwChaseReferral<=0)
  dwTotal--;
if(!bReturnAttributeNamesOnly)
  dwTotal--;
if (!szSortKey)
  dwTotal--;
 
ADS_SEARCHPREF_INFO *prefInfo = new ADS_SEARCHPREF_INFO[ dwTotal ];
ADS_SORTKEY SortKey;
 
    //////////////////
    // Search Scope
    //////////////////
    if(dwScope>=0)
  {
    prefInfo[dwCountPref].dwSearchPref =
                  ADS_SEARCHPREF_SEARCH_SCOPE;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_INTEGER;
    prefInfo[dwCountPref].vValue.Integer = dwScope;
    dwCountPref++;
  }
 
    //////////////////
    // Time-out
    //////////////////
  if(dwOverallTimeOut>0)
  {
    prefInfo[dwCountPref].dwSearchPref = ADS_SEARCHPREF_TIMEOUT;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_INTEGER;
    prefInfo[dwCountPref].vValue.Integer = dwOverallTimeOut;
    dwCountPref++;
  }
 
    ///////////////
    // Size Limit
    ///////////////
  if(dwOverallSizeLimit>0)
  {
    prefInfo[dwCountPref].dwSearchPref = ADS_SEARCHPREF_SIZE_LIMIT;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_INTEGER;
    prefInfo[dwCountPref].vValue.Integer = dwOverallSizeLimit;
    dwCountPref++;
  }
 
    ///////////////
    // Time Limit
    ///////////////
  if(dwOverallTimeLimit>0) 
  {
    prefInfo[dwCountPref].dwSearchPref = ADS_SEARCHPREF_TIME_LIMIT;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_INTEGER;
    prefInfo[dwCountPref].vValue.Integer = dwOverallTimeLimit;
    dwCountPref++;
  }
 
    /////////////////
    // Cache Result
    /////////////////
 
  if (!bCacheResult)
  {
    prefInfo[dwCountPref].dwSearchPref =
                ADS_SEARCHPREF_CACHE_RESULTS;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_BOOLEAN;
    prefInfo[dwCountPref].vValue.Boolean = bCacheResult;
    dwCountPref++;
  }
 
    //////////////
    // Page Size
    //////////////
  if(dwPageSize>0)
  {
    prefInfo[dwCountPref].dwSearchPref = ADS_SEARCHPREF_PAGESIZE;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_INTEGER;;
    prefInfo[dwCountPref].vValue.Integer = dwPageSize;
    dwCountPref++;
  }
 
    //////////////
    // Page Time Limit
    //////////////
  if(dwPageTimeLimit>0)
  {
    prefInfo[dwCountPref].dwSearchPref = ADS_SEARCHPREF_PAGED_TIME_LIMIT;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_INTEGER;;
    prefInfo[dwCountPref].vValue.Integer = dwPageTimeLimit;
    dwCountPref++;
  }
 
    ///////////////////
    // Chase Referrals
    ///////////////////
  if(dwChaseReferral>0)
  {
    prefInfo[dwCountPref].dwSearchPref =
               ADS_SEARCHPREF_CHASE_REFERRALS;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_INTEGER;
    prefInfo[dwCountPref].vValue.Integer = dwChaseReferral;
    dwCountPref++;
  }
 
    /////////////
    // Sort
    /////////////
    if (szSortKey)
    {
        prefInfo[dwCountPref].dwSearchPref = ADS_SEARCHPREF_SORT_ON;
        prefInfo[dwCountPref].vValue.dwType = ADSTYPE_PROV_SPECIFIC;
        SortKey.pszAttrType = (LPWSTR)LocalAlloc(
                        LPTR,
                        wcslen(szSortKey)*sizeof(WCHAR) +sizeof(WCHAR)
                        );
    wcscpy(SortKey.pszAttrType,szSortKey);
        SortKey.pszReserved = NULL;
        SortKey.fReverseorder = 0;
        prefInfo[dwCountPref].vValue.ProviderSpecific.dwLength = sizeof(ADS_SORTKEY);
        prefInfo[dwCountPref].vValue.ProviderSpecific.lpValue = (LPBYTE) &SortKey;
        dwCountPref++;
    }
    
    /////////////////
    // Asynchronous
    /////////////////
  if(bIsAsynchronous)
  {
    prefInfo[dwCountPref].dwSearchPref =
                ADS_SEARCHPREF_ASYNCHRONOUS;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_BOOLEAN;
    prefInfo[dwCountPref].vValue.Integer = bIsAsynchronous;
    dwCountPref++;
  }
 
    ////////////////////////
    // Attribute Type Only
    ////////////////////////
  if(bReturnAttributeNamesOnly)
  {
    prefInfo[dwCountPref].dwSearchPref =
               ADS_SEARCHPREF_ATTRIBTYPES_ONLY;
    prefInfo[dwCountPref].vValue.dwType = ADSTYPE_BOOLEAN;
    prefInfo[dwCountPref].vValue.Integer = bReturnAttributeNamesOnly;
    dwCountPref++;
  }
 
if (SUCCEEDED(hr))
{
  *pdwSearchPrefCount = dwCountPref;
  *ppSearchPref  = prefInfo;
}
else
{
  *pdwSearchPrefCount = 0L;
  *ppSearchPref  = NULL;
}
 
 
return hr;
}