Check whether the network has been disconnected and re-connected in this program. You can change it based on your own situation. To share everything, I hope everyone will continue to develop this spirit, in the smart device project of wm5.0 and wm6.0 Based on MFC, the test passes the CDMA network
**************************************** ***************************
// Connectmanager. h
# Pragma once
Class cconnectmanager
{
Public:
Cconnectmanager (void );
Virtual ~ Cconnectmanager (void );
Void enumnetidentifier (Out cstringarray & strary );
Bool getconnmgravailable ();
Int mapurlandguid (lpctstr lpszurl, guid & guidnetworkobject, out cstring * pcsdesc = NULL );
Bool establishconnection (DWORD dwindex );
Bool establishconnection (guid); // Liu 4-29 modify
Bool waitforconnected (INT ntimeoutsec, DWORD * pdwstatus = NULL );
PRIVATE:
Void releaseconnection ();
Handle m_hconnection;
};
// Connectmanager. cpp
# Include "stdafx. H"
# Include "connectmanager. H"
// # Include "stdafx. H"
// # Include "connectmanager. H"
// For connect manager function
# Include <connmgr. h>
# Pragma comment (Lib, "cellcore. lib ")
Cconnectmanager: cconnectmanager (void)
{
}
Cconnectmanager ::~ Cconnectmanager (void)
{
Releaseconnection ();
}
Bool cconnectmanager: getconnmgravailable ()
{
Handle hconnmgr = connmgrapireadyevent ();
Bool bavailbale = false;
DWORD dwresult =: waitforsingleobject (hconnmgr, 2000 );
If (dwresult = wait_object_0)
{
Bavailbale = true;
}
// Close
If (hconnmgr) closehandle (hconnmgr );
Return bavailbale;
}
Int cconnectmanager: mapurlandguid (maid, out guid & guidnetworkobject, out cstring * pcsdesc/* = NULL */)
{
If (! Lpszurl | lstrlen (lpszurl) <1)
Return false;
Memset (& guidnetworkobject, 0, sizeof (guid ));
Int nindex = 0;
Hresult = connmgrmapurl (lpszurl, & guidnetworkobject, (DWORD *) & nindex );
If (failed (hresult ))
{
Nindex =-1;
DWORD dwlasterror = getlasterror ();
Afxmessagebox (_ T ("cocould not map a request to a network identifier "));
}
Else
{
If (pcsdesc)
{
Connmgr_destination_info destinfo = {0 };
If (succeeded (connmgrenumdestinations (nindex, & destinfo )))
{
* Pcsdesc = destinfo. szdescription;
}
}
}
Return nindex;
}
Void cconnectmanager: enumnetidentifier (Out cstringarray & strary)
{
Connmgr_destination_info networkdestinfo = {0 };
// Obtain the Network List
For (DWORD dwenumindex = 0; dwenumindex ++)
{
Memset (& networkdestinfo, 0, sizeof (connmgr_destination_info ));
If (connmgrenumdestinations (dwenumindex, & networkdestinfo) = e_fail)
{
Break;
}
Strary. Add (networkdestinfo. szdescription );
// Afxmessagebox (dwenumindex );
Cstring tt;
TT. Format (L "% u", dwenumindex );
}
}
// Specify the dial-up connection
Bool cconnectmanager: establishconnection (DWORD dwindex)
{
// Release the previous connection
Releaseconnection ();
// Obtain the correct connection information
Connmgr_destination_info destinfo = {0 };
Hresult = connmgrenumdestinations (dwindex, & destinfo );
Bool Bret = false;
If (succeeded (hresult ))
{
// Initialize the Connection Structure
Connmgr_connectioninfo conninfo;
Zeromemory (& conninfo, sizeof (conninfo ));
Conninfo. cbsize = sizeof (conninfo );
Conninfo. dwparams = connmgr_param_guiddestnet;
Conninfo. dwflags = connmgr_flag_proxy_http | connmgr_flag_proxy_wap | connmgr_flag_proxy_socks4 | signature; // The dwflags parameter is used to specify the access point, such as the cmner and cmwap
Conninfo. dwpriority = connmgr_priority_userinteractive;
Conninfo. guiddestnet = destinfo. guid;
Conninfo. bexclusive = false;
Conninfo. bdisabled = false;
DWORD dwstatus = 0;
Hresult = connmgrestablishconnectionsync (& conninfo, & m_hconnection, 10*1000, & dwstatus );
If (failed (hresult ))
{
M_hconnection = NULL;
}
Else Bret = true;
}
Return Bret;
}
// Add the specified dial-up connection Liu 4-29
Bool cconnectmanager: establishconnection (guid)
{
// Release the previous connection
Releaseconnection ();
// Obtain the correct connection information
Hresult;
Bool Bret = false;
// Initialize the Connection Structure
Connmgr_connectioninfo conninfo;
Zeromemory (& conninfo, sizeof (conninfo ));
Conninfo. cbsize = sizeof (conninfo );
Conninfo. dwparams = connmgr_param_guiddestnet;
Conninfo. dwflags = connmgr_flag_proxy_http | connmgr_flag_proxy_wap | forward; // The dwflags parameter is used to specify the access point, for example, the commonly referred cmner and cmwap
Conninfo. dwpriority = connmgr_priority_userinteractive;
Conninfo. guiddestnet = guid;
Conninfo. bexclusive = false;
Conninfo. bdisabled = false;
DWORD dwstatus = 0;
Hresult = connmgrestablishconnectionsync (& conninfo, & m_hconnection, 10*1000, & dwstatus );
If (failed (hresult ))
{
M_hconnection = NULL;
}
Else
Bret = true;
Return Bret;
}
Bool cconnectmanager: waitforconnected (INT ntimeoutsec, DWORD * pdwstatus/* = NULL */)
{
DWORD dwstarttime = gettickcount (); // obtain the number of milliseconds since the startup
Bool Bret = false;
While (gettickcount ()-dwstarttime <(DWORD) ntimeoutsec * 1000) // The interval is set to 2 seconds.
{
If (m_hconnection)
{
DWORD dwstatus = 0;
Hresult hR = connmgrconnectionstatus (m_hconnection, & dwstatus); // using this function to return dwstatus, we can get a lot of information about the connection. For example, if the connection is successful, we will return connmgr_status_connected, if the connection is disconnected, connmgr_status_disconnected is returned. The returned status information is rich.
If (pdwstatus) * pdwstatus = dwstatus;
If (succeeded (HR ))
{
If (dwstatus = connmgr_status_connected)
{
Bret = true;
Break;
}
}
}
Sleep (100 );
}
Return Bret;
}
Void cconnectmanager: releaseconnection ()
{
If (m_hconnection)
{
Connmgrreleaseconnection (m_hconnection, false );
M_hconnection = NULL;
}
}
**************************************** **************************************** *
Test cases:
Bool yourclass: connectmanagertest (void)
{
Bool Bret = false;
DWORD dwstatus = 0;
Cstring URL;
Cstring strdialup;
Guid;
Carray <cstring, cstring> SS; // The first parameter is the variable type of the array element of the carray class. The last parameter type is used for function calling.
If (! M_connectmanager.waitforconnected (2, & dwstatus ))
{
M_connectmanager.mapurlandguid (URL, guid); // The URL is a specified public Address URL, which can be any one, such as www.baidu.com.
If (m_connectmanager.establishconnection (guid ))
{
Bret = true;
}
Else
{
Bret = false;
}
}
Else
Bret = true;
Return Bret;
}