The Win32 Network Management APIs 34

 

 

 

 

 

NetUserEnum() Program Example

 

The NetUserEnum() function provides information about all user accounts on a server. To quickly enumerate user, computer, or global group account information, call the NetQueryDisplayInformation() function. The following code sample demonstrates how to retrieve information about the user accounts on a server with a call to the NetUserEnum() function. The sample calls NetUserEnum(), specifying information level 0 (USER_INFO_0) to enumerate only global user accounts. If the call succeeds, the code loops through the entries and prints the name of each user account. Finally, the code sample frees the memory allocated for the information buffer and prints a total of the users enumerated.

Create a new empty Win32 console application project. Give the project name and change the project location is needed.

 

NetUserEnum() Program Example: Creating new C++ Win32 console application project in Visual C++ .NET

 

Then, add the source file and give it a suitable name.

 

NetUserEnum() Program Example: Adding new C++ source file for the C++ source code

 

Then, add the following source code.

 

#ifndef UNICODE

#define UNICODE

#endif

 

#pragma comment(lib, "netapi32.lib")

 

#define INFO_BUFFER_SIZE 32767

 

#include <stdio.h>

#include <assert.h>

#include <windows.h>

#include <lm.h>

 

int wmain(int argc, wchar_t *argv[])

{

   LPUSER_INFO_0 pBuf = NULL;

   LPUSER_INFO_0 pTmpBuf;

   DWORD dwLevel = 0;

   DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;

   DWORD dwEntriesRead = 0;

   DWORD dwTotalEntries = 0;

   DWORD dwResumeHandle = 0;

   DWORD i;

   DWORD dwTotalCount = 0;

   NET_API_STATUS nStatus;

   LPTSTR pszServerName = NULL;

   WCHAR  infoBuf[INFO_BUFFER_SIZE];

   DWORD  bufCharCount = INFO_BUFFER_SIZE;

 

 

   if (argc > 2)

   {

      fwprintf_s(stderr, L"Usage: %s [\\\\ServerName]\n", argv[0]);

      wprintf(L"Example: %s \\\\Bigbos\n", argv[0]);

      wprintf(L"Use local as default if server name not supplied...\n");

      exit(1);

   }

 

   // The server is not the default local computer.

   if (argc == 2)

   {

         pszServerName =  (LPTSTR) argv[1];

         wprintf(L"\nUser account on %s: \n", pszServerName);

   }

   else if(argc == 1) // use local as default

   {

         GetComputerName(infoBuf, &bufCharCount);

         wprintf(L"Using local, \"%s\"\n", infoBuf);

         pszServerName = infoBuf;

   }

 

   // Call the NetUserEnum() function, specifying level 0;

   //   enumerate global user account types only.

   do // begin do

   {

      nStatus = NetUserEnum((LPCWSTR) pszServerName,

                            dwLevel,

                            FILTER_NORMAL_ACCOUNT, // global users

                            (LPBYTE*)&pBuf,

                            dwPrefMaxLen,

                            &dwEntriesRead,

                            &dwTotalEntries,

                            &dwResumeHandle);

 

      // If the call succeeds,

      if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))

      {

         wprintf(L"NetUserEnum() looks fine!\n");

         if ((pTmpBuf = pBuf) != NULL)

         {

            // Loop through the entries.

            for (i = 0; (i < dwEntriesRead); i++)

            {

               assert(pTmpBuf != NULL);

 

               if (pTmpBuf == NULL)

               {

                  fwprintf_s(stderr, L"An access violation has occurred\n");

                  break;

               }

 

               //  Print the name of the user account.

               wprintf(L"\t%d. %s\n", i+1, pTmpBuf->usri0_name);

 

               pTmpBuf++;

               dwTotalCount++;

            }

         }

      }

 

        // Otherwise, print the system error.

      else

        {

              wprintf(L"NetUserEnum() failed!\n");

              fwprintf_s(stderr, L"A system error has occurred: %d\n", nStatus);

        }

 

        // Free the allocated buffer.

      if (pBuf != NULL)

      {

         NetApiBufferFree(pBuf);

         pBuf = NULL;

      }

   }

 

   // Continue to call NetUserEnum while there are more entries.

   while (nStatus == ERROR_MORE_DATA); // end do

 

   // Check again for allocated memory.

   if (pBuf != NULL)

      NetApiBufferFree(pBuf);

 

   // Print the final count of users enumerated.

   fwprintf_s(stderr, L"\nTotal of %d entries enumerated\n", dwTotalCount);

 

   return 0;

}

 

Build and run the project. The following screenshot is a sample output.

 

NetUserEnum() Program Example: A sample program output showing all users in the local Win XP Pro machine

 

We can verify the enumeration through Computer Management snap-in for Windows XP Pro as shown below. All the program example in this topic should be tested in the domain based Windows network to see the real outputs.

 

NetUserEnum() Program Example: Computer management snap-in showing all the user in the local machine

 

 

 

 

< Win32 Network Management APIs 33 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 35 >