NetShareAdd function (lmshare.h)

Shares a server resource.

Syntax

NET_API_STATUS NET_API_FUNCTION NetShareAdd(
  [in]  LMSTR   servername,
  [in]  DWORD   level,
  [in]  LPBYTE  buf,
  [out] LPDWORD parm_err
);

Parameters

[in] servername

Pointer to a string that specifies the DNS or NetBIOS name of the remote server on which the function is to execute. If this parameter is NULL, the local computer is used.

[in] level

Specifies the information level of the data. This parameter can be one of the following values.

Value Meaning
2
Specifies information about the shared resource, including the 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 the name of the resource, type and permissions, number of connections, and other pertinent information. The buf parameter points to a SHARE_INFO_502 structure.
503
Specifies information about the shared resource, including the name of the resource, type and permissions, number of connections, and other pertinent information. The buf parameter points to a SHARE_INFO_503 structure.

[in] buf

Pointer to the buffer that specifies the data. The format of this data depends on the value of the level parameter. For more information, see Network Management Function Buffers.

[out] parm_err

Pointer to a 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 value

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.

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

Remarks

This function applies only to Server Message Block (SMB) shares. For other types of shares, such as Distributed File System (DFS) or WebDAV shares, use Windows Networking (WNet) functions, which support all types of shares.

Only members of the Administrators, System Operators, or Power Users local group can add file shares with a call to the NetShareAdd function. The Print Operator can add printer shares.

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.

If 503 is specified for the level parameter, the remote server specified in the shi503_servername member of the SHARE_INFO_503 structure must have been bound to a transport protocol using the NetServerTransportAddEx function. In the call to NetServerTransportAddEx, either 2 or 3 must have been specified for the level parameter, and the SVTI2_SCOPED_NAME flag must have been specified in the SERVER_TRANSPORT_INFO_2 structure for the transport protocol.

Examples

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. A password is not required because these platforms do not support share-level security.

#ifndef UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <stdio.h>
#include <lm.h>
#pragma comment(lib, "Netapi32.lib")

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;
}

Requirements

Requirement Value
Minimum supported client Windows XP [desktop apps only]
Minimum supported server Windows Server 2003 [desktop apps only]
Target Platform Windows
Header lmshare.h (include Lm.h)
Library Netapi32.lib
DLL Netapi32.dll

See also

NetServerTransportAddEx

NetShareDel

NetShareDelEx

NetShareSetInfo

Network Management Functions

Network Management Overview

Network Share Functions

SHARE_INFO_2

SHARE_INFO_502

SHARE_INFO_503