取消网络连接

若要取消与网络资源的连接,应用程序可以调用 WNetCancelConnection2 函数,如以下示例所示。

调用 WNetCancelConnection2 指定网络连接不应再持久。 此示例调用应用程序定义的错误处理程序来处理错误,以及用于打印的 TextOut 函数。

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) 
{ 
    printf("Drive z: not connected.\n"); 
    return dwResult; 
} 
 
// Call an application-defined error handler.
//
else if(dwResult != NO_ERROR) 
{ 
    printf("WNetCancelConnection2 failed.\n"); 
    return dwResult; 
}
//
// Otherwise, report canceling the connection.
//
printf("Connection closed for z: drive.\n"); 

支持 WNetCancelConnection 函数,以便与早期版本的 Windows for Workgroups 兼容。 对于新应用程序,请使用 WNetCancelConnection2

有关使用应用程序定义的错误处理程序的详细信息,请参阅 检索网络错误