Skip to main content

 none
How to get password expiry date using C++ win32 API? RRS feed

  • Question

  • i am using C++ win32 Api...

    i want to get the password expiry date for an user?

    i try to get it using ADSI...

    But i cant get it...

    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();
    
                    }

    this is my code...

    hr = pUser->get_PasswordLastChanged(&expirationDate);

    this will give success...and return last changed password date...

    But i need hr = pUser->get_PasswordExpirationDate(&expirationDate);

    this one.but i cant get S_OK(success).it gives -2147463155 error code value...

    The error translates to ADS_PROPERTY_NOT_FOUND. 

    But i already check it,the password for this user will expire...when i logon using that user,it shows "the password will expire on 2 days,would u like to change password" dialogboxi check this error code...but i cant get any idea...

    Anyone can resolve it?

    Monday, October 29, 2012 6:24 AM

Answers

  • I used this code, it can work for me and display the date for my domain user account: 

    #include "stdafx.h"
    
    
    #include <Windows.h>
    #include <Iads.h>
    #include <Adshlp.h>
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	CoInitialize(NULL);
    	LPWSTR pszADsPath=L"LDAP://CN=username,OU=UserAccounts,DC=d0,DC=d1,DC=com";
    	//HRESULT hr;
    	IADsUser *pUser;
    
    	HRESULT hr = ADsGetObject(pszADsPath, IID_IADsUser, (void**) &pUser);
    	VARIANTARG var;
    	LPSYSTEMTIME lpExpirationDate= (LPSYSTEMTIME)malloc(sizeof(LPSYSTEMTIME));
    	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();
    	}
    	return 0;
    }

    And I used this way to get my ADsPath for the domain user: http://blogs.technet.com/b/heyscriptingguy/archive/2005/11/03/how-can-i-determine-the-adspath-for-the-logged-on-user.aspx 

    You can use the Active Directory Administrator Center like tools to help you check if your user account has the expiration date first, to help you ensure your code is no problem.

    For more about AD aspect question, you also can try this forum: http://forums.asp.net/93.aspx/1?Active+Directory+and+LDAP

    Best wishes,


    Mike Zhang[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, October 30, 2012 8:14 AM
    Moderator