Platform SDK: Network Management

NetShareSetInfo Sample (Windows 95/98)

Windows 95/98: The following code sample demonstrates how to change the parameters associated with a network share using a call to the NetShareSetInfo function.

The sample uses the following basic steps to change the remark associated with a network share:

  1. Determine the size of the buffer needed to receive share information by calling the NetShareGetInfo function.
  2. Retrieve share information by calling the NetShareGetInfo function a second time, specifying information level 50.
  3. Copy the returned information to a second buffer with a call to the CopyMemory function. (This is necessary to prevent resetting share information other than the comment.)
  4. Modify the remark associated with the share by calling the NetShareSetInfo function.

The code also allocates and deallocates the memory required for both buffers.

#include <stdio.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;
   struct share_info_50* pBufNew = NULL;
   unsigned short cbBuffer;
   unsigned short nTotalAvail;
   NET_API_STATUS nStatus;
   //
   // ServerName can be NULL to indicate the local computer.
   //
   if ((argc < 3) || (argc > 4))
   {
      printf("Usage: %s [\\\\ServerName] ShareName Comment\n", argv[0]);
      exit(1);
   }

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

   // Note that for Win9x peer servers,
   //   the share name needs to be uppercase.
   //
   pszNetName = argv[argc-2];

   // Call the NetShareGetInfo function once to determine the 
   //   total size needed for the first buffer.
   cbBuffer = 0;
   nStatus = NetShareGetInfo(pszServerName,
                             pszNetName,
                             nLevel,
                             (char FAR *)pBuf,
                             cbBuffer,
                             &nTotalAvail);
   //
   // Allocate the memory required for the first 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 succeeds, allocate the memory required 
   //   for a second buffer. 
   //
   if (nStatus == NERR_Success)
   {
      pBufNew = malloc(cbBuffer);

      if (pBufNew == NULL)
         printf("No memory\n");
     //
     // Copy the first buffer to the second buffer. 
     //   This is necessary to prevent resetting other share
     //   information when you call NetShareSetInfo.
     //
     CopyMemory(pBufNew, pBuf, cbBuffer);
     //
     // Assign a new value to the comment 
     //  associated with the share. 
     //
     strcpy(pBufNew->shi50_remark, argv[argc-1]);
      //
      // Call NetShareSetInfo to make changes, specifying
      //   PARMNUM_ALL to reset all information.
      //
      nStatus = NetShareSetInfo(pszServerName,
                                pszNetName,
                                nLevel,
                                (char FAR *)pBufNew,
                                cbBuffer,
                                PARMNUM_ALL);
      //
      // Process errors and 
      //  free the memory allocated for the second buffer.
      //
      if (nStatus != NERR_Success)
          fprintf(stderr, "A system error has occurred (NetShareSetInfo): %d\n", nStatus);
      if (pBufNew != NULL)
         free(pBufNew);
   }
   else
      fprintf(stderr, "A system error has occurred (NetShareGetInfo): %d\n", nStatus);
   //
   // Free the memory allocated for the first buffer.
   //
   if (pBuf != NULL)
      free(pBuf);

   return 0;
}