Platform SDK: Network Management

NetShareGetInfo Sample (Windows 95/98)

Windows 95/98: The following code sample demonstrates how to retrieve information about a shared resource with a call to the NetShareGetInfo function.

The sample calls NetShareGetInfo once to determine the size of the buffer needed for the returned data. The code allocates memory for the buffer. Then the sample calls NetShareGetInfo 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;
   char FAR * pszNetName = NULL;
   short nLevel = 50;
   struct share_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] ShareName\n", argv[0]);
      exit(1);
   }

   if (argc == 3)
      pszServerName = argv[1];
   //
   // Note that for a Win9x peer server,
   //   the share name needs to be uppercase.
   //
   pszNetName = argv[argc-1];
   //
   // Call NetShareGetInfo once to determine the 
   //   total size needed for the buffer.
   //
   cbBuffer = 0;
   nStatus = NetShareGetInfo(pszServerName,
                             pszNetName,
                             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 NetShareGetInfo a second time to retrieve the 
   //  information, specifying information level 50.
   //
   nStatus = NetShareGetInfo(pszServerName,
                             pszNetName,
                             nLevel,
                             (char FAR *)pBuf,
                             cbBuffer,
                             &nTotalAvail);
   //
   // If the call is successful, display the data.
   //
   if (nStatus == NERR_Success)
      printf("\n\tPath: %s\n", pBuf->shi50_path);
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      free(pBuf);

   return 0;
}