Solved

Get a list of all users on one machine.

Posted on 2004-08-26
4
399 Views
Last Modified: 2010-04-01
Hi!

How can i get a list of all users on a machine...

maybe like this

Users[0] == "Meier",
Users[1] == "Hans",
....

PS: I mean the windows logon users. Maybe there ist something like EnumUsers?

Thanks for advance.
0
Comment
Question by:ChristopH1987
  • 2
4 Comments
 
LVL 2

Assisted Solution

by:carribus
carribus earned 100 total points
ID: 11901928
The one way that I know of doing this is to use the NetUserEnum() function.

Have a look at this link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netmgmt/netmgmt/NetUserEnum.asp

That explains the function and carries a sample of how to use the function. It should work great for your needs.
Ciao
0
 
LVL 86

Accepted Solution

by:
jkr earned 150 total points
ID: 11902474
And here's a sample on how to use it along with 'NetQueryDisplayInformation()' to get the details: http://win32.mvps.org/network/nue_nqdi.cpp

#include <windows.h>
#include <lm.h>
#include <stdio.h>
#pragma hdrstop



// 28jul97 felixk   fixed a nasty bug in do_NetQueryDisplayInformation
//                  (next_index was set even if read == 0: accvio)


void print_user( const wchar_t *name, DWORD flags )
{
      // NOTE: capital "S" in a printf format string means "Unicode",
      // if we are an ANSI program

      printf( "%-40.40S ", name ); // display Unicode user name
      if ( flags & UF_NORMAL_ACCOUNT )
            puts( "(domain user)" );
      else if ( flags & UF_TEMP_DUPLICATE_ACCOUNT )
            puts( "(local user)" );
      else if ( flags & UF_WORKSTATION_TRUST_ACCOUNT )
            puts( "(computer account)" );
      else if ( flags & UF_SERVER_TRUST_ACCOUNT )
            puts( "(PDC/BDC computer account)" );
      else if ( flags & UF_INTERDOMAIN_TRUST_ACCOUNT )
            puts( "(trusted domain account)" );
      else
            puts( "(funny account type)" );
}



void do_NetUserEnum( const wchar_t *server )
{
      USER_INFO_1 *buf, *cur;
      DWORD read, totalremaining, resumeh, rc, i;

      resumeh = 0;
      do
      {
            buf = NULL;
            rc = NetUserEnum( server, 1, 0, (BYTE **) &buf, 1024, &read, &totalremaining, &resumeh );
            if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
                  break;

            printf( "\ngot %lu entries out of %lu\n", read, totalremaining );
            for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
                  print_user( cur->usri1_name, cur->usri1_flags );

            if ( buf != NULL )
                  NetApiBufferFree( buf );

      } while ( rc == ERROR_MORE_DATA );

      if ( rc != ERROR_SUCCESS )
            printf( "NUE() returned %lu\n", rc );
}



void do_NetQueryDisplayInformation( const wchar_t *server, int level )
{
      void *buf;
      NET_DISPLAY_USER *ndu;
      NET_DISPLAY_MACHINE *ndm;
      NET_DISPLAY_GROUP *ndg;
      DWORD read, next_index, rc, i;

      next_index = 0;
      do
      {
            buf = NULL;
            rc = NetQueryDisplayInformation( server, level, next_index, 10, 1024, &read, &buf );
            if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
                  break;

            printf( "\ngot %lu entries\n", read );

            switch ( level )
            {
                  case 1: // users
                        for ( i = 0, ndu = (NET_DISPLAY_USER *) buf; i < read; ++ i, ++ ndu )
                              print_user( ndu->usri1_name, ndu->usri1_flags );
                        // take the last element's next_index
                        if ( read > 0 )
                              next_index = ((NET_DISPLAY_USER *)buf)[read - 1].usri1_next_index;
                        break;
                  case 2: // machines
                        for ( i = 0, ndm = (NET_DISPLAY_MACHINE *) buf; i < read; ++ i, ++ ndm )
                              print_user( ndm->usri2_name, ndm->usri2_flags );
                        // take the last element's next_index
                        if ( read > 0 )
                              next_index = ((NET_DISPLAY_MACHINE *)buf)[read - 1].usri2_next_index;
                        break;
                  case 3: // groups
                        for ( i = 0, ndg = (NET_DISPLAY_GROUP *) buf; i < read; ++ i, ++ ndg )
                              printf( "%-40.40S (group)\n", ndg->grpi3_name );
                        // take the last element's next_index
                        if ( read > 0 )
                              next_index = ((NET_DISPLAY_GROUP *)buf)[read - 1].grpi3_next_index;
                        break;
            }

            if ( buf != NULL )
                  NetApiBufferFree( buf );

      } while ( rc == ERROR_MORE_DATA );

      if ( rc != ERROR_SUCCESS )
            printf( "NQDI() returned %lu\n", rc );
}



int main( void )
{
      // set this to \\your_domain_controller
      wchar_t *servername = L"\\\\BABYA";

      // get all accounts
      puts( "\n*** Enumerating accounts: NetUserEnum()" );
      do_NetUserEnum( servername );

      // get users
      puts( "\n*** Enumerating users: NetQueryDisplayInformation()" );
      do_NetQueryDisplayInformation( servername, 1 );

      // get machine accounts
      puts( "\n*** Enumerating machine accounts: NetQueryDisplayInformation()" );
      do_NetQueryDisplayInformation( servername, 2 );

      // get groups
      puts( "\n*** Enumerating groups: NetQueryDisplayInformation()" );
      do_NetQueryDisplayInformation( servername, 3 );

      return 0;
}

(link with netapi32.lib)
0
 
LVL 2

Expert Comment

by:carribus
ID: 11902511
nice one jkr :) good to see that our answers correspond with regards to user account :)
*takes note of the other functions*

Thanks..
0
 
LVL 2

Author Comment

by:ChristopH1987
ID: 11904950
Thanks all!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maze travler 6 51
How to gracefully close the c++ 11 thread? 3 83
Exception thrown at 0x00007FFD5BC81F28 7 38
Least Squares Curve Fitting 4 61
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now