WNetAddConnection2

This function allows the caller to redirect (connect) a local device to a network resource. It is similar to WNetAddConnection, except that it takes a pointer to a NETRESOURCE structure to describe the network resource to connect to. It also takes the addition parameters lpUserID and dwFlags.

DWORD WNetAddConnection2(
    LPNETRESOURCE lpNetResource,
    LPTSTR lpPassword,
    LPTSTR lpUserID,
    DWORD dwFlags
    );
 
lpNetResource
Specifies the network resource to connect to. This structure is defined the section describing Enumeration APIs. The following fields must be set when making a connection, the others are ignored.
lpRemoteName Specifies the network resource to connect to. This is limited to MAX_PATH.
lpLocalName This specifies the name of a local device to be redirected, such as "F:" or "LPT1". The string is treated in a case insensitive manner, and may be the empty string (or NULL) in which case a connection to the network resource is made without making a redirection.
lpProvider Specifies the NP to connect to. If NULL or empty string, Windows will try each NP in turn. The caller should set lpProvider only if it knows for sure which network it wants. Otherwise, it is preferable to let Windows determine which NP the network name maps to. If this is non NULL, Windows will try the named NP and no other.
dwType Specifies the type of resource to connect to. It must be RESOURCETYPE_DISK or RESOURCETYPE_PRINT if lpLocalName is not the empty string. It may also be RESOURCETYPE_ANY if lpLocalName is the empty string.

lpPassword
Specifies the password to be used in making the connection, normally the password associated with lpUserID. A NULL value or string may be passed in to indicate to the function to use the current default password.
lpUserID
This specifies the identity of the user needed to make the connection. If NULL, a default will be applied. This is used when the user wishes to connect to a resource, but has a different user name or account assigned to him for that resource. This identification represents a security context, and is NP specific.
dwFlags
This is a bit mask which may have any of the following bits set:
CONNECT_UPDATE_PROFILE If the connection should be made persistent. If set, Windows automatically restores this connection when the user logs on to the network. A connection is only made persistent if the connection was successful.

ERROR_BAD_DEV_TYPE The device type and the resource type do not match.
WN_BAD_PROVIDER lpProvider does not match any NP
WN_BAD_NETNAME lpRemoteName is not acceptable to any NP
WN_BAD_LOCALNAME lpLocalName is invalid.
WN_BAD_PASSWORD invalid password, or the the connection requires additional authentication not provided by the caller (e.g., a username)
WN_ALREADY_CONNECTED lpLocalName is already connected to a network drive or is to a local drive that is already in use locally (e.g. is used for a hard drive or RAM drive).
WN_ACCESS_DENIED access denied (user may need to logon)
WN_CANNOT_OPEN_PROFILE Unable to open the user profile to process persistent connections.
WN_BAD_PROFILE The user profile is in an incorrect format.
WN_FUNCTION_BUSY The MPR or NP is busy (possibly initializing). The caller should retry.
WN_NO_NETWORK network is not present
WN_NO_NET_OR_BAD_PATH the operation could not be handled either because a network component is not started or the specified name could not be handled.
WN_EXTENDED_ERROR a network specific error occurred. WNetGetLastError should be called to obtain a description of the error.