我刚写的一个ldap范例(实际上实现了Ldap浏览的功能了), 我想说明已经比较详细了, 如果需要, 我可以把源程序发给你.
另外, 如果你有IP*Works的注册版(for Delphi or ActiveX version), 可否也给我一份,
我找了好久都没有找到啊... 我用的试用版做的范例.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, XPMan, ipwcore, ipwldap,
ExtCtrls, OleCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
// 服务器地址
edtServer: TEdit;
// 搜索的BaseDN
edtBasedn: TEdit;
// 绑定服务器使用的用户名
edtUsername: TEdit;
// 绑定服务器使用的密码
edtPassword: TEdit;
// 绑定按钮
btnBind: TButton;
XPManifest1: TXPManifest;
ldap: TipwLDAP;
Panel2: TPanel;
lvLdap: TListView;
tvLdap: TTreeView;
Splitter1: TSplitter;
// 绑定ldap服务器
procedure btnBindClick(Sender: TObject);
// 点击TreeView时在ListView中显示Ldap项的内容
procedure tvLdapClick(Sender: TObject);
// ldap搜索结果显示.
// 如果搜索范围是ssBaseObject, 则在Listview中显示属性和内容
// 否则在Treeview中展开添加搜索到的节点.
procedure ldapSearchResult(Sender: TObject; MessageId: Integer;
const DN: String);
// 双击Treeview时查询Ldap子节点
procedure tvLdapDblClick(Sender: TObject);
// 设置活动节点
procedure tvLdapMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ Private declarations }
// 当前活动节点
nodeParent: TTreeNode;
// 根据节点递归生成DN
// @param aNode 当前活动节点
function GetDN(aNode: TTreeNode): String;
// 获取DN的前面部分
// @param DN 搜索到的DN值
function GetBaseName(DN: String): String;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnBindClick(Sender: TObject);
begin
if btnBind.Caption = '&Bind' then
begin
ldap.ServerName := edtServer.Text;
if edtPassword.Text <> '' then
begin
ldap.DN := edtUsername.Text;
ldap.Password := edtPassword.Text;
end;
ldap.Bind;
ldap.DN := edtBasedn.Text;
ldap.SearchScope := ssSingleLevel;
ldap.SearchFilter := 'objectclass=*';
nodeParent := tvLdap.Items.AddChildFirst(nil, edtBasedn.Text);
ldap.Search;
btnBind.Caption := 'Un&bind';
end
else
begin
btnBind.Caption := '&Bind';
tvLdap.Items.Clear;
lvLdap.Items.Clear;
ldap.Unbind;
end;
end;
procedure TForm1.tvLdapClick(Sender: TObject);
begin
if tvLdap.Selected <> nil then
begin
lvLdap.Items.Clear;
ldap.DN := GetDN(tvLdap.Selected);
ldap.SearchScope := ssBaseObject;
ldap.SearchFilter := 'ObjectClass=*';
ldap.AttrCount := 0;
ldap.Search;
end;
end;
procedure TForm1.ldapSearchResult(Sender: TObject; MessageId: Integer;
const DN: String);
var
i: Integer;
item: TListItem;
sAttr: String;
begin
If ldap.SearchScope = ssBaseObject Then
begin
for i := 0 to (ldap.AttrCount - 1) do
begin
if ldap.AttrType <> '' then
sAttr := ldap.AttrType;
item := lvLdap.Items.Add;
item.Caption := sAttr;
item.SubItems.Add(UTF8Decode(ldap.AttrValue));
end;
end
Else
begin
tvLdap.Items.AddChild(nodeParent, GetBaseName(DN));
nodeParent.Expanded := True;
End;
end;
procedure TForm1.tvLdapDblClick(Sender: TObject);
begin
if tvLdap.Selected <> nil then
begin
tvLdap.Selected.DeleteChildren;
ldap.DN := GetDN(tvLdap.Selected);
ldap.SearchScope := ssSingleLevel;
ldap.SearchFilter := 'ObjectClass=*';
ldap.AttrCount := 0;
ldap.Search;
end;
end;
procedure TForm1.tvLdapMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
aNode: TTreeNode;
begin
aNode := tvLdap.GetNodeAt(X, Y);
if aNode <> nil then
begin
nodeParent := aNode;
nodeParent.Selected := True;
end;
end;
function TForm1.GetDN(aNode: TTreeNode): String;
begin
if aNode <> nil then
begin
if aNode.Parent <> nil then
Result := aNode.Text + ',' + GetDN(aNode.Parent)
else
Result := aNode.Text;
end
else
Result := '';
end;
function TForm1.GetBaseName(DN: String): String;
begin
Result := Copy(DN, 1, Pos(',', DN)-1);
end;
end.