NetUserChangePassword调用失败? (50分)

  • 主题发起人 主题发起人 southbird
  • 开始时间 开始时间
S

southbird

Unregistered / Unconfirmed
GUEST, unregistred user!
于win2k上调用更改用户密码的操作;<br>代码如下:<br>function NetUserChangePassword ;external 'Netapi32.dll' name 'NetUserChangePassword' ;<br>procedure TForm1.Button2Click(Sender: TObject);<br>var server, user,oldpass,newpass :PWideChar ;<br>&nbsp; &nbsp; ret:integer;<br>begin<br>&nbsp; &nbsp; GetMem(server,100);<br>&nbsp; &nbsp; GetMem(user,100);<br>&nbsp; &nbsp; GetMem(oldpass,100);<br>&nbsp; &nbsp; GetMem(newpass,100);<br>&nbsp; &nbsp; StringToWideChar(Edit5.text,user,length(Edit5.text)+1 );<br>&nbsp; &nbsp; StringToWideChar(Edit6.text,oldpass,length(Edit6.text)+1 );<br>&nbsp; &nbsp; StringToWideChar(Edit7.text,newpass,length(Edit7.text)+1 &nbsp;);<br>&nbsp; &nbsp; StringToWideChar('',server,50 );<br>&nbsp; &nbsp; ret :=NetUserChangePassword(server,user,oldpass,newpass);<br>&nbsp; if ret&gt;0 &nbsp;then<br>&nbsp; &nbsp; &nbsp;showmessage('erro') ;<br>end;<br>似乎无法成功修改用户密码(操作用户为管理员);那位高手能解释一下?<br>不好意思,我分不够,只能送50分了!<br>
 
NetUserChangePassword<br>The NetUserChangePassword function changes a user's password for a specified network server or domain.<br><br>Security Requirements<br>Windows NT: A server or domain can be configured to require a user to log on before changing the password on a user account. In that case, only members of the Administrators or Account Operators local group or the user can change the password for a user account. If logon is not required, a user can change the password for any user account, as long as the user knows the current password.<br><br>Windows 2000: If you call this function on a Windows 2000 domain controller that is running Active Directory, access is allowed or denied based on the access-control list (ACL) for the securable object. The default ACL permits only Administrators and account operators to call this function. On a member server or workstation, only Administrators and account operators can call this function. For more information, see Security Requirements for the Network Management Functions.<br><br>NET_API_STATUS NetUserChangePassword(<br>&nbsp; LPCWSTR domainname, &nbsp;<br>&nbsp; LPCWSTR username, &nbsp; &nbsp;<br>&nbsp; LPCWSTR oldpassword, <br>&nbsp; LPCWSTR newpassword &nbsp;<br>);<br>Parameters<br>domainname <br>[in] Pointer to a constant null-terminated Unicode string that specifies the name of a remote server or domain. The NetUserChangePassword function changes the user's password on the specified remote server or domain. <br>If this parameter is NULL, the logon domain of the caller is used. <br><br>username <br>[in] Pointer to a constant null-terminated Unicode string that specifies a user name. The NetUserChangePassword function changes the password for the specified user. <br>If this parameter is NULL, the logon name of the caller is used. <br><br>oldpassword <br>[in] Pointer to a constant null-terminated Unicode string that specifies the user's old password. <br>newpassword <br>[in] Pointer to a constant null-terminated Unicode string that specifies the user's new password. <br>Return Values<br>If the function succeeds, the return value is NERR_Success.<br><br>If the function fails, the return value can be one of the following error codes.<br><br>Value Meaning <br>ERROR_ACCESS_DENIED The user does not have access to the requested information. <br>ERROR_INVALID_PASSWORD &nbsp;The user has entered an invalid password. <br>NERR_InvalidComputer The computer name is invalid. <br>NERR_NotPrimary The operation is allowed only on the primary domain controller of the domain. <br>NERR_UserNotFound The user name could not be found. <br>NERR_PasswordTooShort &nbsp;The password is shorter than required. (The password could also be too long, be too recent in its change history, not have enough unique characters, or not meet another password policy requirement.) <br><br><br>Remarks<br>In some cases, the process that calls the NetUserChangePassword function must also have the SE_CHANGE_NOTIFY_NAME privilege enabled; otherwise, NetUserChangePassword fails and GetLastError returns ERROR_ACCESS_DENIED. This privilege is not required for the local system account or for accounts that are members of the administrators group. By default, SE_CHANGE_NOTIFY_NAME is enabled for all users, but some administrators may disable the privilege for everyone. For more information about account privileges, see Privileges. <br><br>The following code sample demonstrates how to change a user's password with a call to the NetUserChangePassword function. All parameters to the function are required.<br><br>#ifndef UNICODE<br>#define UNICODE<br>#endif<br><br>#include &lt;stdio.h&gt;<br>#include &lt;windows.h&gt; <br>#include &lt;lm.h&gt;<br><br>int wmain(int argc, wchar_t *argv[])<br>{<br>&nbsp; &nbsp;DWORD dwError = 0;<br>&nbsp; &nbsp;NET_API_STATUS nStatus;<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// All parameters are required.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;if (argc != 5)<br>&nbsp; &nbsp;{<br>&nbsp; &nbsp; &nbsp; fwprintf(stderr, L"Usage: %s ////ServerName UserName OldPassword NewPassword/n", argv[0]);<br>&nbsp; &nbsp; &nbsp; exit(1);<br>&nbsp; &nbsp;}<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Call the NetUserChangePassword function.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;nStatus = NetUserChangePassword(argv[1], argv[2], argv[3], argv[4]);<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// If the call succeeds, inform the user.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;if (nStatus == NERR_Success)<br>&nbsp; &nbsp; &nbsp; fwprintf(stderr, L"User password has been changed successfully/n");<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;// Otherwise, print the system error.<br>&nbsp; &nbsp;//<br>&nbsp; &nbsp;else<br>&nbsp; &nbsp; &nbsp; fprintf(stderr, "A system error has occurred: %d/n", nStatus);<br><br>&nbsp; &nbsp;return 0;<br>}<br>See Forcing a User to Change the Logon Password for a code sample that demonstrates how to force a user to change the logon password on the next logon using the NetUserGetInfo and NetUserSetInfo functions.<br><br>If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management user functions. For more information, see IADsUser and IADsComputer.<br><br>Windows 95/98: You can use the PwdChangePassword function to change a user's Windows logon password on Windows 95 and Windows 98. The PwdChangePassword function is defined in the header file Pwdspi.h, which is distributed with the Windows 95 Driver Development Kit (DDK). For more information about PwdChangePassword, see the Windows 95 DDK documentation. For more information about designing a routine to change domain passwords on Windows 95 and Windows 98, as well as a code sample, see the following article in the Microsoft Knowledge Base: <br><br>ARTICLE ID: Q177200<br>TITLE: HOWTO: Programmatically Change Network Password Under Windows 95. <br><br>
 
lyywy:<br>我查过MSDN,知道那些注释和SAMPLE,但那是C语言下的做法,<br>delphi下要解决的问题就多一些!<br>希望能得到解释!
 
这次没问题了,我在win2000下试验通过。[:D][:D][:D][:D][:D][:D]<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; Label2: TLabel;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; Edit2: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br>&nbsp; function NetUserChangePassword(A:PWideChar;B:PWideChar;c:PWideChar;d:PWideChar):integer;stdcall;external 'Netapi32.dll';<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br>procedure TForm1.Button1Click(Sender: TObject);<br>var oldpass,newpass :PWideChar ;<br>&nbsp; &nbsp; ret:integer;<br>begin<br>&nbsp; &nbsp; GetMem(oldpass,50);<br>&nbsp; &nbsp; GetMem(newpass,50);<br>&nbsp; &nbsp; <br>&nbsp; &nbsp; StringToWideChar(Edit1.text,oldpass,length(Edit1.text)+1 );<br>&nbsp; &nbsp; StringToWideChar(Edit2.text,newpass,length(Edit2.text)+1 &nbsp;);<br>&nbsp; &nbsp; showmessage(WideCharToString(newpass));<br>&nbsp; &nbsp; ret :=NetUserChangePassword(nil,nil,oldpass,newpass);<br>&nbsp; &nbsp; case (ret) of<br>&nbsp; &nbsp; &nbsp; ERROR_ACCESS_DENIED:label1.caption:='The user does not have access to the requested information';<br>&nbsp; &nbsp; &nbsp; //NERR_InvalidComputer:label1.caption:='The computer name is invalid.';<br>&nbsp; &nbsp; &nbsp; //NERR_NotPrimary:label1.caption:='The operation is allowed only on the primary domain controller of the domain.';<br>&nbsp; &nbsp; &nbsp; //NERR_UserNotFound:label1.caption:='The user name could not be found.';<br>&nbsp; &nbsp; &nbsp; //NERR_PasswordTooShort:label1.caption:='The password is shorter than required.';<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; label2.Caption:=inttostr(ret);<br>end;<br><br><br>end.
 
lyywy :<br>我再研究了一下,<br>发现问题出在 调用 NetUserChangePassword 时的第一个参数上,<br>当对本机上的用户进行修改时, msdn里提示"If this parameter is NULL,<br>&nbsp;the logon domain of the caller is used. ",应该传入空指针 nil ,而不是空字符串的<br>开始地址。<br>&nbsp;lyywy,调用时 ret :=NetUserChangePassword(nil,nil,oldpass,newpass);<br>&nbsp;第二个参数传入的也是nil,我不知道这样会更改哪个用户的口令了?<br>不过还是谢谢你了!<br>送分了!
 
送分了!
 
to southbird:<br>我已admin登陆的,第二个参数为空,该的就是admin用户的密码
 
后退
顶部