Platform SDK: Network Management

NetShareAdd

The NetShareAdd function shares a server resource.

Security Requirements

Only members of the Administrators or Account Operators local group or those with Communication, Print, or Server operator group membership can successfully execute the NetShareAdd function. The Print operator can add only Printer queues. The Communication operator can add only communication-device queues.

Windows NT/2000: The parameter order is as follows.

NET_API_STATUS NetShareAdd(
  LPWSTR servername, 
  DWORD level,       
  LPBYTE buf,        
  LPDWORD parm_err   
);

Windows 95/98: You must specify the size of the information buffer, in bytes, using the cbBuffer parameter. The Windows NT/Windows 2000 parm_err parameter is not available on this platform. Therefore, the parameter list is as follows.

extern API_FUNCTION
 NetShareAdd(
  const char FAR * pszServer,       
  short sLevel,                     
  const char FAR * pbBuffer,        
  unsigned short  cbBuffer          
);

Parameters

servername
[in] Pointer to a Unicode (Windows NT/2000) or ANSI (Windows 95/98) string specifying the name of the remote server on which the function is to execute. The string must begin with \\. If this parameter is NULL, the local computer is used.
level
[in] Specifies the information level of the data. This parameter can be one of the following values.

Windows NT/2000: The following levels are valid.
Value Meaning
2 Specifies information about the shared resource, including name of the resource, type and permissions, and number of connections. The buf parameter points to a SHARE_INFO_2 structure.
502 Specifies information about the shared resource, including name of the resource, type and permissions, number of connections, and other pertinent information. The buf parameter points to a SHARE_INFO_502 structure.

Windows 95/98: The following level is valid.
Value Meaning
50 Specifies information about the shared resource, including the name and type of the resource, a comment associated with the resource, and passwords. The pbBuffer parameter points to a share_info_50 structure.

buf
[in] Pointer to the buffer that specifies the data. The format of this data depends on the value of the level parameter.
parm_err
[out] Pointer to a DWORD value that receives the index of the first member of the share information structure that causes the ERROR_INVALID_PARAMETER error. If this parameter is NULL, the index is not returned on error. For more information, see the NetShareSetInfo function.

Return Values

If the function succeeds, the return value is NERR_Success.

If the function fails, the return value can be one of the following error codes.

Value Meaning
ERROR_ACCESS_DENIED The user does not have access to the requested information.
ERROR_INVALID_LEVEL The value specified for the level parameter is invalid.
ERROR_INVALID_NAME The character or file system name is invalid.
ERROR_INVALID_PARAMETER The specified parameter is invalid.
NERR_DuplicateShare The share name is already in use on this server.
NERR_RedirectedPath The operation is invalid for a redirected resource. The specified device name is assigned to a shared resource.
NERR_UnknownDevDir The device or directory does not exist.

Remarks

Windows 95/98: See the NetShareAdd Sample (Windows 95/98) topic to view a code sample that demonstrates how to use the NetShareAdd function.

Windows NT/2000: The following code sample demonstrates how to share a network resource using a call to the NetShareAdd function. The code sample fills in the members of the SHARE_INFO_2 structure and calls NetShareAdd, specifying information level 2.

#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <lm.h>

void wmain( int argc, TCHAR *argv[ ])
{
   NET_API_STATUS res;
   SHARE_INFO_2 p;
   DWORD parm_err = 0;

   if(argc<2)
      printf("Usage: NetShareAdd server\n");
   else
   {
      //
      // Fill in the SHARE_INFO_2 structure.
      //
      p.shi2_netname = TEXT("TESTSHARE");    
      p.shi2_type = STYPE_DISKTREE; // disk drive
      p.shi2_remark = TEXT("TESTSHARE to test NetShareAdd");
      p.shi2_permissions = 0;    
      p.shi2_max_uses = 4;
      p.shi2_current_uses = 0;    
      p.shi2_path = TEXT("C:\\");
      p.shi2_passwd = NULL; // no password
      //
      // Call the NetShareAdd function,
      //  specifying level 2.
      //
      res=NetShareAdd(argv[1], 2, (LPBYTE) &p, &parm_err);
      //
      // If the call succeeds, inform the user.
      //
      if(res==0)
         printf("Share created.\n");
      
      // Otherwise, print an error,
      //  and identify the parameter in error.
      //
      else
         printf("Error: %u\tparmerr=%u\n", res, parm_err);
   }
   return;
}

If you are programming for Active Directory, you may be able to call certain Active Directory Service Interface (ADSI) methods to achieve the same functionality you can achieve by calling the network management share functions. For more information, see IADsFileShare.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Lmshare.h (Windows NT/2000) or Svrapi.h (Windows 95/98); include Lm.h (Windows NT/2000).
  Library: Use Netapi32.lib (Windows NT/2000) or Svrapi.lib (Windows 95/98).

See Also

Network Management Overview, Network Management Functions, Share Functions, NetShareDel, NetShareSetInfo, SHARE_INFO_2, SHARE_INFO_502, share_info_50