Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
486 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


User list on Windows NTGo to Serhiy Perevoznyk's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
Win API
Skill Level:
Scoring:
Last Update:
04/22/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet LanMan NetWkstaGetInfo NetUserEnum
Times Scored:
3
Visits:
4302
Uploader: Serhiy Perevoznyk
Company: http://users.chello.be/ws36637
Reference: N/A
 
Question/Problem/Abstract:
How to retrieve a list of the users on Windows NT
Answer:



Windows exposes LanMan 32-bit Application Programming Interfaces (APIs) to provide network services. These APIs are called only from 32-bit programs  and are used to control both the local and remote Windows  machines that you have permission to access.
The following code sample demonstrates how to retrieve information about the user accounts on a server with a call to the NetUserEnum function.  
User account names are limited to 20 characters and group names are limited to 256 characters. In addition, account names cannot be terminated by a period and they cannot include commas or any of the following printable characters: ", /, \, [, ], :, |, <, >, +, =, ;, ?, *. Names also cannot include nonprintable characters in the ranges 1-7, 10-17, 20-27, and 30-37.

First, we will need some API functions:

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.dll' Name '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
    usri0_name: LPWSTR;
  end;
  TUserInfo0 = _USER_INFO_0;

You must be a member of the Administrators local group to successfully execute NetWkstaSetInfo on a remote server or on a computer that has local security enabled.


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;

Following function returns the name of the local computer:

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

Following function returns the name of the domain to which the computer belongs:

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

The GetDomainControllerName function returns the name of the primary domain controller (PDC). It does not return the name of the backup domain controller (BDC) for the specified domain.

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 : TStringList; AServer : string);
type
  TUserInfoArr = array[0..(MaxInt - 4) div SizeOf(TUserInfo0)] 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 * SizeOf(TUserInfo0),
      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].usri0_name);
      NetApiBufferFree(UserInfo);
    end;
  until Res <> ERROR_MORE_DATA;
end;


To get a list of the users on local computer you can use the result of the function GetComputerName as a second parameter of the GetUsers function.
To get a list of the users from PDC you can use GetDomainController function to get PDC name.

If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management user functions.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Group List
    Josir Gomes (Mar 29 2006 4:53PM)

Thanks for the tip. How do I get the current user's groups ?
Respond

NET USER /DOMAIN
    Maarten de Haan (May 2 2003 11:03AM)

Thanks, I now get what I want, if I issue:
GetUsers(strlist,GetDomainControllername(GetDomainname));
This is compatible with: NET USER /DOMAIN.
Thanks again.

Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)