Platform SDK: Network Management

NetShareAdd Sample (Windows 95/98)

Windows 95/98: The following code sample demonstrates how to share a resource on the local computer with a call to the NetShareAdd function.

The code sample specifies the share_info_50 structure and no password on the share. The sample also allocates and frees the memory required for the information buffer.

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

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

   if (argc == 4)
      pszServerName = argv[1];
   //
   // Allocate the memory required to specify a 
   //   share_info_50 structure.
   //
   cbBuffer = sizeof(struct share_info_50);
   pBuf = malloc(cbBuffer);

   if (pBuf == NULL)
      printf("No memory\n");
   //
   // Assign values to the share_info_50 structure.
   //
   strcpy(pBuf->shi50_netname, argv[argc-2]);
   pBuf->shi50_type = STYPE_DISKTREE;
   pBuf->shi50_flags = SHI50F_FULL;
   pBuf->shi50_remark = NULL;
   pBuf->shi50_path = argv[argc-1];
   pBuf->shi50_rw_password[0] = '\0'; // No password
   pBuf->shi50_ro_password[0] = '\0'; // No password
   //
   // Call the NetShareAdd function
   //  specifying information level 50.
   //
   nStatus = NetShareAdd(pszServerName,
                         nLevel,
                         (char FAR *)pBuf,
                         cbBuffer);
   //
   // Display the result of the function call.
   //
   if (nStatus == NERR_Success)
      printf("Share added successfully\n");
   else
      fprintf(stderr, "A system error has occurred: %d\n", nStatus);
   //
   // Free the allocated memory.
   //
   if (pBuf != NULL)
      free(pBuf);

   return 0;
}