WNetAddConnection3

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

The WNetAddConnection3 function is similar to the WNetAddConnection2 function. The main difference is that WNetAddConnection3 has an additional parameter, a handle to a window that the provider of network resources can use as an owner window for dialog boxes.

DWORD WNetAddConnection3(
  HWND hwndOwner,               // handle to an owner window for 
                                // network provider dialog boxes
  LPNETRESOURCE lpNetResource,  // pointer to structure that 
                                // specifies connection details
  LPTSTR lpPassword,            // pointer to password string
  LPTSTR lpUserName,            // pointer to user name string
  DWORD dwFlags                 // set of bit flags that specify 
                                // connection options
);
 

Parameters

hwndOwner
Specifies a window that the provider of network resources can use as an owner window for dialog boxes.

The hwndOwner parameter may be NULL. If it is, a call to WNetAddConnection3 is equivalent to calling the WNetAddConnection2 function.

lpNetResource
Points to a NETRESOURCE data structure that specifies details of the proposed connection: 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 lpLocalName points to a non-empty string, this member can be RESOURCETYPE_DISK or RESOURCETYPE_PRINT. If lpLocalName is NULL or points to an empty string, dwType can be RESOURCETYPE_DISK, RESOURCETYPE_PRINT, or RESOURCETYPE_ANY.
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 treated in a case-insensitive manner. If the string is empty or 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 . The string 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 points to an empty string, the operating system attempts to determine the correct provider by parsing the string pointed to by lpRemoteName.
You should set this member only if you know for sure which network provider you want to use. Otherwise, let the operating system determine which network provider the network name maps to.
If this member is not NULL, the operating system attempts to make a connection only to the named network provider.

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

lpPassword
Points to a 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 lpUserName.

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

lpUserName
Points to a null-terminated string that specifies a user name to be used in 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.

dwFlags
A set of bit flags that specify connection options. The following bit flag constants are 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 unsuccessful connections and deviceless connections. A deviceless connection occurs when lpLocalName is NULL or points to an empty string.

If this bit flag is clear, the operating system will 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 is an error code. Returning an error code provides compatibility with the behavior of the Windows 3.1 function WNetAddConnection. You can also call the GetLastError function to obtain the (same) error code. One of the following error codes may be returned when WNetAddConnection3 fails:

Value Meaning
ERROR_ACCESS_DENIED Access to the network resource was denied.
ERROR_ALREADY_ASSIGNED The local device specified by lpLocalName 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 lpRemoteName is not acceptable to any network resource provider. The resource name is invalid, or the named resource cannot be located.
ERROR_BAD_PROFILE The user profile is in an incorrect format.
ERROR_BAD_PROVIDER The value specified by lpProvider 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 in lpLocalName is already in the user profile.
ERROR_EXTENDED_ERROR A network-specific error occured. Call the WNetGetLastError function to get a description of the error.
ERROR_INVALID_PASSWORD The specified password is invalid.
ERROR_NO_NET_OR_BAD_PATH A network component has not started, or the specified name could not be handled.
ERROR_NO_NETWORK There is no network present.

Remarks

Windows CE: Windows CE does not support "drive-letters" (for example, "F:") as local names of redirected resources. The lpLocalName field of the NETRESOURCE structure can contain any string of up to 64 characters long. If the local name is NULL or empty, the resource is connected without a local name. If it is "*", the default name will be constructed as "<share> on <server>" extracted from the remote name of "\\server\share".

Except for nameless connections, the connected resource will appear as a subfolder under the \NETWORK directory. The subfolder is named after the lpLocalName string.

Windows CE supports only the "Microsoft Windows Network" provider. Persistent connections are stored in the registry and not automatically restored when the user logs in.

QuickInfo

  Windows NT: Use version 3.5 or later.
  Windows: Unsupported.
  Windows CE: Use version 2.0 or later.
  Header: Declared in winnetwk.h.
  Import Library: Use mpr.lib.

See Also

Windows Networking (WNet) Overview, Windows Networking Functions, NETRESOURCE, WNetAddConnection2, WNetCancelConnection2, WNetGetConnection, WNetGetLastError