WNetCancelConnection2A function (winnetwk.h)

The WNetCancelConnection2 function cancels an existing network connection. You can also call the function to remove remembered network connections that are not currently connected.

The WNetCancelConnection2 function supersedes the WNetCancelConnection function.


DWORD WNetCancelConnection2A(
  [in] LPCSTR lpName,
  [in] DWORD  dwFlags,
  [in] BOOL   fForce


[in] lpName

Pointer to a constant null-terminated string that specifies the name of either the redirected local device or the remote network resource to disconnect from.

If this parameter specifies a redirected local device, the function cancels only the specified device redirection. If the parameter specifies a remote network resource, all connections without devices are canceled.

[in] dwFlags

Connection type. The following values are defined.

Value Meaning
The system does not update information about the connection.

If the connection was marked as persistent in the registry, the system continues to restore the connection at the next logon. If the connection was not marked as persistent, the function ignores the setting of the CONNECT_UPDATE_PROFILE flag.

The system updates the user profile with the information that the connection is no longer a persistent one.

The system will not restore this connection during subsequent logon operations. (Disconnecting resources using remote names has no effect on persistent connections.)

[in] fForce

Specifies whether the disconnection should occur if there are open files or jobs on the connection. If this parameter is FALSE, the function fails if there are open files or jobs.

Return value

If the function succeeds, the return value is NO_ERROR.

If the function fails, the return value is a system error code, such as one of the following values.

Return code Description
The user profile is in an incorrect format.
The system is unable to open the user profile to process persistent connections.
The device is in use by an active process and cannot be disconnected.
A network-specific error occurred. To obtain a description of the error, call the WNetGetLastError function.
The name specified by the lpName parameter is not a redirected device, or the system is not currently connected to the device specified by the parameter.
There are open files, and the fForce parameter is FALSE.


Windows Server 2003 and Windows XP:  The WNet functions create and delete network drive letters in the MS-DOS device namespace associated with a logon session because MS-DOS devices are identified by AuthenticationID. (An AuthenticationID is the locally unique identifier, or LUID, associated with a logon session.) This can affect applications that call one of the WNet functions to create a network drive letter under one user logon, but query for existing network drive letters under a different user logon. An example of this situation could be when a user's second logon is created within a logon session, for example, by calling the CreateProcessAsUser function, and the second logon runs an application that calls the GetLogicalDrives function. GetLogicalDrives does not return network drive letters created by a WNet function under the first logon. Note that in the preceding example the first logon session still exists, and the example could apply to any logon session, including a Terminal Services session. For more information, see Defining an MS-DOS Device Name.


For a code sample that illustrates how to cancel a connection to a network resource with a call to the WNetCancelConnection2 function, see Canceling a Network Connection.


The winnetwk.h header defines WNetCancelConnection2 as an alias which automatically selects the ANSI or Unicode version of this function based on the definition of the UNICODE preprocessor constant. Mixing usage of the encoding-neutral alias with code that not encoding-neutral can lead to mismatches that result in compilation or runtime errors. For more information, see Conventions for Function Prototypes.


Requirement Value
Minimum supported client Windows 2000 Professional [desktop apps only]
Minimum supported server Windows 2000 Server [desktop apps only]
Target Platform Windows
Header winnetwk.h
Library Mpr.lib
DLL Mpr.dll

See also




Windows Networking (WNet) Overview

Windows Networking Functions