Article ID: 177200
Article Last Modified on 3/21/2005
////////////////////////////////////
#include <windows.h>
#include <stdio.h>
#include "pwdspi.h"// Found in July 96 DDK otherwise define as follows
#ifndef PS_SYNCMASTERPWD //if pwdspi.h is not present
#define PS_SYNCMASTERPWD 0x03
#define PS_SYNCMASTERPWD_OFF 0x00
#define PS_SYNCMASTERPWD_ON 0x01
// Function definition of PwdChangePassword and helper struct for
// Windows 95. Helper structure (also found in pwdspi.h)
typedef struct _CHANGEPWDINFO{
LPTSTR lpUsername;
LPTSTR lpPassword;
DWORD cbPassword;
} CHANGEPWDINFO, FAR *LPCHANGEPWDINFO;
#endif
// Function definitions:
typedef DWORD
(APIENTRY *LPPwdChangePassword)(LPCTSTR,HWND,DWORD,LPCHANGEPWDINFO);
typedef DWORD (APIENTRY *LPPwdSetPasswordStatus)(LPCSTR,DWORD,DWORD);
// Function to display error messages upon API failure
void DisplayError(char *pszAPI);
void main()
{
CHANGEPWDINFO s = {NULL,NULL,0}; // initialize struct to empty.
LPPwdChangePassword sPwdChangePassword;
LPPwdSetPasswordStatus sPwdSetPasswordStatus;
DWORD res = 0;
DWORD cbUserName = 250;
HINSTANCE lib = LoadLibrary("MPR.DLL");
if (lib == NULL)
DisplayError("LoadLibrary");
// To synchronize network provider password with windows password we
// need to call PwdSetPasswordStatus. By synchronizing the network
// provider password we avoid having to change each password separately.
// Network providers listed in registry at:
// HKLM/System\CurrentControlSet\Control\PwdProvider
// Get address of PwdGetPasswordStatus
sPwdSetPasswordStatus =
(LPPwdSetPasswordStatus) GetProcAddress(lib,"PwdSetPasswordStatusA");
if (sPwdSetPasswordStatus == NULL)
DisplayError("GetProcAddress");
res = sPwdSetPasswordStatus("MSNP32", // name of password provider
PS_SYNCMASTERPWD,
PS_SYNCMASTERPWD_ON
);
if (res != WN_SUCCESS)
DisplayError("PwdSetPasswordStatus");
// Must get PwdChangePasswordA because in Windows 95 we are using the
// ascii version of the exported function
sPwdChangePassword =
(LPPwdChangePassword) GetProcAddress(lib,"PwdChangePasswordA");
// Call sPwdChangePassword specifying NULL as network provider.
// This causes windows (non-network) password to be changed and hence
// all synchronized passwords.
res = sPwdChangePassword( NULL, // Network provider
GetDesktopWindow(), //Window to own dialog
0, // No special flag
&s // Address of CHANGEPWDINFO structure
);
if (res != WN_SUCCESS)
DisplayError("PwdChangePassword");
FreeLibrary(lib);
printf("Password = %s\n",s.lpPassword);
return;
}
void DisplayError(char *pszAPI)
{
LPVOID lpvMessageBuffer;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpvMessageBuffer, 0, NULL);
//... now display this string
printf("ERROR: API = %s.\n", pszAPI);
printf(" error code = %d.\n", GetLastError());
printf(" message = %s.\n", (char *)lpvMessageBuffer);
// Free the buffer allocated by the system
LocalFree(lpvMessageBuffer);
ExitProcess(GetLastError());
}
Additional query words: windows password PwdChangePassword domain
Keywords: kbhowto kbapi kbnetwork kbkernbase kbsecurity kbfaq KB177200