2

我的要求是验证用户输入的密码是他登录时使用的正确密码。所以,我写了下面的代码,但它总是说“未登录”。有什么帮助吗?

var
    DomainName,UserName,BackwardSlashString,DomainUserName : String;
    ServerDetailsInputPage : TInputQueryWizardPage;
    hToken, LoginOk : INTEGER; 

function LogonUser(lpszUsername,lpszDomain,lpszPassword: string; 
    dwLogonType,dwLogonProvider: INTEGER; var hToken: INTEGER): INTEGER;
external 'LogonUserA@advapi32.dll stdcall';

procedure InitializeWizard();
begin
    DomainName:= ExpandConstant(GetEnv('USERDOMAIN'));
    UserName := ExpandConstant( +GetUserNameString);
    BackwardSlashString := '\'
    DomainUserName := DomainName + BackwardSlashString + UserName;
    ServerDetailsInputPage := 
       CreateInputQueryPage(wpWelcome,'','','Please enter following data and click Next.');
    ServerDetailsInputPage.Add('IP Address',False);
    ServerDetailsInputPage.Add('Port Number',False);
    ServerDetailsInputPage.Add('Domain Name\User Name',False);
    ServerDetailsInputPage.Add('Password',True);
    ServerDetailsInputPage.Values[1] := '80';
    ServerDetailsInputPage.Values[2] := DomainUserName;
end;

function RunAsUser(): BOOLEAN; 
var
    Passwd : String;
begin
    DomainName := ExpandConstant(GetEnv('USERDOMAIN'));
    UserName := ExpandConstant( +GetUserNameString);
    Passwd := ServerDetailsInputPage.Values[3];
    LoginOk := LogonUser(UserName,DomainName,Passwd,1,0,hToken); 
    if (not (LoginOk=0)) then
    begin
        MsgBox('successfully logged-in', mbInformation, MB_OK);
        Result := true;
    end

    else if (LoginOk=0) then
    begin
        MsgBox('Not logged-in', mbInformation, MB_OK);
        Result := false;
    end;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
    Result := True;

    if CurPageID = ServerDetailsInputPage.ID then
    begin
        if not RunAsUser then
        begin
            MsgBox('Please enter correct Password!', mbError, MB_OK);
            Result:=False;
        end;
    end;
end;

function InitializeSetup(): Boolean;
var
    PrevInstallPath : String;
    ResultCode : Integer;
    FXStopStatus : Boolean;
begin
    Result:=True;
end;
4

1 回答 1

5

起初,您的LogonUser函数原型及其调用都是错误的。您不能混合函数原型的数据类型,也不能在函数调用中使用任意值。你可以改用这样的东西:

[Code]
#ifdef UNICODE
  #define AW "W"
#else
  #define AW "A"
#endif
const  
  LOGON32_LOGON_INTERACTIVE = 2;
  LOGON32_LOGON_NETWORK = 3;
  LOGON32_LOGON_BATCH = 4;
  LOGON32_LOGON_SERVICE = 5;
  LOGON32_LOGON_UNLOCK = 7;
  LOGON32_LOGON_NETWORK_CLEARTEXT = 8;
  LOGON32_LOGON_NEW_CREDENTIALS = 9;

  LOGON32_PROVIDER_DEFAULT = 0;
  LOGON32_PROVIDER_WINNT40 = 2;
  LOGON32_PROVIDER_WINNT50 = 3;

  ERROR_SUCCESS = 0;
  ERROR_LOGON_FAILURE = 1326;

function LogonUser(lpszUsername, lpszDomain, lpszPassword: string;
  dwLogonType, dwLogonProvider: DWORD; var phToken: THandle): BOOL;
  external 'LogonUser{#AW}@advapi32.dll stdcall';

var
  ServerDetailsPage: TInputQueryWizardPage;

function TryLogonUser(const Domain, UserName, Password: string; 
  var ErrorCode: Longint): Boolean;
var
  Token: THandle;
begin
  Result := LogonUser(UserName, Domain, Password, LOGON32_LOGON_INTERACTIVE,
    LOGON32_PROVIDER_DEFAULT, Token);
  ErrorCode := DLLGetLastError;
end;

procedure InitializeWizard;
var
  UserName: string;
begin
  UserName := AddBackslash(GetEnv('USERDOMAIN')) + GetUserNameString;
  ServerDetailsPage := CreateInputQueryPage(wpWelcome, 
    '', '', 'Please enter following data and click Next.');
  ServerDetailsPage.Add('IP Address', False);
  ServerDetailsPage.Add('Port Number', False);
  ServerDetailsPage.Add('Domain Name\User Name', False);
  ServerDetailsPage.Add('Password', True);
  ServerDetailsPage.Values[1] := '80';
  ServerDetailsPage.Values[2] := UserName;
end;

procedure ParseDomainUserName(const Value: string; var Domain,
  UserName: string);
var
  DelimPos: Integer;
begin
  DelimPos := Pos('\', Value);
  if DelimPos = 0 then
  begin
    Domain := '.';
    UserName := Value;
  end
  else
  begin
    Domain := Copy(Value, 1, DelimPos - 1);
    UserName := Copy(Value, DelimPos + 1, MaxInt);
  end;
end;

function ServerDetailsLogonUser: Boolean; 
var
  Domain: string;
  UserName: string;
  Password: string;
  ErrorCode: Longint;
begin
  ParseDomainUserName(ServerDetailsPage.Values[2], Domain, UserName);
  Password := ServerDetailsPage.Values[3];
  Result := TryLogonUser(Domain, UserName, Password, ErrorCode);

  case ErrorCode of
    ERROR_SUCCESS:
      MsgBox('Logon successful!', mbInformation, MB_OK);
    ERROR_LOGON_FAILURE:
      MsgBox('The user name or password is incorrect!', mbError, MB_OK);
  else
    MsgBox('Login failed!' + #13#10 + SysErrorMessage(DLLGetLastError),
      mbError, MB_OK);
  end;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  Result := True;

  if CurPageID = ServerDetailsPage.ID then
    Result := ServerDetailsLogonUser;
end;
于 2013-03-19T14:35:53.423 回答