Platform SDK: Windows Networking

Canceling a Network Connection

Windows NT/Windows 2000

To cancel a connection to a network resource, an application can call the WNetCancelConnection2 function, as shown in the following example. The call to WNetCancelConnection2 specifies that a network connection should no longer be persistent. The sample calls an application-defined error handler to process errors, and the TextOut function for printing.

DWORD dwResult; 
 
// Call the WNetCancelConnection2 function, specifying
//  that the connection should no longer be a persistent one.
//
dwResult = WNetCancelConnection2("z:", 
    CONNECT_UPDATE_PROFILE, // remove connection from profile 
    FALSE);                 // fail if open files or jobs 
 
// Process errors.
//  The device is not a local redirected device.
//
if (dwResult == ERROR_NOT_CONNECTED) 
{ 
    TextOut(hdc, 10, 10, "Drive z: not connected.", 23); 
    return FALSE; 
} 
 
// Call an application-defined error handler.
//
else if(dwResult != NO_ERROR) 
{ 
    NetErrorHandler(hwnd, dwResult, (LPSTR)"WNetCancelConnection2"); 
    return FALSE; 
}
//
// Otherwise, report canceling the connection.
//
TextOut(hdc, 10, 10, "Connection closed for z:.", 25); 

The WNetCancelConnection function is supported for compatibility with earlier versions of Windows for Workgroups. For new applications, use WNetCancelConnection2.

For more information about using an application-defined error handler, see Retrieving Network Errors.