Simple code snippets to get AD user information

Simple code snippets to get AD user information

I wrote a small AD snippet just the other day, to check which AD groups a specific AD user was member of.  I needed to check if a user was member of a specific AD group, to enable or disable some “super user” functions in the system management application.

Note that it uses web.wintypes.adstypes which may only be available in XE5 and upwards?

   http://pastebin.com/EYwHUerR

XE7’s new dynamic array syntax

XE7’s new dynamic array syntax

Good thing: Empty arrays can be used as default param value.

    procedure Test(const Params: TVarArray = []);

Bad thing: Variants not fully supported?

Try uncommenting CVarArr in the example below.

Doesn’t work and gives “Constant expression expected”.  

Yet the same constant list can be assigned to VVarArr

Go figure.

program XE7Tests;

{$APPTYPE CONSOLE}

{$R *.res}

uses

  System.SysUtils, Variants;

type

  TVarArray = array of Variant;

  TIntArray = array of Integer;

  TTest = class

    procedure Test(const Params: TVarArray = []);

  end;

{ TTest }

procedure TTest.Test(const Params: TVarArray);

var

  npar: Integer;

begin

  npar := High(Params) – Low(Params) + 1;

  Writeln(nPar);

end;

const

  CIntArr: TIntArray = [1,2,3];

//  CVarArr: TVarArray = [1,’2′, 3];  // “Constant expression expected”

var

  Test: TTest;

  VVarArr : TVarArray;

  VIntArr : TIntArray;

begin

  Test := TTest.Create;

  try

    try

      Test.Test;      // 0

      Test.Test(nil); // 0

      Test.Test([1,’2′, 3]); // 3

      VVarArr := [1,’2′, 3, 3.14];

      Test.Test(VVarArr); // 4

      Test.Test(VVarArr + [‘five’]); // 5

//      Test.Test(VVarArr + CVarArr); // Should have been 7

    except

      on E: Exception do

        Writeln(E.ClassName, ‘: ‘, E.Message);

    end;

  finally

    Test.Free;

    Write(‘ – Enter: ‘);

    Readln;

  end;

end.