Hi,
I try to make calls to the NetAPI32 Windows networking API. Generally I think I got the translation of the C-Style API calls to Pascal conventions right, I can, for instance, query user infos and such using similar code.
But this call to NetUserGetLocalGroups gives me headaches:
implementation
{$R *.lfm}
uses Windows, CTypes;
const
MAX_PREFERRED_LENGTH = DWORD(-1);
NERR_SUCCESS = 0;
type
TLocalGroup_Users_Info0 = record
lgrui0_name: LPWSTR;
end;
PTLocalGroup_Users_Info0 = ^TLocalGroup_Users_Info0;
function NetUserGetLocalGroups(ServerName, UserName: PWideChar;
Level: DWORD; var Buffer: Pointer; PrefMaxLen: DWORD;
var EntriesRead: DWORD; var TotalEntries: DWORD): DWORD;
stdcall; external 'netapi32.dll';
function NetApiBufferFree(Buffer: pointer): DWORD; stdcall;
external 'netapi32.dll';
{ TForm1 }
procedure TForm1.ButtonLocalGroupsClick(Sender: TObject);
var
dwEntriesRead, dwEntriesTotal: DWORD;
pBuffer: Pointer;
pInfo: PTLocalGroup_Users_Info0;
ServerName,UserName:WideString;
Result : DWORD;
begin
ListBox1.Clear;
ServerName := ServerEdit.Text;
UserName := UserEdit.Text;
Result := NetUserGetLocalGroups(PWideChar(Servername), PWideChar(UserName), 0, pBuffer, MAX_PREFERRED_LENGTH, dwEntriesRead, dwEntriesTotal);
If Result = NERR_SUCCESS then
try
pInfo := pBuffer;
while dwEntriesRead > 0 do
begin
ListBox1.Items.Add(pInfo^.lgrui0_name);
Inc(pInfo);
Dec(dwEntriesRead);
end;
finally
NetAPIBufferFree(pBuffer);
end
else
ListBox1.Items.Add('Error ' + IntToStr(Result));
end;
The call to NetUserGetLocalGroups in the button click handler causes an access violation error (SIGSEV) in samcli!NetUserGetLocalgroups, and I cannot figure out what I possibly have done wrong. There is also plenty of sample code around about this call which looks very similar to mine, I did even try to use some, but all crash the same way.
From forum comments I read, the code has once been working, though 10 years ago. Seems something has changed which breaks the old code?
Anyone knows why this call fails?
Thx
Armin.