共用方式為


擷取連線名稱

若要擷取與本機裝置相關聯的網路資源名稱,應用程式可以呼叫 WNetGetConnection 函式,如下列範例所示。

下列範例會呼叫應用程式定義的錯誤處理常式來處理錯誤,以及用於列印的 TextOut 函式。

TCHAR szDeviceName[80]; 
DWORD dwResult, cchBuff = sizeof(szDeviceName); 
 
// Call the WNetGetConnection function.
//
dwResult = WNetGetConnection(_T("z:"), 
    szDeviceName, 
    &cchBuff); 
 
switch (dwResult) 
{ 
    //
    // Print the connection name or process errors.
    //
    case NO_ERROR: 
        printf("Connection name: %s\n", szDeviceName); 
        break; 
    //
    // The device is not a redirected device.
    //
    case ERROR_NOT_CONNECTED: 
        printf("Device z: not connected.\n"); 
        break;
    //
    // The device is not currently connected, but it is a persistent connection.
    //
    case ERROR_CONNECTION_UNAVAIL: 
        printf("Connection unavailable.\n"); 
        break;
    //
    // Handle the error.
    //
    default: 
        printf("WNetGetConnection failed.\n"); 
}

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