Q:

Password Expiry Date using c++?

i want to display the password expiry dialogbox using c++ win32 API...
i done it using System.directoryservice namespace...
But now i need in Win32 API..
Any functions there for get password expiry date?
Thanks in advance

A:

You can use the following function to get the password expiration date:
HRESULT GetPasswordExpirationDate(LPTSTR lpszPathName, LPSYSTEMTIME lpExpirationDate)
{
    HRESULT hr;
    IADsUser *pUser;
    hr = ADsGetObject(lpszPathName, IID_IADsUser, (void**) &pUser);
    if(SUCCEEDED(hr))
    {
        DATE expirationDate;
        hr = pUser->get_PasswordExpirationDate(&expirationDate);
        if (SUCCEEDED(hr))
            VariantTimeToSystemTime(expirationDate, lpExpirationDate);

        pUser->Release();
    }

    return hr;
}

Where lpszPathNameis a LDAP or WinNT path and lpExpirationDate is a pointer to SYSTEMTIME structure.
Note, that you must include Windows.h, Iads.h and Adshlp.h and link with ADSIid.Lib and ActiveDS.Lib to get it work.
Example usage:
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

SYSTEMTIME expirationDate;
HRESULT hr = GetPasswordExpirationDate(_T("WinNT://ComputerName/UserName,user"),
    &expirationDate);

if (SUCCEEDED(hr))
{
    //TODO: do whatever you want with the expirationDate here
}

CoUninitialize();

You may want to use one of the following API calls to retrieve current user and computer/domain names: GetUserName, GetComputerName GetUserNameEx, NetWkstaUserGetInfo
If you need to retrieve password expiration dates for multiple domain users, it might be better to use ADsBuildEnumerator instead of ADsGetObject (see example on MSDN).

