首页  编辑  

获取Windows用户列表

Tags: /超级猛料/Network.网络通讯/共享和用户、域/   Date Created:

获取Windows用户列表

Steven Lam <Lam@onlineprogrammer.org>

Dear Kingron,

The question is solved already.

Anyway,thanks a lot for ur kindly attention!:)

The following source code is for ur referece.

Best regards,

Lam

/ //--------------------------------------------------------------

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls, ComCtrls;

type

USER_INFO_10 = packed record

usri10_name: LPWSTR;

usri10_comment: LPWSTR;

usri10_usr_comment: LPWSTR;

usri10_full_name: LPWSTR;

end;

PUSER_INFO_10 = ^USER_INFO_10;

function NetUserEnum (Servername: LPCWSTR;

Level: DWORD;

Filter: DWORD;

Buf: PUSER_INFO_10;

PrefMaxLen: DWORD;

EntriesRead: LPDWORD;

TotalEntries: LPDWORD;

Resume_Handle: LPDWORD

): DWORD;

stdcall;

external 'netapi32.dll';

function NetApiBufferFree (Buffer: Pointer

): DWORD;

stdcall;

external 'netapi32.dll';

type

TForm1 = class(TForm)

Button1: TButton;

ListBox1: TListBox;

procedure Button1Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

implementation

{$R *.dfm}

function GetAccountList(ServerName: string; ListBox: TListBox): TListBox;

var

Buffer,

TmpBuffer : Pointer;

PrefMaxLen : Integer;

Resume_Handle,

EntriesRead,

TotalEntries : DWORD;

I,

Size : Integer;

Account,

FullName,

Comment : string;

PSrvr : PWideChar;

begin

Result := ListBox;

Result.Clear;

Result.Columns := 3;

Result.TabWidth := Result.Width div 4 - 20;

try

Size := Length (ServerName);

GetMem(PSrvr,Size * SizeOf(WideChar) + 1);

StringToWideChar(ServerName,PSrvr,Size + 1);

PrefMaxLen := -1;

EntriesRead := 0;

TotalEntries := 0;

Resume_Handle := 0;

if NetUserEnum (PSrvr, 10, $0002, @Buffer, PrefMaxLen, @EntriesRead, @TotalEntries, @Resume_Handle) = S_OK then

begin

TmpBuffer := Buffer;

for I := 0 To TotalEntries - 1 do

begin

Account := PUSER_INFO_10(TmpBuffer).usri10_name;

FullName := PUSER_INFO_10(TmpBuffer).usri10_full_name;

Comment := PUSER_INFO_10(TmpBuffer).usri10_comment;

Resume_Handle := 0;

Result.Items.Add(Account + ^I + FullName + ^I + Comment);

TmpBuffer := Pointer (DWORD (TmpBuffer) + SizeOf (USER_INFO_10));

end;

end;

finally

NetApiBufferFree (Buffer);

end;

FreeMem(PSrvr);

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

GetAccountList('', ListBox1);

end;

end.

/ //--------------------------------------------------------------

_____________________________________________________________

Free Resources for programmers

Ezines, Articles, Source Code, Downloads , Programming Forums

http://www.onlineprogrammer.org

_____________________________________________________________

Promote your group and strengthen ties to your members with email@yourgroup.org by Everyone.net  http://www.everyone.net/?btn=tag

****************************************************

// The NetUserEnum function provides information about all user accounts on a server.

type

 USER_INFO_1 = record

   usri1_name : LPWSTR ;

   usri1_password : LPWSTR ;

   usri1_password_age : DWORD ;

   usri1_priv : DWORD ;

   usri1_home_dir : LPWSTR ;

   usri1_comment : LPWSTR ;

   usri1_flags : DWORD ;

   usri1_script_path : LPWSTR ;

  end ;

 lpUSER_INFO_1 = ^ USER_INFO_1 ;

function NetUserEnum ( ServerName : PWideChar ; Level , Filter : DWORD ; var Buffer : Pointer ;

 PrefMaxLen : DWORD ; var EntriesRead , TotalEntries , ResumeHandle : DWORD ): Longword ; stdcall ;

    external 'netapi32.dll' ;

function NetApiBufferFree ( pBuffer : PByte ): Longint ; stdcall ; external 'netapi32.dll' ;

{...}

procedure TForm1 . Button1Click ( Sender : TObject );

var

 EntiesRead         : DWORD ;

 TotalEntries       : DWORD ;

 UserInfo           : lpUSER_INFO_1 ;

 lpBuffer           : Pointer ;

 ResumeHandle       : DWORD ;

 Counter           : Integer ;

 NetApiStatus       : LongWord ;

begin

 ResumeHandle := 0 ;

  repeat

    // NetApiStatus :=

    // NetUserEnum(PChar('\\NT-Domain'), 1, 0, lpBuffer, 0,EntiesRead, TotalEntries, ResumeHandle);

   NetApiStatus := NetUserEnum ( nil , 1 , 0 , lpBuffer , 0 , EntiesRead ,

     TotalEntries , ResumeHandle );

   UserInfo := lpBuffer ;

    for Counter := 0 to EntiesRead - 1 do

    begin

     listbox1 . Items . Add ( WideCharToString ( UserInfo ^. usri1_name ) + ' --> ' +

       WideCharToString ( UserInfo ^. usri1_comment ));

     Inc ( UserInfo );

    end ;

   NetApiBufferFree ( lpBuffer );

  until ( NetApiStatus <> ERROR_MORE_DATA );

end ;