Microsoft KB Archive/223151

{|
 * width="100%"|

HOWTO: Search and List Recipients Using IDirectorySearch

 * }

Q223151

-

The information in this article applies to:


 * Microsoft Active Directory Service Interfaces, versions 2.0, 2.5
 * Microsoft Exchange Server, version 5.5 Service Packs 1, 2

-

SUMMARY
There are several ways to find and list Microsoft Exchange Server recipients. If you are looking for a way to perform requests with little demand on the system, the IDirectorySearch interface of Active Directory Services Interfaces (ADSI) is the best option.

MORE INFORMATION
IDirectorySearch is a COM interface that enables you to query a directory server, such as a Microsoft Exchange Server, directly from non-automation clients. The following example shows how to search for a subset of mailboxes on an Exchange Server site.

Sample Code
/* include: activeds.h  link with: activeds.lib adsiid.lib

HRESULT MyFunc {  HRESULT hr; IDirectorySearch *pSearch; // Inititalization

CoInitialize(NULL);

// Bind to the base search object

hr = ADsGetObject(           L"LDAP://server/cn=recipients,ou=site,o=org",            IID_IDirectorySearch,            (void**) &pSearch            );

if (!SUCCEEDED(hr)) {      return hr; }  // Perform a subtree search

ADS_SEARCHPREF_INFO prefInfo[1]; prefInfo[0].dwSearchPref = ADS_SEARCHPREF_SEARCH_SCOPE; prefInfo[0].vValue.dwType = ADSTYPE_INTEGER; prefInfo[0].vValue.Integer = ADS_SCOPE_SUBTREE; hr = pSearch->SetSearchPreference( prefInfo, 1);

// Prepare for attributes to be returned LPWSTR pszAttr[] = { L"cn",L"title",L"mail"}; ADS_SEARCH_HANDLE hSearch; DWORD dwCount= sizeof(pszAttr)/sizeof(LPWSTR);

// Search for mailboxes with First Name starting with letter 'F'  hr = pSearch->ExecuteSearch(             L"(&(objectClass=organizationalPerson)(givenName=F*))",             pszAttr,             dwCount,             &hSearch             );

if (!SUCCEEDED(hr)) {      pSearch->Release; return hr; }  // Enumerate the search result

ADS_SEARCH_COLUMN col; while( pSearch->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS ) {     // Print list of attributes for(unsigned int i=0; i < dwCount; i++) {         hr = pSearch->GetColumn( hSearch, pszAttr[i], &col ); if ( SUCCEEDED(hr) ) {            printf("\n%S",(LPWSTR)col.pADsValues->CaseIgnoreString); pSearch->FreeColumn( &col ); }     }   }   // Clean-up

pSearch->CloseSearchHandle(hSearch); pSearch->Release;

CoUninitialize;

return S_OK; }