/**********************************************************************************
*Function:GPRSConnect()
**Description:If there is active CMNET connection,no need to create CMNET connection again.
*If no active connection,then create a CMNET connection,
*Input:N/A
*Output:N/A
*Return:BOOL,TRUE-Success FALSE-FAILURE
*Others:None
***********************************************************************************/
BOOL GPRSConnect()
{
int ret=CloseRasGPRSConnections();
if(1==ret)
return TRUE;
else if(0==ret)
return GPRSCmnetConnect();
else
return FALSE;
}
/**********************************************************************************
*Function:GPRSCmnetConnect()
*Description:Create CMNET GPRS connection
*Input:N/A
*Output:N/A
*Return:BOOL,TRUE-Success FALSE-FAILURE
*Others:None
***********************************************************************************/
BOOL GPRSCmnetConnect()
{
HANDLE hConnection = NULL;
DWORD dwIndex = 0;
DWORD dwResult = 0;
DWORD dwStatus = 0;
BOOL tResult = FALSE;
CONNMGR_CONNECTIONINFO sCI = { 0 };
HRESULT hResult = S_OK;
sCI.cbSize = sizeof(sCI);
sCI.dwParams = CONNMGR_PARAM_GUIDDESTNET;
sCI.dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
sCI.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
sCI.guidDestNet = IID_DestNetInternet;
if (FAILED(ConnMgrEstablishConnectionSync(&sCI, &hConnection, 30000, &dwStatus)))
{
return FALSE;
}
return TRUE;
}
/**********************************************************************************
*Function:ClosRasGPRSConnections()
*Description:If there is active CMNET connection,don't close it,because we need
*CMNET connection in our program.If there is active CMWAP connection,we disconnect it.
*Input:N/A
*Output:N/A
*Return:DWORD,0-no active connection(CMWAP),1-cmnet is active,-1-error happened
*Others:None
***********************************************************************************/
DWORD CloseRasGPRSConnections()
{
int index; // An integer index
DWORD dwError, dwRasConnSize, dwNumConnections; // Number of connections found
RASCONN RasConn[20]; // Buffer for connection state data,Assume the maximum number of entries is 20.
TCHAR CMNET[64]=_T("GPRS连接互联网");
TCHAR CMWAP[64]=_T("移动梦网(GPRS)");
// Assume no more than 20 connections.
RasConn[0].dwSize = sizeof (RASCONN);
dwRasConnSize = 20 * sizeof (RASCONN);
// Find all connections.
if (dwError = RasEnumConnections (RasConn, &dwRasConnSize,&dwNumConnections))
{
return -1;
}
// If there are no connections, return zero.
if (!dwNumConnections)
{
return 0;
}
// Terminate all of the remote access connections.
for (index = 0; index < (int)dwNumConnections; ++index)
{
if(!wcscmp(RasConn[index].szEntryName,CMWAP))
{
if (dwError = RasHangUp (RasConn[index].hrasconn))
return -1;
else
return 0; //successfully disconnect cmwap;
}
if(wcscmp(RasConn[index].szEntryName,CMNET)==0){
return 1;
}
}
return 0;
}