0
votes

*i used this code : but ther is an errore on ADS_SEARCH_HANDLE can any body help me?*

Code :

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.

Delphi Error; [Error] Unapp.pas(33): Undeclared identifier: 'ADS_SEARCH_HANDLE' [Error] Unapp.pas(70): Types of actual and formal var parameters must be identical [Error] Unapp.pas(70): Incompatible types: 'Char' and 'WideChar' [Error] Unapp.pas(73): Undeclared identifier: 'CaseIgnoreString' [Error] Unapp.pas(74): Types of actual and formal var parameters must be identical [Error] Unapp.pas(77): Types of actual and formal var parameters must be identical [Fatal Error] ProjApp.dpr(5): Could not compile used unit 'Unapp.pas'

1
Could you please post the complete error message and indicate the corresponding line in your code?Alexander Vogt
You don't initialize DirSearchDavid Heffernan
yes i use that code but it doesnt work? because of ADS_SEARCH_HANDLE. can you help me pls?user2925173
Hi Alexander, I sent the original code and delphi error could you pls help me?user2925173

1 Answers

0
votes

Looks like DirSearch wasn't initialized

Use this code in order to initialize it

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

Don't forget to replace YourDomain with your actual domain