Delphi中的Active Directory

时间:2013-10-27 14:16:13

标签: delphi active-directory

*我使用此代码:  但是在ADS_SEARCH_HANDLE上是错误的 任何人都可以帮助我吗?*

代码:

unit Unapp;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TFormApp = class(TForm)
    Button1: TButton;
  private
     function GetADDisplayName(const Username: String): String;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormApp: TFormApp;

implementation
uses
  ActiveX,
  JwaAdsTlb, JwaActiveDS; // From Jedi ApiLib

{$R *.dfm}
function TFormApp.GetADDisplayName(const Username: String): String;
var
  hr: HRESULT;
  DirSearch: IDirectorySearch;
  SearchInfo: ADS_SEARCHPREF_INFO;
  hSearch: ADS_SEARCH_HANDLE; // ************this has error**************
  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=Tbco,dc=com',
      IID_IDirectorySearch, Pointer(DirSearch));
    Win32Check(Succeeded(hr));

    SearchInfo.dwSearchPref   := ADS_SEARCHPREF_SEARCH_SCOPE;
    SearchInfo.vValue.dwType  := ADSTYPE_INTEGER;
    SearchInfo.vValue.dwType := 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(PWideChar(Filter),PWideChar(Attributes[0]),Length(Attributes), Pointer(hSearch));
    Win32Check(Succeeded(hr));

    try
      hr := DirSearch.GetFirstRow(Pointer(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;

end.

德尔福错误; [错误] Unapp.pas(33):未声明的标识符:'ADS_SEARCH_HANDLE' [错误] Unapp.pas(70):实际和正式var参数的类型必须相同 [错误] Unapp.pas(70):不兼容的类型:'Char'和'WideChar' [错误] Unapp.pas(73):未声明的标识符:'CaseIgnoreString' [错误] Unapp.pas(74):实际和正式var参数的类型必须相同 [错误] Unapp.pas(77):实际和正式var参数的类型必须相同 [致命错误] ProjApp.dpr(5):无法编译使用过的单位'Unapp.pas'

1 个答案:

答案 0 :(得分:0)

看起来DirSearch未初始化

使用此代码进行初始化

    AdsGetObject(PWideChar('LDAP://YourDomain'), IDirectorySearch, DirSearch);

不要忘记将YourDomain替​​换为您的实际域名