若要連線至 NETRESOURCE 結構所描述的網路資源,應用程式可以呼叫 WNetAddConnection2、WNetAddConnection3或 WNetUseConnection 函式。 下列範例示範如何使用 WNetAddConnection2 函式。
程式代碼範例會呼叫 WNetAddConnection2 函式,並指定系統應該使用資訊更新使用者設定檔,建立記憶或持續性的連線。 此範例會呼叫應用程式定義的錯誤處理程式來處理錯誤,以及用於列印的 TextOut 函式。
DWORD dwResult;
NETRESOURCE nr;
//
// Call the WNetAddConnection2 function to make the connection,
// specifying a persistent connection.
//
dwResult = WNetAddConnection2(&nr, // NETRESOURCE from enumeration
(LPSTR) NULL, // no password
(LPSTR) NULL, // logged-in user
CONNECT_UPDATE_PROFILE); // update profile with connect information
// Process errors.
// The local device is already connected to a network resource.
//
if (dwResult == ERROR_ALREADY_ASSIGNED)
{
printf("Already connected to specified resource.\n");
return dwResult;
}
// An entry for the local device already exists in the user profile.
//
else if (dwResult == ERROR_DEVICE_ALREADY_REMEMBERED)
{
printf("Attempted reassignment of remembered device.\n");
return dwResult;
}
else if(dwResult != NO_ERROR)
{
//
// Call an application-defined error handler.
//
printf("WNetAddConnection2 failed.\n");
return dwResult;
}
//
// Otherwise, report a successful connection.
//
printf("Connected to the specified resource.\n");
WNetAddConnection 函式為了與舊版 Windows for Workgroups 相容而提供支援。 新的應用程式應該呼叫 WNetAddConnection2 函式或 WNetAddConnection3 函式。
如需使用應用程式定義錯誤處理程式的詳細資訊,請參閱 擷取網路錯誤。