本文介绍了如何阅读AD显示名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用德尔福的Pascal版本。 4我有一个用户名缩写,需要从英文缩写获取活动目录显示名称所以我不希望改变AD刚读 - 功能将是很好的这样的:

I am using Delphi pascal ver. 4I have a user initials and need to get the active directory display name from the initialsso I do not want to change AD just read - a function will be nice like this:

fullname, user :string;
user:='DKTB'

(通话功能)

(call function)

fullname:=getAdDispayName(user);

(通话后则全名='托本·巴格')

(after call then fullname = 'Torben Bagge')

我已经使用谷歌找到它,但只能够找到它在C和不帕斯卡。

I have used google to find it but was only able to find it in C and not pascal.

推荐答案

您可以使用IDirectorySearch接口很容易地做到这一点。我做了一个简单的例子给你(不要忘了添加适当的错误处理):

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;

这篇关于如何阅读AD显示名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 00:26