I can't get the Win32 ADSI c GetInfoEx API to retrieve the an AD user's mail attribute. The Get call instead returns hr 0x8000500D (E_ADS_PROPERTY_NOT_FOUND). Any ideas of how I can get the get the mail attribute? Here's my code.
HRESULT hr = CoInitialize(NULL);
if (hr == S_OK || hr == S_FALSE)
{
IADs *pUsr = NULL;
hr = ADsGetObject(L"WinNT://adomainname/ausername,user", IID_IADs, (void**)&pUsr);
if (SUCCEEDED(hr))
{
VARIANT var;
VariantInit(&var);
LPWSTR pszAttrs[] = { L"mail" };
DWORD dwNumber = sizeof(pszAttrs) / sizeof(LPWSTR);
HRESULT hrAry = ADsBuildVarArrayStr(pszAttrs, dwNumber, &var);
hr = pUsr->GetInfoEx(var, 0);
VariantClear(&var);
if (SUCCEEDED(hrAry) && SUCCEEDED(hr))
{
hr = pUsr->Get(CComBSTR("mail"), &var);
if (SUCCEEDED(hr))
{
printf("mail: %S\n", V_BSTR(&var));
VariantClear(&var);
}
}
if (pUsr)
pUsr->Release();
}
CoUninitialize();
}
CodePudding user response:
The mail
attribute is not available when using the WinNT provider: Unsupported IADsUser Properties
You have to use LDAP.
If you have the distinguishedName
of the account, you can use that:
hr = ADsGetObject(L"LDAP://CN=someuser,OU=Users,DC=example,DC=com", IID_IADs, (void**)&pUsr);
If all you have is the domain and username, then you can use LDAP://adomainname
and search domain for the username with the filter:
(&(objectClass=user)(objectCategory=person)(sAMAccountName=ausername))
Or you can use IADsNameTranslate
to translate the domain\username (ADS_NAME_TYPE_NT4
) into the distinguished name (ADS_NAME_TYPE_1779
).