Einzelnen Beitrag anzeigen

Benutzerbild von Remko
Remko

Registriert seit: 10. Okt 2006
Ort: 's-Hertogenbosch, Die Niederlande
222 Beiträge
 
RAD-Studio 2010 Arc
 
#3

Re: Probleme mit ADSI

  Alt 3. Jun 2008, 22:36
Posted a sample using Jwa yesterday: http://www.delphipraxis.net/internal...t.php?t=135542

This is a snippet from one of my old programs that enumerates all objects in the Active Directory into a treeview
Delphi-Quellcode:
uses
   ActiveDS_TLB, AdsHlp, ActiveX, ComObj; // AdsHlp is a helper unit from agnisoft

...

procedure TADTreeThread.Execute;
var hr: hResult;
  objrootDSE: IADS;
  objContainer: IADS;
  sPath: string;
begin
  CoInitialize(nil);
  try
    hr := ADsGetObject(WideString('LDAP://rootDSE'), IID_IADS, objrootDSE);
  except
    on E: Exception do
    begin
     Exit;
    end;
  end;
  if Failed(hr) then exit;
  sPath := Format('LDAP://%s', [VarToStr(objrootDSE.Get('defaultNamingContext'))]);
  hr := ADsGetObject(sPath, IADS, objContainer);
  if Failed(hr) then exit;
  fRootNode := fADTree.Items.Add(nil, StringReplace(objContainer.Name, 'DC=', '', [rfReplaceAll]));
  with fRootNode do
  begin
    Data := TNodeData.Create;
    TNodeData(Data).Filled := True;
    TNodeData(Data).sAdsPath := sPath;
    TNodeData(Data).sClass := objContainer.Class_;
    ImageIndex := frMain.Icons.AD;
  end;
  EnumAndAdd(fRootNode, (objContainer as IADSContainer), VarArrayOf(['organizationalUnit', 'container']));
  objContainer := nil;
  objrootDSE := nil;
  CoUnInitialize;
end;

procedure TADTreeThread.EnumAndAdd(ParentNode: TTreeNode; objContainer: IADSContainer; Filter: OleVariant);
var Enum: IEnumVariant;
  Fetched: Cardinal;
  ResultItem: OleVariant;
  objAD: IADS;
  NewNode: TTreeNode;
begin
// fADTree.Items.BeginUpdate;
  if Assigned(objContainer) then
  begin
    objContainer.Filter := Filter;
    Enum := objContainer._Newenum as IEnumVariant;
    Enum.Reset;
    Enum.Next(1, ResultItem, Fetched);
    while Fetched = 1 do begin
      objAD := IDispatch(ResultItem) as IADS;
      if objAD.Class_ = 'userthen
      begin
        with fADTree.Items.AddChild(ParentNode, StringReplace(StringReplace(objAD.Name, 'CN=', '', [rfIgnoreCase]), '\,', '', [rfIgnoreCase])) do
        begin
          ImageIndex := IconRecord.User;
          SelectedIndex := IconRecord.User;
          Data := TNodeData.Create;
          TNodeData(Data).sAdsPath := objAD.ADsPath;
          TNodeData(Data).sClass := objAD.Class_;
          TNodeData(Data).Filled := True;
        end;
      end
      else if objAD.Class_ = 'groupthen
      begin
        with fADTree.Items.AddChild(ParentNode, StringReplace(objAD.Name, 'CN=', '', [rfIgnoreCase])) do
        begin
          ImageIndex := IconRecord.Users;
          SelectedIndex := IconRecord.Users;
          Data := TNodeData.Create;
          TNodeData(Data).sAdsPath := objAD.ADsPath;
          TNodeData(Data).sClass := objAD.Class_;
          TNodeData(Data).Filled := True;
        end;
      end
      else if objAD.Class_ = 'organizationalUnitthen
      begin
        NewNode := FADTree.Items.AddChild(ParentNode, StringReplace(objAD.Name, 'OU=', '', [rfIgnoreCase]));
        with NewNode do
        begin
          ImageIndex := IconRecord.ADOU;
          SelectedIndex := IconRecord.ADOU;
          Data := TNodeData.Create;
          TNodeData(Data).sAdsPath := objAD.ADsPath;
          TNodeData(Data).sClass := objAD.Class_;
          FADTree.Items.AddChild(NewNode, 'dummy');
          if NewNode <> nil then
          begin
// EnumAndAdd(NewNode, (objAD as IADSContainer), Filter);
          end;
        end;
      end
      else if objAD.Class_ = 'containerthen
      begin
        NewNode := fADTree.Items.AddChild(ParentNode, StringReplace(objAD.Name, 'CN=', '', [rfIgnoreCase]));
        with NewNode do
        begin
          ImageIndex := IconRecord.ADCont;
          SelectedIndex := IconRecord.ADCont;
          Data := TNodeData.Create;
          TNodeData(Data).sAdsPath := objAD.ADsPath;
          TNodeData(Data).sClass := objAD.Class_;
// EnumAndAdd(NewNode, (objAD as IADSContainer), Filter);
          fADTree.Items.AddChild(NewNode, 'dummy');
        end;
      end;
      Enum.Next(1, ResultItem, Fetched);
    end;
  NewNode := nil;
  NewNode.Free;
  objAD := nil;
  end;
// fADTree.Items.EndUpdate;
end;
  Mit Zitat antworten Zitat