Microsoft

Programmatically changing a user's Windows password with Visual Basic

In the first installment of Visual Basic Q&A, Lamont Adams explains a simple way to change a user's password on Windows 95/98 systems.


Q: How do I change a user’s Windows 95/98 password with Visual Basic?

A: If you’re programming for Windows 95 or 98 and you don’t require a custom user interface, the pwdChangePassword API function is the simplest way to change a password. This function displays a standard, noncustomizable dialog box allowing the user to change his or her password. The VB declaration is:
 
Declare Function PwdChangePassword Lib _
"mpr.dll" Alias "PwdChangePasswordA" _
(ByVal lpProvider As String, _
ByVal hwndOwner As Long, _
ByVal dwFlags As Long, _
ByRef lpChangePwdInfo As Any) As Long

The pwdChangePassword function actually has two declarations, depending on the character set in use: Unicode or ASCII. The above is the ASCII declaration. PwdChangePassword accepts the following arguments:
  • ·        LpProvider—A standard Visual Basic string indicating the name of the provider for which we are about to issue a new password. Specifying vbNullString causes the function to operate on the logon password.
  • ·        HwndOwner—The window handle of the window that will “own” the dialog. A dialog’s owner window is blocked until the dialog has been closed. Passing 0 (zero) for this argument results in a modeless dialog.
  • ·        DwFlags—Specifies special behavior for the dialog; these flags are ignored if lpProvider is a null string.
  • ·        LpChangePwdInfo—A user-defined type containing the username and password. This is actually ignored when setting the logon password.

On Windows 9x systems, there are usually two distinct passwords: the Windows password and the logon password. Calling PwdChangePassword will change only the user’s Windows password. That’s fine on a stand-alone system but could possibly leave the logon and Windows passwords out of sync and confuse the user of a networked system. To prevent this, we synchronize the two passwords by calling the PwdSetPasswordStatus API function and specifying "MSNP32" as the provider name, before calling PwdChangePassword. PwdSetPasswordStatus is declared as follows:
 
Declare Function PwdSetPasswordStatus _
Lib "mpr.dll" Alias "PwdSetPasswordStatusA" _
(ByVal lpProvider As String, _
ByVal dSyncMasterPWD As Long, _
ByVal dSyncMasterPWD2 As Long) As Long

 

You can see our sample code illustrating the use of these two functions in this sidebar.

 

Feedback time
What do you think of this feature? Send the editors your comments on this or any other article.

 

 

Editor's Picks

Free Newsletters, In your Inbox