Partager via


Handling Connection Status Notifications (Windows Embedded CE 6.0)

1/6/2010

This topic describes how an application can handle connection-status notifications from Connection Manager after the application has established a connection.

To complete this task, you must have completed the previous steps in Making a Data Connection Using Connection Manager.

To handle connection-status notifications in a connected application

  1. After a connection is already established in your application code, call ConnMgrConnectionStatus to retrieve the current connection status. The current status is returned in pdwStatus.

  2. Post a custom message to the main window, such as WM_CONNECTIONSTATE, that includes the value of pdwStatus in one of its parameters. **You can do this by calling PostMessage and passing in a handle to the main application window.

  3. In your WndProc, provide message-handling code for WM_CONNECTIONSTATE that handles states such as the following:

    • CONNMGR_STATUS_DISCONNECTED
    • CONNMGR_STATUS_SUSPENDED
    • CONNMGR_STATUS_CONNECTIONCANCELED
    • CONNMGR_STATUS_CONNECTIONDISABLED
    • CONNMGR_STATUS_WAITINGCONNECTION
    • CONNMGR_STATUS_WAITINGDISCONNECTION

    For a list of values, see Connection Manager Status Constants.

  4. Rebuild your application.

Example

The following code example shows you how to retrieve the updated connection status and post a message to the main window that contains the updated status information.

#include <windows.h>
#include <connmgr.h>

CONNMGR_CONNECTIONINFO ConnInfo = { 0 };
ConnInfo.cbSize     = SIZEOF(ConnInfo);
ConnInfo.dwParams   = CONNMGR_PARAM_GUIDDESTNET;
ConnInfo.dwFlags    = 0;
ConnInfo.dwPriority = (m_fInteractive ? CONNMGR_PRIORITY_USERINTERACTIVE : CONNMGR_PRIORITY_HIPRIBKGND);
ConnInfo.guidDestNet= m_guidCurrentPath;
ConnInfo.hWnd = m_hWndNotify;
ConnInfo.uMsg = WM_CONNECTSTATE;

HANDLE hConnection = NULL;

HRESULT hr = ConnMgrEstablishConnection(&ConnInfo, &hConnection);

The following code example shows you how to handle a connection-status message within the callback function for the main window of your application.

LRESULT CALLBACK WndProc (HWND hwnd, UINT umsg, WPARAM wParam, 
                          LPARAM lParam)
{
  switch (umsg)
  {
     case WM_CONNECTSTATE:
    {
        // Status is in wParam.
        if(wParam == CONNMGR_STATUS_CONNECTED) 
       {
            // Status is connected. Post connected-related messages as
            // needed.
        } 
        else if ((wParam != CONNMGR_STATUS_WAITINGCONNECTION) &&
                   (wParam != CONNMGR_STATUS_WAITINGFORRESOURCE) &&
                   (wParam  & (CONNMGR_STATUS_WAITINGCONNECTION |
                               CONNMGR_STATUS_DISCONNECTED)))
        {
            if(wParam != CONNMGR_STATUS_DISCONNECTED)
            {
                // Log an error
            }
            else
            {
                // Write code to notify application of waiting status.
            }
        }
    }
    case WM_CLOSE:
      DestroyWindow (hwnd);
      return 0;

    case WM_DESTROY:
      PostQuitMessage (0);
      return 0;
  }

  return DefWindowProc (hwnd, umsg, wParam, lParam);
}

See Also

Tasks

Establishing an Internet Connection Using Connection Manager

Concepts

Connection Manager How-to Topics

Other Resources

Connection Manager Application Development