Platform SDK: Network Management

NetServerGetInfo Sample (Windows 95/98)

Windows 95/98: The following code sample demonstrates how to retrieve a server's configuration information using a call to the NetServerGetInfo function.

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

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

int main(int argc, char FAR * argv[])
{
   char FAR * pszServerName = NULL;
   short nLevel = 50;
   struct server_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)
   {
      printf("Usage: %s [\\\\ServerName]\n", argv[0]);
      exit(1);
   }

   if (argc == 2)
      pszServerName = argv[1];
   //
   // Call NetServerGetInfo once to determine the 
   //   total size needed for the buffer.
   //
   cbBuffer = 0;
   nStatus = NetServerGetInfo(pszServerName,
                              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 NetServerGetInfo a second time to retrieve the 
   //  information, specifying information level 50.
   //
   nStatus = NetServerGetInfo(pszServerName,
                              nLevel,
                              (char FAR *)pBuf,
                              cbBuffer,
                              &nTotalAvail);
   //
   // If the call is successful, display the data.
   //
   if (nStatus == NERR_Success)
   {
      printf("\tName: %s\n", pBuf->sv50_name);
      printf("\tVersion: %d.%d\n", pBuf->sv50_version_major,pBuf->sv50_version_minor);
      printf("\tType: 0x%x\n", pBuf->sv50_type);
   }
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      free(pBuf);

   return 0;
}