Paradox table password

Peter Laman delphi-db@elists.org
Fri, 12 Oct 2001 12:13:34 +0200


Here is my routine for doing this. You need to use the following units: BDE,
DbTables.

procedure TParadoxPassword.ChangeParadoxPassword(
            const TableName, OldPassword, NewPassword: string; Encrypt:
Boolean;
            Session: TSession);
var
  DBase: TDatabase;
  TableDescriptor: CRTblDesc;
  Sts: DBIResult;
  MaxTries: Integer;
  RemovePwd: Boolean;
begin

  { A password only works if encryption is enabled. The only reason the
Encrypt
    parameter exists is to specify whether encryption should take place if
no
    password is specified }
  if Length(NewPassword) > 0 then
    Encrypt := True;

  { If no session specified, use default session }
  if not Assigned(Session) then
    Session := DbTables.Session;

  { Open the database (because we need a handle) }
  DBase := Session.OpenDatabase(ExtractFilePath(TableName));
  try

    { The old password is required for accessing the old table. So add it to
the
      session }
    RemovePwd := Length(OldPassword) > 0;
    if RemovePwd then
      Session.AddPassword(OldPassword);
    try

      { Initialize table descriptor }
      with TableDescriptor do
      begin
        FillChar(TableDescriptor, SizeOf(TableDescriptor), 0);
        AnsiToNative(Session.Locale, ExtractFileName(TableName), szTblName,
          DBIMAXTBLNAMELEN);
        AnsiToNative(Session.Locale, 'PARADOX', szTblType, DBIMAXNAMELEN);
        AnsiToNative(Session.Locale, NewPassword, szPassword,
DBIMAXNAMELEN);
        bProtected := Encrypt;
      end;

      { Apply the password to the table }
      MaxTries := 2;
      while MaxTries > 0 do
      begin
        Sts := DbiDoRestructure(DBase.Handle, 1, @TableDescriptor, nil, nil,
nil,
          False);
        if Sts = DBIERR_NONE then
          MaxTries := 0
        else
          begin

            { If DbiDoRestructure returns DBIERR_NOTSUFFTABLERIGHTS, there
is no
              correct old password available, either because the caller
didn't
              specify one, or because it was wrong and the valid password
has not
              yet been added to the session }
            if (Sts <> DBIERR_NOTSUFFTABLERIGHTS) or
               not Session.GetPassword
            then
              Check(Sts);
            Dec(MaxTries);
          end;
      end; { while MaxTries > 0 }
    finally
      if RemovePwd then
        Session.RemovePassword(OldPassword);
    end;
  finally
    Session.CloseDatabase(DBase);
  end;
end;


Enjoy!!

Peter Laman
Lance IT B.V. Roermond, the Netherlands
Developers of Protecs Safety Management for the Chemical Industry.
drs. Peter Laman,
Senior Software Engineer.
Tel.    +31 475 33 17 24
Fax.    +31 475 33 56 83
E-mail: peter@lance.nl
Web:    http://www.lance.nl