tags:

views:

91

answers:

2

I'm looking for a winapi function that will allow me to change current logged in user's password. In my case, I know current password of logged in user. I use Windows 7 Ultimate.

Thanks.

Background

The background will look weird, but I'm going to describe it for clarification. My personal home PC is used by several users (dad, sister, cousins etc.). I'm the only administrator of the PC. All others are standard user. All user accounts are password protected.

I don't like that other people use my account as they mess with anything. I always install all softwares from my account and also troubleshoot PC. When I'm away from my PC for several weeks, may be some other user need to install a software, or do something that require administrative right. For this reason they may need to use my account. There may be emergency cases, and I must allow the user to log into my account by giving account password.

I recently faced such a situation. Before leaving my PC for long time, I changed my regular password to something else. I'll change that again when I reach my PC.

So I'm thinking to write a tiny program that will run each time someone logs into my account. The program will only change current password to something else that I only know. In this case, if anyone logs into my account, installs something, logs out, and cannot logged back in as the password changes.

Suppose I set my account's password as abcd. When someone logs in, the program will change it to abcde. Next time may be abc123 and so on.

A: 

You could use ADSI to connect to the local machine, get the user object and call the change password action.

MSDN doc on IADsUser::ChangePassword has the following example.

The following "C++" code example shows how to change a user password.

HRESULT ChangePassword(
    IADsUser *pUser, 
    LPWSTR oldPasswd, 
    LPWSTR newPasswd)
{
    HRESULT hr=S_OK;
    if(!pUser) { return E_FAIL;}
    hr = pUser->ChangePassword(oldPasswd, newPasswd);
    printf("User password has been changed");
    return hr;
}

The following VB code example shows how to get the user and change a user password.

Dim usr As IADsUser
Dim szOldPass As String
Dim szNewPass As String

On Error GoTo Cleanup

' Fabrikam is the MSDN fictional domain - us '.' for localhost
Set usr = GetObject("WinNT://Fabrikam/JeffSmith,user")
' Add code to securely retrieve the old and new password.

usr.ChangePassword szOldPass, szNewPass

Cleanup:
    If (Err.Number<>0) Then
        MsgBox("An error has occurred. " & Err.Number)
    End If
    Set usr = Nothing
Greg Domjan
I've never used ADSI. I'll look into it and will return if I face any problem. :)
Donotalo
+4  A: 

You're looking for NetUserChangePassword(). Check this MSDN link for sample code:

http://support.microsoft.com/kb/151546

Bukes
I tried `NetUserChangePassword(0, 0, (LPCWSTR) "ab", (LPCWSTR) "123456");`. and the returned value is `ERROR_INVALID_PASSWORD`. Any idea why?
Donotalo
Because you need to specify real UNICODE strings. Try this: NetUserChangePassword(0, 0, L"ab", L"123456");
Bukes