Platform SDK: Windows Networking

WNetAddConnection2

The WNetAddConnection2 function makes a connection to a network resource. The function can redirect a local device to the network resource.

The WNetAddConnection2 function supersedes the WNetAddConnection function. If you can pass a handle to a window that the provider of network resources can use as an owner window for dialog boxes, call the WNetAddConnection3 function instead.

DWORD WNetAddConnection2(
  LPNETRESOURCE lpNetResource,  // connection details
  LPCTSTR lpPassword,           // password
  LPCTSTR lpUsername,           // user name
  DWORD dwFlags                 // connection options
);

Parameters

lpNetResource
[in] Pointer to a NETRESOURCE structure that specifies details of the proposed connection, such as information about the network resource, the local device, and the network resource provider.

You must specify the following members of the NETRESOURCE structure.
Member Description
dwType Specifies the type of network resource to connect to.

If the lpLocalName member points to a nonempty string, this member can be equal to RESOURCETYPE_DISK or RESOURCETYPE_PRINT.

If lpLocalName is NULL, or if it points to an empty string, dwType can be equal to RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or RESOURCETYPE_ANY.

lpLocalName Points to a null-terminated string that specifies the name of a local device to redirect, such as "F:" or "LPT1". The string is treated in a case-insensitive manner.

If the string is empty, or if lpLocalName is NULL, the function makes a connection to the network resource without redirecting a local device.

lpRemoteName Points to a null-terminated string that specifies the network resource to connect to. The string can be up to MAX_PATH characters in length, and must follow the network provider's naming conventions.
lpProvider Points to a null-terminated string that specifies the network provider to connect to.

If lpProvider is NULL, or if it points to an empty string, the operating system attempts to determine the correct provider by parsing the string pointed to by the lpRemoteName member.

If this member is not NULL, the operating system attempts to make a connection only to the named network provider.

You should set this member only if you know the network provider you want to use. Otherwise, let the operating system determine which provider the network name maps to.


The WNetAddConnection2 function ignores the other members of the NETRESOURCE structure.

lpPassword
[in] Pointer to a constant null-terminated string that specifies a password to be used in making the network connection.

If lpPassword is NULL, the function uses the current default password associated with the user specified by the lpUserName parameter.

If lpPassword points to an empty string, the function does not use a password.

Windows 95/98: This parameter must be NULL or an empty string.

lpUsername
[in] Pointer to a constant null-terminated string that specifies a user name for making the connection.

If lpUserName is NULL, the function uses the default user name. (The user context for the process provides the default user name.)

The lpUserName parameter is specified when users want to connect to a network resource for which they have been assigned a user name or account other than the default user name or account.

The user-name string represents a security context. It may be specific to a network provider.

Windows 95/98: This parameter must be NULL or an empty string.

dwFlags
[in] Specifies a DWORD value that describes connection options. The following value is currently defined.
Value Meaning
CONNECT_UPDATE_PROFILE The network resource connection should be remembered.

If this bit flag is set, the operating system automatically attempts to restore the connection when the user logs on.

The operating system remembers only successful connections that redirect local devices. It does not remember connections that are unsuccessful or deviceless connections. (A deviceless connection occurs when the lpLocalName member is NULL or points to an empty string.)

If this bit flag is clear, the operating system does not automatically restore the connection at logon.


Return Values

If the function succeeds, the return value is NO_ERROR.

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

Value Meaning
ERROR_ACCESS_DENIED Access to the network resource was denied.
ERROR_ALREADY_ASSIGNED The local device specified by the lpLocalName member is already connected to a network resource.
ERROR_BAD_DEV_TYPE The type of local device and the type of network resource do not match.
ERROR_BAD_DEVICE The value specified by lpLocalName is invalid.
ERROR_BAD_NET_NAME The value specified by the lpRemoteName member is not acceptable to any network resource provider, either because the resource name is invalid, or because the named resource cannot be located.
ERROR_BAD_PROFILE The user profile is in an incorrect format.
ERROR_BAD_PROVIDER The value specified by the lpProvider member does not match any provider.
ERROR_BUSY The router or provider is busy, possibly initializing. The caller should retry.
ERROR_CANCELLED The attempt to make the connection was cancelled by the user through a dialog box from one of the network resource providers, or by a called resource.
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 by lpLocalName is already in the user profile.
ERROR_EXTENDED_ERROR A network-specific error occurred. Call 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 cannot be performed because a network component is not started or because a specified name cannot be used.
ERROR_NO_NETWORK The network is unavailable.

Remarks

For a code sample that illustrates how to make a connection to a network resource using the WNetAddConnection2 function, see Adding a Network Connection. For an example that demonstrates how to use the WNetAddConnection2 function to connect a drive letter to a remote share on a server, see Assigning a Drive to a Share.

Requirements

  Windows NT/2000: Requires Windows NT 3.1 or later.
  Windows 95/98: Requires Windows 95 or later.
  Header: Declared in Winnetwk.h.
  Library: Use Mpr.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.

See Also

Windows Networking (WNet) Overview, Windows Networking Functions, WNetAddConnection3, WNetCancelConnection2, WNetGetConnection, NETRESOURCE