SteveL
SteveL

Reputation: 319

NetUserGetLocalGroups - how to call it?

I am using Delphi 2010, latest version (from repository) of JEDI WinAPI and Windows Security Code Library (WSCL).

I don't know how to call the NetUserSetGroups function. The way I am doing it, it is throwing an exception:

Access violation at address 5B8760BE in module 'netapi32.dll'. Write of address 00000000.

Following is my code:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  NetApiStatus: NET_API_STATUS;
  dwEntriesRead, dwEntriesTotal: PDWORD;
  lgi01: LOCALGROUP_USERS_INFO_0;
  username: PChar;
begin
  username := 'Elise';

  NetApiStatus := NetUserGetLocalGroups(nil, PChar(username), 0, LG_INCLUDE_INDIRECT, PByte(lgi01),
    MAX_PREFERRED_LENGTH, dwEntriesRead, dwEntriesTotal);
  if NetApiStatus = NERR_SUCCESS then
    showmessage('Total groups user belongs to: ' + IntTostr(dwEntriesTotal^));

end;

end.

Would appreciate if someone could kindly show me how I can call this function?

Thanks in advance.

Upvotes: 0

Views: 1620

Answers (1)

Remko
Remko

Reputation: 7330

This code works fine for me:

type
  LocalGroupUsersInfo0Array = array[0..ANYSIZE_ARRAY-1] of LOCALGROUP_USERS_INFO_0;
  PLocalGroupUsersInfo0Array = ^LocalGroupUsersInfo0Array;

procedure TForm3.Button3Click(Sender: TObject);
var
  nas: NET_API_STATUS;
  PLGUIA: PLocalGroupUsersInfo0Array;
  Count: DWORD;
  Total: DWORD;
  i: Integer;
begin
  PLGUIA := nil;
  nas := NetUserGetLocalGroups(nil, PChar('rweijnen'), 0, LG_INCLUDE_INDIRECT,
    PByte(PLGUIA), MAX_PREFERRED_LENGTH, @Count, @Total);

  if (nas =  NERR_Success) or (nas = ERROR_MORE_DATA) then
  begin
    for i := 0 to Count - 1 do
    begin
      Memo1.Lines.Add(Format('name=%s', [PLGUIA^[i].lgrui0_name]));
    end;

    if Assigned(PLGUIA) then
      NetApiBufferFree(PLGUIA);
  end;
end;

Upvotes: 1

Related Questions