Share via


取消網路連線

若要取消網路資源的連線,應用程式可以呼叫 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

如需使用應用程式定義錯誤處理常式的詳細資訊,請參閱 擷取網路錯誤