Einzelnen Beitrag anzeigen

Benutzerbild von cherry
cherry

Registriert seit: 14. Nov 2005
561 Beiträge
 
RAD-Studio 2009 Ent
 
#1

kann dieses Konstrukt überhaupt funktionieren? (Arrays...)

  Alt 28. Jul 2010, 07:00
Delphi-Version: 2009
Hallo Zusammen...

Ich bin grade an einem (Massen)Importassistenten um Benutzer im AD zu erstellen...
nachdem man das Template und die CSV Datei (als Liste mit z.B. Namen, Adressen usw.) und die Felder zugewiesen hat,
soll man nun noch jeden zu erstellenden Benutzer bearbeiten können, bevor diese dann endgültig erstellt werden.

Dazu habe ich mir nachfolgendes "Arraykonstrukt" geschaffen, welches ich mitlerweile schwer anzweifle...
Da ich sowas bisher noch nicht brauchte, habe ich k.A. wo die Kritischen Stellen sind, wenn mir vielleicht mal jemand auf die Sprünge helfen könnte?

Im grossen und ganzen funktioniert das alles eingentlich ganz gut...
Ein Problem tritt aber z.B. nach folgender Verwendung auf:

Man nehme an: TUser wurden erstellt mit dazugehörigen werten. Jetzt will man die Gruppenzugehörigkeiten (AD) bearbeiten...
Im GUI hab ichs mit TListBoxen gelöst, so sieht dann die Funktion aus, die die Gruppenzugehörigkeiten in mein Userobjekt speichert...

Delphi-Quellcode:
procedure TFormWzrEditUser.saveGroupsIntoUser;
var
  I: Integer;
  grps: TGroups;
begin

  // cleanup
  user.ClearGroups; // <--- IMMER NACH DEM ZWEITEN AUFRUF DIESR FUNKTION HIER DER SCHWERWIEGENDE FEHLER...

  // create groups...
  SetLength(grps, ListBox2.Count);
  for I := 0 to ListBox2.Count - 1 do
  begin
    // WITZIG IST: WENN MAN DUMMY STRINGS WIE Z.B. 'TEST' VERWENDET TRITT NIE EIN PROBLEM AUF
    grps[I].name := String(ListBox2.Items.Strings[I]);//Copy(ListBox2.Items.Strings[I], 1, Length(ListBox2.Items.Strings[I]));
    grps[I].ldappath := String(ListBox2.Items.Objects[I]);//Copy(String(ListBox2.Items.Objects[I]), 1, Length(String(ListBox2.Items.Objects[I])));
  end;

  // add groups
  user.AssignGroups(grps);
  SetLength(grps, 0);

end;
Wenn man die Zugehörigkeiten 1 Mal ändert funktionierts... immer beim Zweiten Aufruf von "saveGroupsIntoUser" tritt ein "EInvalidPointer" Üngültige Zeigeroperation auf...
Der Debugger hält immer an dieser Stelle an: in der Systemfunktion "_LStrClr(var S)" bei "CALL _FreeMem"

Hier also noch das angezweifelte Konstrukt selber:

Delphi-Quellcode:
unit UUserCrossTable;

interface

type

  // type for filesettings
  TDirectory = record
    Name: string;
    FullAccess: Boolean;
    Modify: Boolean;
    Execute: Boolean;
    List: Boolean;
    Read: Boolean;
    Write: Boolean;
  end;
  PDirectory = ^TDirectory;
  TDirectories = array of TDirectory;

  // type for optional ad attributes
  TOptAttr = record
    name: string; //string[50]
    value: string;
  end;
  POptAttr = ^TOptAttr;
  TOptAttrs = array of TOptAttr;

  // type for group
  TGroup = record
    name: string;
    ldappath: string;
  end;
  PGroup = ^TGroup;
  TGroups = array of TGroup;

  // type for quota
  TQuota = record
    volume: string;
    limit: Integer;
    Threshold: Integer;
  end;
  PQuota = ^TQuota;
  TQuotas = array of TQuota;

  // THIS IS ONE USER
  TUser = class(TObject)
    private
      arrGroups: TGroups;
      arrDirectories: TDirectories;
      arrQuotas: TQuotas;
      arrOptAttrs: TOptAttrs;
    public
      sAMAccountName: string;
      givenName: string;
      sn: string;
      password: string;
      containerPath: string;

      function Groups: TGroups;
      function AddGroup(group: TGroup): Integer;
      procedure removeGroup(index: Integer);
      function GroupCount: Integer;
      procedure AssignGroups(Groups: TGroups);
      procedure ClearGroups;

      function Directories: TDirectories;
      function AddDirectory(directory: TDirectory): Integer;
      procedure removeDirectory(index: Integer);
      function DirectoryCount: Integer;
      procedure AssignDirectories(Directories: TDirectories);
      procedure ClearDirectories;

      function Quotas: TQuotas;
      function AddQuota(quota: TQuota): Integer;
      procedure removeQuota(index: Integer);
      function QuotaCount: Integer;
      procedure AssignQuotas(Quotas: TQuotas);
      procedure ClearQuotas;

      function OptionalAttributes: TOptAttrs;
      function AddOptionalAttribute(attribute: TOptAttr): Integer;
      procedure removeOptionalAttribute(index: Integer);
      function OptionalAttributeCount: Integer;
      procedure AssignOptionalAttributes(Attributes: TOptAttrs);
      procedure ClearOptionalAttributes;

  end;
  TUsers = array of TUser;

  // THIS IS THE TABLE CLASS WHO HANDLES THE ENTRIES
  TUserCrossTable = class(TObject)
    private
      arrUsers: TUsers;
    public
      function Users: TUsers;
      function addUser(user: TUser): Integer;
      procedure removeUser(index: Integer);
      function UserCount: Integer;

// procedure SaveToFile(name: string);
// procedure LoadFromFile(name: string);
  end;

implementation

//------------------------------------------------------------------------------
// CLASS USER
//------------------------------------------------------------------------------

// GET ALL GROUPS
function TUser.Groups: TGroups;
begin
  result := arrGroups;
end;

// ADD NEW GROUP
function TUser.AddGroup(group: TGroup): Integer;
begin
  SetLength(arrGroups, Length(arrGroups)+1);
  arrGroups[Length(arrGroups)-1] := group;
  result := Length(arrGroups)-1;
end;

// REMOVE A GROUP
procedure TUser.removeGroup(index: Integer);
begin
  if index < 0 then exit;
  if index > Length(arrGroups) then exit;
  if index = Length(arrGroups) then
  begin
    SetLength(arrGroups, Length(arrGroups)-1);
    Exit;
  end;
  System.Move(arrGroups[index+1], arrGroups[index], (Length(arrGroups) - index - 1) * SizeOf(TGroup) + 1 ); // move
  SetLength(arrGroups, Length(arrGroups)-1); // set new length
  Exit; // exit
end;

// GET GROUP COUNT
function TUser.GroupCount;
begin
  result := Length(arrGroups);
end;

// ASSIGN GROUPS
procedure TUser.AssignGroups(Groups: TGroups);
begin
  arrGroups := Copy(Groups, 0, Length(Groups));
end;

// CLEAR GROUPS
procedure TUser.ClearGroups;
begin
  SetLength(arrGroups, 0);
end;

// GET ALL DIRECTORIES
function TUser.Directories: TDirectories;
begin
  result := arrDirectories;
end;

// ADD NEW DIRECTORY
function TUser.AddDirectory(directory: TDirectory): Integer;
begin
  SetLength(arrDirectories, Length(arrDirectories)+1);
  arrDirectories[Length(arrDirectories)-1] := directory;
  result := Length(arrDirectories)-1;
end;

// REMOVE A DIRECTORY
procedure TUser.removeDirectory(index: Integer);
begin
  if index < 0 then exit;
  if index > Length(arrDirectories) then exit;
  if index = Length(arrDirectories) then
  begin
    SetLength(arrDirectories, Length(arrDirectories)-1);
    Exit;
  end;
  System.Move(arrDirectories[index+1], arrDirectories[index], (Length(arrDirectories) - index - 1) * SizeOf(TDirectory) + 1 ); // move
  SetLength(arrDirectories, Length(arrDirectories)-1); // set new length
  Exit; // exit
end;

// GET DIRECOTRY COUNT
function TUser.DirectoryCount;
begin
  result := Length(arrDirectories);
end;

// ASSIGN DIRECTORIES
procedure TUser.AssignDirectories(Directories: TDirectories);
begin
  arrDirectories := Copy(Directories, 0, Length(Directories));
end;

// CLEAR DIRECTORIES
procedure TUser.ClearDirectories;
begin
  SetLength(arrDirectories, 0);
end;

// GET ALL QUOTAS
function TUser.Quotas: TQuotas;
begin
  result := arrQuotas;
end;

// ADD NEW QUOTA
function TUser.AddQuota(quota: TQuota): Integer;
begin
  SetLength(arrQuotas, Length(arrQuotas)+1);
  arrQuotas[Length(arrQuotas)-1] := quota;
  result := Length(arrQuotas)-1;
end;

// REMOVE A QUOTA
procedure TUser.removeQuota(index: Integer);
begin
  if index < 0 then exit;
  if index > Length(arrQuotas) then exit;
  if index = Length(arrQuotas) then
  begin
    SetLength(arrQuotas, Length(arrQuotas)-1);
    Exit;
  end;
  System.Move(arrQuotas[index+1], arrQuotas[index], (Length(arrQuotas) - index - 1) * SizeOf(TQuota) + 1 ); // move
  SetLength(arrQuotas, Length(arrQuotas)-1); // set new length
  Exit; // exit
end;

// GET QUOTA COUNT
function TUser.QuotaCount;
begin
  result := Length(arrQuotas);
end;

// ASSIGN QUOTA
procedure TUser.AssignQuotas(Quotas: TQuotas);
begin
  arrQuotas := Copy(Quotas, 0, Length(Quotas));
end;

// CLEAR QUOTAS
procedure TUser.ClearQuotas;
begin
  SetLength(arrQuotas, 0);
end;

// GET ALL OPTIONAL ATTRIBUTES
function TUser.OptionalAttributes: TOptAttrs;
begin
  result := arrOptAttrs;
end;

// ADD NEW OPTIONAL ATTRIBUTE
function TUser.AddOptionalAttribute(attribute: TOptAttr): Integer;
begin
  SetLength(arrOptAttrs, Length(arrOptAttrs)+1);
  arrOptAttrs[Length(arrOptAttrs)-1] := attribute;
  result := Length(arrOptAttrs)-1;
end;

// REMOVE A OPTIONAL ATTRIBUTE
procedure TUser.removeOptionalAttribute(index: Integer);
begin
  if index < 0 then exit;
  if index > Length(arrOptAttrs) then exit;
  if index = Length(arrOptAttrs) then
  begin
    SetLength(arrOptAttrs, Length(arrOptAttrs)-1);
    Exit;
  end;
  System.Move(arrOptAttrs[index+1], arrOptAttrs[index], (Length(arrOptAttrs) - index - 1) * SizeOf(TOptAttr) + 1 ); // move
  SetLength(arrOptAttrs, Length(arrOptAttrs)-1); // set new length
  Exit; // exit
end;

// GET OPTIONAL ATTRIBUTE COUNT
function TUser.OptionalAttributeCount;
begin
  result := Length(arrOptAttrs);
end;

// ASSIGN OPTIONAL ATTRIBUTES
procedure TUser.AssignOptionalAttributes(Attributes: TOptAttrs);
begin
  arrOptAttrs := Copy(Attributes, 0, Length(Attributes));
end;

// CLEAR OPTIONAL ATTRIBUTES
procedure TUser.ClearOptionalAttributes;
begin
  SetLength(arrOptAttrs, 0);
end;

//------------------------------------------------------------------------------
// CLASS USER CROSS TABLE
//------------------------------------------------------------------------------

// GET ALL USERS
function TUserCrossTable.Users: TUsers;
begin
  result := arrUsers;
end;

// ADD AN USER
function TUserCrossTable.addUser(user: TUser): Integer;
begin
  SetLength(arrUsers, Length(arrUsers)+1);
  arrUsers[Length(arrUsers)-1] := user;
  result := Length(arrUsers)-1;
end;

// REMOVE AN USER
procedure TUserCrossTable.removeUser(index: Integer);
begin
  if index < 0 then exit;
  if index > Length(arrUsers) then exit;
  if index = Length(arrUsers) then
  begin
    SetLength(arrUsers, Length(arrUsers)-1);
    Exit;
  end;
  Finalize(arrUsers[index]);
  System.Move(arrUsers[index+1], arrUsers[index], (Length(arrUsers) - index - 1) * SizeOf(TUser) + 1 ); // move
  Pointer(arrUsers[Length(arrUsers)-1]) := nil; // set nil
  SetLength(arrUsers, Length(arrUsers)-1); // set new length
  Exit; // exit
end;

// GET USER COUNT
function TUserCrossTable.UserCount;
begin
  result := Length(arrUsers);
end;

end.
Ist das nur mein Gefühl, oder ist die ganze Welt verrückt geworden!?
  Mit Zitat antworten Zitat