WNetAddConnection2

  DWORD WNetAddConnection2(lpNetResource, lpszPassword, lpszUserName, fdwConnection)    
  LPNETRESOURCE lpNetResource; /* structure specifying net resource */
  LPTSTR lpszPassword; /* pointer to password */
  LPTSTR lpszUserName; /* pointer to user name */
  DWORD fdwConnection; /* connection-type flags */

The WNetAddConnection2 function allows the caller to redirect (connect) a local device to a network resource.

Parameters

lpNetResource

Points to an NETRESOURCE structure that specifies the network resource to connect to. This structure has the following format:

typedef struct _NETRESOURCE { /* nr */

DWORD dwScope; /* enumeration scope */

DWORD dwType; /* resource type */

DWORD dwUsage; /* resource usage */

LPTSTR lpLocalName; /* local resource name */

LPTSTR lpRemoteName; /* remote resource name */

LPTSTR lpComment; /* provider-supplied comment */

LPTSTR lpProvider; /* name of the provider */

} NETRESOURCE, *LPNETRESOURCE;

The following members must be set in the NETRESOURCE structure:

Member Description

lpRemoteName  
  Points to a null-terminated string that specifies the network resource to connect to.
lpLocalName  
  Points to a null-terminated string that specifies the name of a local device to be redirected, such as “F:” or “LPT1”. The string is not case sensitive. If the string is NULL, a connection to the network resource is made without making a redirection.
lpProvider  
  Points to a null-terminated string that specifies the network provider to connect to. If lpProvider is NULL or points to an empty string, Windows attempts to determine the correct provider.
  An application should set lpProvider only when it must specify a network provider. Otherwise, the application should allow Windows to determine which provider the network name maps to.
  If lpProvider is not NULL, Windows attempts to make a connection only to the named provider.
dwType  
  Specifies the type of resource to connect to. It can be RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or RESOURCETYPE_ANY. The value RESOURCETYPE_ANY is used if the calling application does not care or does not know.

The other members of the NETRESOURCE structure are ignored.

lpszPassword

Points to a null-terminated string that specifies the password to be used to make the connection. This is normally the password associated with the current user.

If lpszPassword is NULL, the default password is used. If the string is empty, no password is used.

lpszUserName

Points to a null-terminated string that specifies the username used to make the connection. If lpszUserName is NULL, the default user context for the process will be applied. This is used when the user wants to connect to a resource but has a different user name or account assigned to him for that resource. This name represents a security context and is provider-specific.

fdwConnection

Specifies a bitmask for the connection type. The following values are defined:

Value Meaning

0  
  No information about the connection is stored.
CONNECT_UPDATE_PROFILE  
  Information about the connection is stored in the user profile. If the connection is successful, Windows will automatically restore this connection during subsequent log-on operations.

Return Value

If the function is successful, the return value is NO_ERROR. Otherwise, the return value is an error code, which may be one of the following:

Value Meaning

ERROR_ACCESS_DENIED Access is denied.
ERROR_ALREADY_ASSIGNED The device specified in lpszLocalName is already connected.
ERROR_BAD_DEVICE The value specified in lpszLocalName is invalid.
ERROR_BAD_NET_NAME The value specified in lpszRemoteName is not acceptable to any provider.
ERROR_BAD_PROFILE The user profile is in an incorrect format.
ERROR_CANNOT_OPEN_PROFILE The system is unable to open the user profile to process persistent connections.
ERROR_DEVICE_ALREADY_REMEMBERED An entry for the device specified in lpszLocalName is already in the user profile.
ERROR_EXTENDED_ERROR A network-specific error occurred. Use the WNetGetLastError function to obtain a description of the error.
ERROR_INVALID_PASSWORD The specified password is invalid.
ERROR_NO_NET_OR_BAD_PATH The operation could not be performed, either because a network component was not started or because the specified name could not be used.
ERROR_NO_NETWORK The network is not present.

This function returns error codes for compatibility with Windows 3.1. For compatibility with Win32, the function also sets the error-code value returned by the GetLastError function.

See Also

WNetAddConnection, WNetCancelConnection2, WNetGetConnection