Change NT Account Password

I want to write a COM object to change the NT user password. I want to write COM so that I can also use it on a web page. So how can we change the user password? ofcourse the user that is logged=in and running this COM object.
I tried the Netuserchangepassword() function but that does not work, it gives unknown error. so tell me a code that works.

jaykuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
HOWTO: Change Passwords Programmatically in Windows NT
http://support.microsoft.com/support/kb/articles/Q151/5/46.asp
0
jaykuAuthor Commented:
I tried this, but it always gives "User not found" error or "Unlnown Error" if computer name is given NULL.
Pl. verify and tell me where I must check to make this work.

Thanks
0
chensuCommented:
How do you specify the computer name to NULL?
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

jaykuAuthor Commented:
From the help link you gave i converted that commandline program to a function that takes username,computername,old and new password. So i passed NULL as computer name.
the function is here

int ChangePWD(LPWSTR  wUserName,LPWSTR wComputerName, LPWSTR wOldPassword, LPWSTR wNewPassword)
{
      USER_INFO_1003  pi1003;
      NET_API_STATUS  nas;

   //
   // the user specified a domain name. Look up the PDC.
   // This is done in both password change cases to ensure the
   // same computer is targeted for the update operation.
   //

      if( wComputerName != NULL)
      {
            nas = NetGetDCName(NULL, wComputerName, (LPBYTE *)&wComputerName );
         if(nas != NERR_Success)
         {
               DisplayErrorText( nas );
               //return RTN_ERROR;
            }
       }
      else
            wComputerName = (LPWSTR)"\\\\redmond";
      
      if(wOldPassword == NULL)
      {

         //
         // administrative over-ride of existing password
         //

         pi1003.usri1003_password = wNewPassword;

         nas = NetUserSetInfo(
                     wComputerName,  // computer name
                     wUserName,      // username
                     1003,           // info level
                     (LPBYTE)&pi1003,     // new info
                     NULL
                     );
      }
      else
      {

         //
         // allows user to change their own password
         //

         nas = NetUserChangePassword(
                     wComputerName,
                     wUserName,
                     wOldPassword,
                     wNewPassword
                     );
      }

      NetApiBufferFree(wComputerName);
      

      if(nas != NERR_Success)
      {
         //DisplayErrorText( nas );
            CString csStr;
            switch(nas)
            {
                  case ERROR_ACCESS_DENIED:
                        AfxMessageBox("Access Denied");
                        break;

                  case NERR_InvalidComputer:
                        AfxMessageBox("Invalid Computer Name");
                        break;

                  case NERR_UserNotFound:
                        AfxMessageBox("User Not Found");
                        break;

                  default:
                        csStr.Format("Unknown Error");
                        AfxMessageBox(csStr);

            }

         return RTN_ERROR;
      }

      return RTN_OK;
}
0
chensuCommented:
Note that the 2nd parameter of NetGetDCName is the domain name instead of the computer name.
0
jaykuAuthor Commented:
yeah I tried that too. but it says cannot find domain controller for the given domain name.
so tell me how it can be done

Thanks
0
chensuCommented:
I guess there is something wrong with your Unicode strings. Use something like L"domainname" instead of (LPWSTR)"domainname".
0
jaykuAuthor Commented:
I tried this, it yet gives either "No Domain Controller found for this domain" error or "Invalid Password. Can you please write a small function test it and send me exactly how to use it, so that it will solve my problem

Thanks for you help
0
chensuCommented:
The following code works fine on my computer.

LPBYTE lpbyBuf;
if (::NetGetDCName(NULL, NULL, &lpbyBuf) == NERR_Success)
{
    wprintf(L"%s\n", lpbyBuf);

    ::NetApiBufferFree(lpbyBuf);
}
0
jaykuAuthor Commented:
What I get using ur function is '\' thats all?
So can you send me entire function to change password what works well there..

Thanks
0
chensuCommented:
>What I get using ur function is '\' thats all?

The returned string is in Unicode. You need to use Unicode-enabled functions to display it.

So, any other errors?
0
jaykuAuthor Commented:
yeah but the function NetUserSetInfo and NetUserChangePassword take LPWSTR are paramters so I typecasted and passed. I get the error "Unknown Error"
0
chensuCommented:
I would suggest you use hardcoded strings (e.g. L"dcname") for the time being to see if it works.
0
gvgCommented:
If you are still having trouble I guess it is because you are not sending correct LPWSTR.

If you have for example LPSTR you can use the ATL macro A2W to convert it into a LPWSTR.

I guess you might need to include <atlbase.h>

Gunnar Valur

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jaykuAuthor Commented:
Thanks it works ok now
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.