Establishing a Network Connection

To establish a network connection, the user must know something about the network to be connected to. At a minimum, the user must know the local name or UNC of the network. For more information on functions you can use to retrieve network data, see Retrieving Network Data. An application can use one of two functions to establish network connections: WNetAddConnection3 and WNetConnectionDialog1. Network connections are not automatically restored in Windows CE when a user logs on.

If a user knows the data needed to identify the network resource, an application can call WNetAddConnection3. The following code example shows how WNetAddConnection3 is used to establish a connection with a network resource described by a NETRESOURCE structure.

DWORD dwResult;

// Make a connection to the network resource.
dwResult = WNetAddConnection3 (
                hwnd,                     // Handle to the owner window
                &nr,                      // Retrieved from enumeration
                NULL,                     // No password
                NULL,                     // Logged-in user
                CONNECT_UPDATE_PROFILE);  // Update profile with
                                          // connection data.
if (dwResult == ERROR_ALREADY_ASSIGNED)
{
  MessageBox (hwnd, TEXT("Already connected to specified resource."), 
              TEXT("Info"), MB_OK);
  return FALSE;
}

else if (dwResult == ERROR_DEVICE_ALREADY_REMEMBERED)
{
  MessageBox (hwnd, TEXT("Attempted reassignment of remembered device"), 
              TEXT("Info"), MB_OK);
  return FALSE;
}
else if (dwResult != ERROR_SUCCESS)
{
  ErrorHandler (hwnd, dwResult, TEXT("WNetAddConnection3"));
  return FALSE;
}

MessageBox (hwnd, TEXT("Connected to specified resource."), 
            TEXT("Info"), MB_OK);

The WNetConnectionDialog1 function creates a dialog box that enables a user to browse and connect to network resources. This function prompts the user to choose a local name or UNC in a dialog box. The following code example shows how WNetConnectionDialog1 is called to create a dialog box that displays all disk resources on a network.

DWORD dwResult = WNetConnectionDialog1 (lpConnectDlgStruc);

if (dwResult != ERROR_SUCCESS)
{
  ErrorHandler (hwnd, dwResult, TEXT("WNetConnectionDialog1"));
  return FALSE;
}