I am wrote a function which return all the direct reports of the active directory user but it return only the first value rather returning all the direct reports.
void getDirectReports(LPCWSTR pwszAccountName) {
HRESULT hr;
CoInitialize(NULL);
IDirectorySearch* pdSearch = NULL;
ADS_SEARCH_COLUMN col;
CComBSTR path = L"LDAP://WIN-F94H2MP3UJR.Test.local/CN=";
path += pwszAccountName;
path += L",CN=Users,DC=Test,DC=local";
hr = ADsOpenObject(path, L"Administrator", L"Password",
ADS_SECURE_AUTHENTICATION, // For secure authentication
IID_IDirectorySearch,
(void**)&pdSearch);
if (SUCCEEDED(hr)) {
LPWSTR directReports = convert(L"directReports");
LPWSTR pszAttr[] = { directReports};
ADS_SEARCH_HANDLE hSearch;
DWORD dwCount = 0;
DWORD nameSize = sizeof(pszAttr) / sizeof(LPWSTR);
LPWSTR path = convert(L"(&(objectClass=user))");
hr = pdSearch->ExecuteSearch(path, pszAttr, nameSize, &hSearch);
while (pdSearch->GetNextRow(hSearch) != S_ADS_NOMORE_ROWS) {
hr = pdSearch->GetColumn(hSearch,directReports, &col);
if (SUCCEEDED(hr)) {
printf("%S\n", (BSTR)col.pADsValues->CaseIgnoreString);
}
}
}
}
I have 5 direct reports to the ad user and this function only prints the first value alone.
The IDirectorySearch::GetColumn
method will give you an ADS_SEARCH_COLUMN
structure in your col
variable. That documentation tells us that the value in col.pADsValues
is an array, with the array size given in the col.dwNumValues
.
In C++, accessing an array without the index (like col.pADsValues
) is equivalent to reading the first value in the array (col.pADsValues[0]
).
You just need to loop through the array.
hr = pdSearch->GetColumn(hSearch,directReports, &col);
if (SUCCEEDED(hr)) {
for (DWORD x = 0; x < col.dwNumValues; x++) {
printf("%S\n", (BSTR)col.pADsValues[x].CaseIgnoreString);
}
}