WNetCancelConnection2

This function breaks an existing network connection. It may also be used to remove a persistent connection for which there is no existing network connection.

When WNetCancelConnection2 is called it removes the connection entirely if it is to a redirected local device or a non-reference counted session, or if CONNECT_REFCOUNT bit is not set in the dwFlags parameter. Otherwise WNetCancelConnection2 decrements the reference count to the session and removes the session when it is no longer being referenced.

DWORD WNetCancelConnection2(
    LPTSTR lpName,
    DWORD dwFlags,
    BOOL fForce
    );
 
lpName
The name of either the redirected local device or the remote network resource to disconnect from. In the former case, only the redirection specified is broken, in the latter case only the deviceless connection to the remote network resource is broken. If WNetUseConnection was called to make the connection with a non-empty lpLocalName field, then lpName is set to this, otherwise it should be the name that was returned in the lpAccessName field.
dwFlags
This is a bit mask which may have any of the following bits set:
CONNECT_UPDATE_PROFILE If the disconnection should remain persistent. If set, Windows will no longer restore this connection when the user logs on.
CONNECT_REFCOUNT If this bit is not set, the connection is to a redirected local device, or the connection is not reference counted, then the connection is cancelled completely (which will interfere with other threads or processes that are using the connection). If this bit is set and the connection supports reference counts then the reference count to the connection is descremented. When nothing is referencing the connection it is cancelled.

fForce
Used to indicate if the disconnect should be done forcefully in the event of open files or jobs on the connection. If FALSE is specified, the call will fail if there are open files or jobs. If TRUE is specified, then CONNECT_REFCOUNT must not be set in dwFlags.
WN_NOT_CONNECTED lpName not a redirected or persistent device, or not currently connected to lpName.
WN_CANNOT_OPEN_PROFILE Unable to open the user profile to process persistent connections.
WN_DEVICE_IN_USE The device is in use by an active process and cannot be disconnected.
WN_BAD_PROFILE The user profile is in an incorrect format.
WN_OPEN_FILES there are open files and fForce was FALSE.
WN_FUNCTION_BUSY The MPR or NP is busy (possibly initializing). The caller should retry.
WN_EXTENDED_ERROR a network specific error occurred. WNetGetLastError should be called to obtain a description of the error.