whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

31 Visitors Online


 
...list information about all users currently logged on to a workstation?
Autor: Thomas Stutz
[ Print tip ]  

Tip Rating (20):  
     


type
  
THostInfo = record
    
username: PWideChar;
    logon_domain: PWideChar;
    other_domains: PWideChar;
    logon_server: PWideChar;
  end;

  WKSTA_USER_INFO_0 = packed record
    
wkui0_username: PWideChar;
  end;
  PWKSTA_USER_INFO_0 = ^WKSTA_USER_INFO_0;

implementation

{$R *.dfm}

function NetWkstaUserEnum(servername: PWideChar;
  // Pointer to a string that specifies the DNS or NetBIOS name of the
  // remote server on which the function is to execute.
  // If this parameter is nil, the local computer is used.
  
level: DWORD;
  // Level = 0 : Return the names of users currently logged on to the workstation.
  
var bufptr: Pointer;   // Pointer to the buffer that receives the data
  
prefmaxlen: DWORD;
  // Specifies the preferred maximum length of returned data, in bytes.
  
var entriesread: PDWord;
  // Pointer to a value that receives the count of elements actually enumerated.
  
var totalentries: PDWord;  // total number of entries
  
var resumehandle: PDWord)
  // contains a resume handle which is used to continue an existing search
  
: Longint;
  stdcallexternal 'netapi32.dll' Name 'NetWkstaUserEnum';


function EnumNetUsers(HostName: WideString {; Users: TStrings}): THostInfo;
const
  
STR_ERROR_ACCESS_DENIED = 'The user does not have access to the requested information.';
  STR_ERROR_MORE_DATA = 'Specify a large enough buffer to receive all entries.';
  STR_ERROR_INVALID_LEVEL = 'The level parameter is invalid.';
var
  
Info: Pointer;
  ElTotal: PDWord;
  ElCount: PDWord;
  Resume: PDWord;
  Error: Longint;
  // UI : PWKSTA_USER_INFO_0;
  // i : Integer;
begin
  
Resume := 0;
  NetWkstaUserEnum(PWideChar(HostName),
    1,
    Info,
    0,
    ElCount,
    ElTotal,
    Resume);

  Error := NetWkstaUserEnum(PWideChar(HostName),
    1,
    Info,
    256 * Integer(ElTotal),
    ElCount,
    ElTotal,
    Resume);

  case Error of
    
ERROR_ACCESS_DENIED: Result.UserName := STR_ERROR_ACCESS_DENIED;
    ERROR_MORE_DATA: Result.UserName     := STR_ERROR_MORE_DATA;
    ERROR_INVALID_LEVEL: Result.UserName := STR_ERROR_INVALID_LEVEL
      else
        if 
Info <> nil then
        begin
          
Result := THostInfo(info^);
    {
    To retrieve all users:
    UI := PWKSTA_USER_INFO_0(Info);
    for i := 1 to DWord(ElCount) do
    begin
      Users.Add(UI^.wkui0_username);
      inc(UI);
    end;
    }
        
end
        else
          begin
            
Result.UserName      := 'N/A';
            Result.logon_domain  := 'N/A';
            Result.other_domains := 'N/A';
            Result.logon_server  := 'N/A';
          end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
var
  
HostInfo: THostInfo;
begin
  
// Specify target machine as argument (with or without preceding \\)
  
HostInfo := EnumNetUsers('WORKSTATION');
  with HostInfo do
  begin
    
Memo1.Lines.Add(username + #13#10 +
      logon_domain + #13#10 +
      other_domains + #13#10 +
      logon_server);
  end;
end;

 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners