'How to get password expiry date using C++ win32 API?

I am using C++ with the win32 Api and I want to get the password expiry date for a user using ADSI.

CoInitialize(NULL);

LPWSTR pszADsPath=L"LDAP://CN=arjun,CN=Users,DC=raja,DC=com";
//HRESULT hr;
IADsUser *pUser;

hr = ADsGetObject(pszADsPath, IID_IADsUser, (void**) &pUser);
if(SUCCEEDED(hr))
{
    DATE expirationDate;
    VariantInit(&var);
    hr = pUser->get_PasswordLastChanged(&expirationDate);
    hr = pUser->get_PasswordExpirationDate(&expirationDate);

    if (SUCCEEDED(hr))
        VariantTimeToSystemTime(expirationDate,&lpExpirationDate);

    pUser->Release();
}

Calling get_PasswordLastChanged will give success and return last changed password date, but I need the expiration date.

Using get_PasswordExpirationDate, I get S_OK (success) but it also gives an error code of -2147463155.

Can anyone explain what is going wrong?



Solution 1:[1]

I know this is an old post but I looked for the same thing and came upon this post and for me the OP's code (with a slight change) works. I added ldap url and using port 636. Just in case anyone searches for the same thing :

CCoInitialize CoInitialize;

if( !CoInitialize.IsSuccessful() )
{
    stringstream Text;
    Text << L"CoInitialize returned <" << CoInitialize.GetMessage() << L">" << endl;
    this->WriteLogThreadSafe( Text.str() );

    // Continue anyway because it could be it was already initialized.
}

LPWSTR pszADsPath=L"LDAP://<YOUR LDAP SERVER>:636/<YOUR DN>";
//HRESULT hr;
IADsUser *pUser;

HRESULT hr = ADsGetObject(pszADsPath, IID_IADsUser, (void**) &pUser);
if(SUCCEEDED(hr))
{
    SYSTEMTIME SystemTime = {};
    DATE expirationDate = {};
    VARIANTARG var = {};
    VariantInit(&var);
    hr = pUser->get_PasswordLastChanged(&expirationDate);
    hr = pUser->get_PasswordExpirationDate(&expirationDate);

    if (SUCCEEDED(hr))
        VariantTimeToSystemTime( expirationDate,&SystemTime );

    pUser->Release();
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ron AF Greve