Platform SDK: Network Management

NetSessionEnum Sample (Windows 95/98)

Windows 95/98: The following code sample demonstrates how to list the current sessions on a server with a call to the NetSessionEnum function.

The sample allocates the memory required to receive 20 session_info_50 structures. If this size is inadequate, the sample warns the caller that there are more entries to enumerate. Finally, the sample frees the allocated memory.

#include <stdio.h>
#include <assert.h>
#include <windows.h> 
#include <svrapi.h>

const short MAX_ENTRIES = 20;

int main(int argc, char FAR * argv[])
{
   char FAR * pszServerName = NULL;
   short nLevel = 50;
   struct session_info_50* pBuf = NULL;
   struct session_info_50* pTmpBuf = NULL;
   short cbBuffer;
   short nEntriesRead = 0;
   short nTotalEntries = 0;
   short nTotalCount = 0;
   int i;
   NET_API_STATUS nStatus;
   //
   // ServerName can be NULL to indicate the local computer.
   //
   if (argc > 2)
   {
      printf("Usage: %s [\\\\ServerName]\n", argv[0]);
      exit(1);
   }

   if (argc == 2)
      pszServerName = argv[1];

   cbBuffer = MAX_ENTRIES * sizeof(struct session_info_50);
   //
   // Allocate the memory required to receive a maximum of
   //  20 session_info_50 structures.
   //
   pBuf = malloc(cbBuffer);

   if (pBuf == NULL)
      printf("No memory\n");

   // Call the NetSessionEnum function to list the
   //  sessions, specifying information level 50.
   //
   nStatus = NetSessionEnum(pszServerName,
                            nLevel,
                            (char FAR *)pBuf,
                            cbBuffer,
                            &nEntriesRead,
                            &nTotalEntries);
   //
   // Loop through the entries; process errors.
   //
   if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
   {
      if ((pTmpBuf = pBuf) != NULL)
      {
         for (i = 0; (i < nEntriesRead); i++)
         {
            assert(pTmpBuf != NULL);

            if (pTmpBuf == NULL)
            {
               fprintf(stderr, "An access violation has occurred\n");
               break;
            }
            //
            // Display the information for each entry retrieved.
            //
            printf("\n\tClient: %s\n", pTmpBuf->sesi50_cname);
            printf("\tUser:   %s\n", pTmpBuf->sesi50_username);
            printf("\tActive: %d\n", pTmpBuf->sesi50_time);
            printf("\tIdle:   %d\n", pTmpBuf->sesi50_idle_time);
            printf("\tKey:   %d\n", pTmpBuf->sesi50_key);

            pTmpBuf++;
            nTotalCount++;
         }
      }
   }
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Display a warning if the buffer was not large enough
   //  to contain all available entries.
   //
   if ((nEntriesRead < nTotalEntries) || (nStatus == ERROR_MORE_DATA))
      fprintf(stderr, "Not all entries have been enumerated\n");
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      free(pBuf);

   fprintf(stderr, "\nTotal of %d entries enumerated\n", nTotalCount);

   return 0;
}