我正在使用德尔福帕斯卡版本。4 我有一个用户姓名首字母,需要从姓名首字母中获取活动目录显示名称,所以我不想更改 AD 只是读取 - 一个函数会很好,如下所示:
fullname, user :string;
user:='DKTB'
(调用函数)
fullname:=getAdDispayName(user);
(通话后全名 = 'Torben Bagge')
我用谷歌找到它,但只能在 C 中找到它,而不是帕斯卡。
我正在使用德尔福帕斯卡版本。4 我有一个用户姓名首字母,需要从姓名首字母中获取活动目录显示名称,所以我不想更改 AD 只是读取 - 一个函数会很好,如下所示:
fullname, user :string;
user:='DKTB'
(调用函数)
fullname:=getAdDispayName(user);
(通话后全名 = 'Torben Bagge')
我用谷歌找到它,但只能在 C 中找到它,而不是帕斯卡。
You can easily do this using the IDirectorySearch Interface. I made an quick example for you (don't forget to add proper error handling):
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;