/*
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;
}