The Win32 Network Management APIs 46

 

 

 

 

 

NetSessionEnum() Program Example

 

This function provides information about sessions established on a server. Only members of the Administrators or Server Operators local group can successfully execute the NetSessionEnum() function at level 1 or level 2. No special group membership is required for level 0 or level 10 calls. 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 session functions.

The following code sample demonstrates how to retrieve information about current sessions using a call to the NetSessionEnum() function. The sample calls NetSessionEnum(), specifying information level 10 (SESSION_INFO_10). The sample loops through the entries and prints the retrieved information. Finally, the code prints the total number of sessions enumerated and frees the memory allocated for the information buffer.

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

 

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

 

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

 

NetSessionEnum() Program Example: Adding new C++ source file to the existing Visual C++ project

 

Then, add the following source code.

 

#ifndef UNICODE

#define UNICODE

#endif

 

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

 

#include <stdio.h>

#include <assert.h>

#include <windows.h>

#include <lm.h>

 

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

{

   LPSESSION_INFO_10 pBuf = NULL;

   LPSESSION_INFO_10 pTmpBuf;

   DWORD dwLevel = 10;

   DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;

   DWORD dwEntriesRead = 0;

   DWORD dwTotalEntries = 0;

   DWORD dwResumeHandle = 0;

   DWORD i;

   DWORD dwTotalCount = 0;

   LPTSTR pszServerName = NULL;

   LPTSTR pszClientName = NULL;

   LPTSTR pszUserName = NULL;

   NET_API_STATUS nStatus;

 

   // Check command line arguments.

   if (argc > 4 || argc == 1)

   {

      wprintf(L"Usage: %s [\\\\ServerName] [\\\\ClientName] [UserName]\n", argv[0]);

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

      exit(1);

   }

 

   if (argc >= 2)

      pszServerName = argv[1];

   if (argc >= 3)

      pszClientName = argv[2];

   if (argc == 4)

      pszUserName = argv[3];

 

   // Call the NetSessionEnum() function, specifying level 10.

   do // begin do

   {

      nStatus = NetSessionEnum(pszServerName,

                               pszClientName,

                               pszUserName,

                               dwLevel,

                               (LPBYTE*)&pBuf,

                               dwPrefMaxLen,

                               &dwEntriesRead,

                               &dwTotalEntries,

                               &dwResumeHandle);

 

      // If the call succeeds,

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

      {

         wprintf(L"NetSessionEnum() is OK!\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 retrieved data.

               wprintf(L"\n\tClient: %s\n", pTmpBuf->sesi10_cname);

               wprintf(L"\tUser:   %s\n", pTmpBuf->sesi10_username);

               wprintf(L"\tActive: %d\n", pTmpBuf->sesi10_time);

               wprintf(L"\tIdle:   %d\n", pTmpBuf->sesi10_idle_time);

 

               pTmpBuf++;

               dwTotalCount++;

            }

         }

      }

      // Otherwise, indicate a system error.

      else

        {

              wprintf(L"NetSessionEnum() 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 NetSessionEnum() while there are more entries.

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

 

   // Check again for an allocated buffer.

   if (pBuf != NULL)

      NetApiBufferFree(pBuf);

   // Print the final count of sessions 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.

 

NetSessionEnum() Program Example: A sample output on local pc. This program should be run against server in the domain based network

 

 

 

 

< Win32 Network Management APIs 45 | Win32 Network Management APIs | Win32 Programming | Win32 Network Management APIs 47 >