Platform SDK: Network Management

NetSessionGetInfo Sample (Windows 95/98)

Windows 95/98: The following code sample demonstrates how to retrieve information about a particular session using a call to the NetSessionGetInfo function.

The sample calls NetSessionGetInfo once to determine the size of the buffer needed for the returned data. The code allocates memory for the buffer. Then the sample calls NetSessionGetInfo again to retrieve the data. Finally, the sample displays the information and frees the allocated memory.

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

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

   if (argc == 3)
      pszServerName = argv[1];
   //
   // Call NetSessionGetInfo once to determine the 
   //   total size needed for the buffer.
   //
   cbBuffer = 0;
   nStatus = NetSessionGetInfo(pszServerName,
                               argv[argc-1],
                               nLevel,
                               (char FAR *)pBuf,
                               cbBuffer,
                               &nTotalAvail);

   if (nStatus != NERR_BufTooSmall)
   {
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
      exit(1);
   }
   //
   // Allocate the memory required for the buffer.
   //
   cbBuffer = nTotalAvail;
   pBuf = malloc(cbBuffer);

   if (pBuf == NULL)
      printf("No memory\n");
   //
   // Call NetSessionGetInfo a second time to retrieve the 
   //  information, specifying information level 50.
   //
   nStatus = NetSessionGetInfo(pszServerName,
                               argv[argc-1],
                               nLevel,
                               (char FAR *)pBuf,
                               cbBuffer,
                               &nTotalAvail);
   //
   // If the call is successful, display the data.
   //
   if (nStatus == NERR_Success)
   {
      printf("\n\tComputer: %s\n", pBuf->sesi50_cname);
      printf("\tUser: %s\n", pBuf->sesi50_username);
      printf("\tKey: %d\n", pBuf->sesi50_key);
      printf("\t# Connections: %d\n", pBuf->sesi50_num_conns);
      printf("\t# Opens: %d\n", pBuf->sesi50_num_opens);
   }
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      free(pBuf);

   return 0;
}