我正在使用Delphi pascal ver。 4 我有一个用户首字母,需要从首字母获取活动目录显示名称 所以我不想改变AD只读 - 一个函数将是这样的好:
fullname, user :string;
user:='DKTB'
(通话功能)
fullname:=getAdDispayName(user);
(调用之后fullname ='Torben Bagge')
我使用谷歌找到它但只能在C中找到它而不是pascal。
答案 0 :(得分:3)
您可以使用IDirectorySearch界面轻松完成此操作。我为你做了一个简单的例子(不要忘记添加适当的错误处理):
uses
ActiveX,
JwaAdsTlb, JwaActiveDS; // From Jedi ApiLib
function GetADDisplayName(const Username: String): String;
var
hr: HRESULT;
DirSearch: IDirectorySearch;
SearchInfo: ADS_SEARCHPREF_INFO;
hSearch: ADS_SEARCH_HANDLE;
col: ADS_SEARCH_COLUMN;
Filter: String;
Attributes: array of PChar;
begin
Result := 'Undefined Result';
// Initialize COM
CoInitialize(nil);
try
// Change line below with your domain name
hr := ADsGetObject('LDAP://dc=contoso,dc=com',
IID_IDirectorySearch, Pointer(DirSearch));
Win32Check(Succeeded(hr));
SearchInfo.dwSearchPref := ADS_SEARCHPREF_SEARCH_SCOPE;
SearchInfo.vValue.dwType := ADSTYPE_INTEGER;
SearchInfo.vValue.Integer := ADS_SCOPE_SUBTREE;
hr := DirSearch.SetSearchPreference(@SearchInfo, 1);
Win32Check(Succeeded(hr));
Filter := Format('(&(objectClass=user)(sAMAccountName=%s))',
[Username]);
SetLength(Attributes, 1);
Attributes[0] := 'displayName';
// When using Dynamic Array with WinApi ALWAYS use pointer to first element!
hr := DirSearch.ExecuteSearch(PChar(Filter), @Attributes[0],
Length(Attributes), hSearch);
Win32Check(Succeeded(hr));
try
hr := DirSearch.GetFirstRow(hSearch);
Win32Check(Succeeded(hr));
hr := DirSearch.GetColumn(hSearch, Attributes[0], col);
if Succeeded(hr) then
begin
Result := col.pADsValues^.CaseIgnoreString;
DirSearch.FreeColumn(@col);
end;
finally
DirSearch.CloseSearchHandle(hSearch);
end;
finally
// UnInitialize COM
CoUninitialize;
end;
end;