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_ = '
user'
then
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_ = '
group'
then
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_ = '
organizationalUnit'
then
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_ = '
container'
then
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;