The Win32 Network Management APIs 28

 

 

 

 

 

NetWkstaUserEnum() Program Example

 

The NetWkstaUserEnum() function lists information about all users currently logged on to the workstation. This list includes interactive, service and batch logons. The following code sample demonstrates how to list information about all users currently logged on to a workstation using a call to the NetWkstaUserEnum() function. The sample calls NetWkstaUserEnum(), specifying information level 0 (WKSTA_USER_INFO_0). The sample loops through the entries and prints the names of the users logged on to a workstation. Finally, the code sample frees the memory allocated for the information buffer, and prints the total number of users enumerated. Note that since the NetWkstaUserEnum() function lists entries for service and batch logons, as well as for interactive logons, the function can return entries for users who have logged off a workstation. This can occur, for example, when a user calls a service that impersonates the user. In this instance, NetWkstaUserEnum() returns an entry for the user until the service stops impersonating the user.

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

 

NetWkstaUserEnum() Program Example: Creating new Win32 C++ console application project

 

Then, add the source file. Give it a name.

 

NetWkstaUserEnum() Program Example: Adding C++ source file to the existing project

 

Then, add the following source code.

 

#include <stdio.h>

#include <assert.h>

#include <windows.h>

#include <lm.h>

 

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

 

#define INFO_BUFFER_SIZE 32767

 

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

{

   LPWKSTA_USER_INFO_0 pBuf = NULL;

   LPWKSTA_USER_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;

   LPWSTR 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 \\\\Atlantic\n", argv[0]);

      wprintf(L"If server name not supplied, default to local\n");

      exit(1);

   }

  

   // The server is not the default local computer.

   if (argc == 2)

   {

         pszServerName = argv[1];

         fwprintf_s(stderr, L"\nUsers currently logged on %s are:\n\n", pszServerName);

   }

   else // default to local

   {

         GetComputerName(infoBuf, &bufCharCount);

         wprintf(L"Users currently logged on %s are:\n\n", infoBuf);

   }

 

   // Call the NetWkstaUserEnum() function, specifying level 0.

   do // begin do

   {

      nStatus = NetWkstaUserEnum( pszServerName, dwLevel, (LPBYTE*)&pBuf, dwPrefMaxLen, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);

      // If the call succeeds,

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

      {

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

         if ((pTmpBuf = pBuf) != NULL)

         {

            // Loop through the entries.

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

            {

               assert(pTmpBuf != NULL);

 

               if (pTmpBuf == NULL)

               {

                  // Only members of the Administrators local group

                  //  can successfully execute NetWkstaUserEnum() locally and on a remote server.

                  fwprintf_s(stderr, L"An access violation has occurred, buffer is NULL!\n");

                  break;

               }

               // Print the user logged on to the workstation.

               wprintf(L"\t-- %s\n", pTmpBuf->wkui0_username);

 

               pTmpBuf++;

               dwTotalCount++;

            }

         }

      }

      // Otherwise, indicate a system error.

      else

        {

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

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

        }

      // Free the allocated memory.

      if (pBuf != NULL)

      {

         NetApiBufferFree(pBuf);

         pBuf = NULL;

      }

   }

   // Continue to call NetWkstaUserEnum() 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 workstation users.

   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.

 

NetWkstaUserEnum() Program Example: A sample console output

 

 

 

 

< Win32 Network Management APIs 27 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 29 >