Thema: Delphi NetUserEnum die 2.

Einzelnen Beitrag anzeigen

MiKaEr
(Gast)

n/a Beiträge
 
#22

AW: NetUserEnum die 2.

  Alt 20. Apr 2012, 21:57
ich finde bei mir den fehler leider nicht. es kommt eine unerklärliche AV beim lesen von irgendetwas. es wird der nutzername Administrator in meine ListBox geschrieben mehr aber nicht. das ist erst passiert als ich das record _USER_INFO_0 um password, password_age und home_dir erweitert habe:

Delphi-Quellcode:
unit Unit1;

interface

uses
 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls;

type
 TForm1 = class(TForm)
  ListView1: TListView;
  ListBox1: TListBox;
  procedure FormCreate(Sender: TObject);
 private
  { Private-Deklarationen }
 public
  { Public-Deklarationen }
 end;

var
 Form1: TForm1;

const
 NERR_Success = 0;

function NetApiBufferAllocate(ByteCount: DWORD; var Buffer: Pointer): DWORD; stdcall; external 'netapi32.dll';
function NetGetDCName(servername: LPCWSTR; domainname: LPCWSTR; bufptr: Pointer): DWORD; stdcall; external 'netapi32.dll';
function NetApiBufferFree(Buffer: Pointer): DWORD; stdcall; external 'netapi32.dll';
function NetWkstaGetInfo(servername: LPWSTR; Level: DWORD; bufptr: Pointer): Longint; stdcall;
 external 'netapi32.dllname 'NetWkstaGetInfo';

function NetUserEnum(servername: LPCWSTR; Level: DWORD; filter: DWORD; var bufptr: Pointer; prefmaxlen: DWORD;
 var entriesread: DWORD; var totalentries: DWORD; resume_handle: PDWORD): DWORD; stdcall; external 'netapi32.dll';

type
 WKSTA_INFO_100 = record
  wki100_platform_id: DWORD;
  wki100_computername: LPWSTR;
  wki100_langroup: LPWSTR;
  wki100_ver_major: DWORD;
  wki100_ver_minor: DWORD;
 end;

 LPWKSTA_INFO_100 = ^WKSTA_INFO_100;

 _USER_INFO_0 = record
  usri1_name: LPWSTR;
  usri1_password: LPWSTR;
  usri1_password_age: DWORD;
  usri1_home_dir: LPWSTR;
 end;

 TUserInfo0 = _USER_INFO_0;

function NetUserChangePassword(Domain: PWideChar; UserName: PWideChar; OldPassword: PWideChar; NewPassword: PWideChar)
 : Longint; stdcall; external 'netapi32.dllname 'NetUserChangePassword';

implementation

{$R *.dfm}

function GetNetParam(AParam: integer): string;
var
 PBuf: LPWKSTA_INFO_100;
 Res: Longint;
begin
 result := '';
 Res := NetWkstaGetInfo(nil, 100, @PBuf);
 if Res = NERR_Success then
  begin
   case AParam of
    0:
     result := string(PBuf^.wki100_computername);
    1:
     result := string(PBuf^.wki100_langroup);
   end;
  end;
end;

function GetComputerName: string;
begin
 result := GetNetParam(0);
end;

function GetDomainName: string;
begin
 result := GetNetParam(1);
end;

function GetDomainControllerName(const ADomainName: string): string;
var
 wDomainName: WideString;
 Controller: PWideChar;
begin
 wDomainName := ADomainName;
 NetGetDCName(nil, PWideChar(wDomainName), @Controller);
 result := WideCharToString(Controller);
 NetApiBufferFree(Controller);
end;

procedure GetUsers(Users: TStrings; AServer: string);
type
 TUserInfoArr = array [0 .. 3] of TUserInfo0;
var
 UserInfo: Pointer;
 entriesread, totalentries, ResumeHandle: DWORD;
 Res: DWORD;
 i: integer;
 FServer: WideString;
begin
 FServer := AServer;
 ResumeHandle := 0;
 repeat
  Res := NetUserEnum(PWideChar(FServer), 0, 0, UserInfo, 64 * 3, entriesread, totalentries, @ResumeHandle);
  if (Res = NERR_Success) or (Res = ERROR_MORE_DATA) then
   begin
    for i := 0 to entriesread - 1 do
     Users.Add(TUserInfoArr(UserInfo^)[i].usri1_name);
    NetApiBufferFree(UserInfo);
   end;
 until Res <> ERROR_MORE_DATA;
end;

function isPw(const User, Password: string): Boolean;
var
 Err: LongWord;
begin
 Err := NetUserChangePassword(nil, PWideChar(WideString(User)), PWideChar(WideString(Password)),
  PWideChar(WideString(Password)));
 result := Err = 0;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 GetUsers(ListBox1.Items, '');
end;

end.
  Mit Zitat antworten Zitat