Click here to Skip to main content
15,896,606 members
Articles / Mobile Apps / Windows Mobile

Using the Connection Manager

Rate me:
Please Sign up or sign in to vote.
4.78/5 (8 votes)
30 Jul 2010CPOL5 min read 65.3K   5K   24  
How to effectively use the Connection Manager API to connect to an arbitrary network.
#pragma once
#include <connmgr_status.h>

// Bonus!
// This WTL class can be added to any CWindowImpl<> derived Window (as in the 
// example below) to be notified of any changes in the connection manager 
// status.
//
// class CMyWindow : public CWindowImpl< CMyWindow >, 
//                   // ...
//                   public CMStatusChangeWindow< CMyWindow >
// {
// public:
//     BEGIN_MSG_MAP( CMyWindow )
//         // ...
//         CHAIN_MSG_MAP( CMStatusChangeWindow< CMyWindow > )
//     END_MSG_MAP()
// 
//     void OnStatusChange( DWORD status )
//     {
//         // Alert! The Connection Manager status has changed.
//     };
// }; // class CMyWindow 
//

/// @brief sent by connection manager to indicate a change in status
/// @param WPARAM wParam - [DWORD] new status
/// @param LPARAM lParam - unused.
static const UINT UWM_CM_STATUS_CHANGE = 
    ::RegisterWindowMessage( CONNMGR_STATUS_CHANGE_NOTIFICATION_MSG );

/// A window that listens for connection manager status change notifications
template< class T >
class CMStatusChangeWindow
{
public:
    BEGIN_MSG_MAP( CMStatusChangeWindow< T > )
        MESSAGE_HANDLER( WM_CREATE, OnCreate )
        MESSAGE_HANDLER( UWM_CM_STATUS_CHANGE, OnCMStatusChange )
        MESSAGE_HANDLER( WM_DESTROY, OnDestroy )
    END_MSG_MAP()

    LRESULT OnCreate( UINT /*uMsg*/, 
                      WPARAM /*wParam*/, 
                      LPARAM /*lParam*/, 
                      BOOL& bHandled )
    {
        bHandled = FALSE;
        T* pT = static_cast< T* >( this );
        ::ConnMgrRegisterForStatusChangeNotification( TRUE, pT->m_hWnd );
        return 0;
    };

    LRESULT OnDestroy( UINT /*uMsg*/, 
                       WPARAM /*wParam*/, 
                       LPARAM /*lParam*/, 
                       BOOL& bHandled )
    {
        bHandled = FALSE;
        T* pT = static_cast< T* >( this );
        ::ConnMgrRegisterForStatusChangeNotification( FALSE, pT->m_hWnd );
        return 0;
    };

    LRESULT OnCMStatusChange( UINT /*uMsg*/, 
                              WPARAM wParam, 
                              LPARAM /*lParam*/, 
                              BOOL& /*bHandled*/ )
    {
        T* pT = static_cast< T* >( this );
        pT->OnStatusChange( static_cast< DWORD >( wParam ) );
        return 0;
    };

    void OnStatusChange( DWORD status )
    {
        // must be implemented in a derived class
        ASSERT( FALSE );
    };
}; // class CMStatusChangeWindow

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) An engineering firm in Cedar Rapids, Iowa
United States United States
I'm also on the MSDN forums
http://social.msdn.microsoft.com/profile/paulh79

Comments and Discussions