WNetCancelConnection function

The WNetCancelConnection function cancels an existing network connection.

The WNetCancelConnection function is provided for compatibility with 16-bit versions of Windows. Other Windows-based applications should call the WNetCancelConnection2 function.


DWORD WNetCancelConnection(
  _In_ LPCTSTR lpName,
  _In_ BOOL    fForce


lpName [in]

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.

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

fForce [in]

Specifies whether or not 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 codeDescription

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.


Minimum supported client

Windows 2000 Professional [desktop apps only]

Minimum supported server

Windows 2000 Server [desktop apps only]







Unicode and ANSI names

WNetCancelConnectionW (Unicode) and WNetCancelConnectionA (ANSI)

See also

Windows Networking (WNet) Overview
Windows Networking Functions



Community Additions

© 2015 Microsoft