⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mobiletool.cpp

📁 Mobile平台下GPRS上网连接方式
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#include "stdafx.h"
#include "mobiletool.h"

#include "connmgr.h"
#include "Ras.h"
#include <extapi.h>
#include <tsp.h>
#include <Iptypes.h>
#include <Iphlpapi.h>
#include <projects.h>
//#include <atlbase.h>

#include "../test/ilogger.h"

extern HINSTANCE g_hInst;
extern HWND g_hwnd;
extern HWND g_hWndMenuBar;		// 菜单栏句柄

#ifdef _WIN32_WCE
#pragma comment(lib, "note_prj.lib")
#endif

EXTERN_C const GUID IID_DestNetInternet \
= { 0x436ef144, 0xb4fb, 0x4863, { 0xa0, 0x41, 0x8f, 0x90, 0x5a, 0x62, 0xc5, 0x72 } };

EXTERN_C const GUID IID_DestNetWAP \
= { 0x7022e968, 0x5a97, 0x4051, { 0xbc, 0x1c, 0xc5, 0x78, 0xe2, 0xfb, 0xa5, 0xd9 } };

EXTERN_C const GUID IID_DestNetSecureWAP \
= { 0xf28d1f74, 0x72be, 0x4394, { 0xa4, 0xa7, 0x4e, 0x29, 0x62, 0x19, 0x39, 0x0c } };


#pragma comment( lib, "Iphlpapi.lib" )

/**********************************************************************************
*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( uint32* pnResultStatus )
{
	BOOL bRet = FALSE;
	uint8 nOuterConnection = UNKNOWN;

	//uint32 Version = 0;
	//uint32 Version_maj = 0;
	//uint32 Version_min = 0;
	//OSVERSIONINFO VersionInformation;
	//GetVersionEx( &VersionInformation);
	//if(VersionInformation.dwPlatformId == VER_PLATFORM_WIN32_CE) 
	//{
	//	Version_maj= VersionInformation.dwMajorVersion;
	//	Version_min= VersionInformation.dwMinorVersion;
	//	if(4==Version_maj)
	//	{
	//		Version = 4;
	//		//::MessageBoxW(g_hwnd,L"4==Version",L"",0);
	//	}
	//	if(5==Version_maj)
	//	{
	//		if(1==Version_min)
	//		{
	//			Version = 5;
	//			//::MessageBoxW(g_hwnd,L"5==Version",L"",0);
	//		}
	//		if(2==Version_min)
	//		{
	//			Version = 6;
	//			//::MessageBoxW(g_hwnd,L"6==Version",L"",0);
	//		}
	//	}
	//}

	//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n GPRSConnect start..." );

	// Enum outer CONNECTION
	if( UNKNOWN == nOuterConnection )
	{
		DWORD dwNumConnections=0;	// Number of connections found
		RASCONN RasConn[20]={0};	// Buffer for connection state data,Assume the maximum number of entries is 20.
		RasConn[0].dwSize = sizeof (RASCONN); // Assume no more than 20 connections.
		DWORD dwRasConnSize = 20 * sizeof (RASCONN);

		DWORD dwEnum = RasEnumConnections (RasConn, &dwRasConnSize,&dwNumConnections);

		if( 0 == dwEnum )// Find all connections.
		{
			if(dwNumConnections) // If there are no connections, return zero.
			{
				//::MessageBoxW(g_hwnd,L"there are connections",L"",0);
				if(g_pLoggerNet) g_pLoggerNet->Save( L"\n nOuterConnection:%02x [%s]", nOuterConnection, RasConn[0].szEntryName );

				if(NULL != zmwcsstr(RasConn[0].szEntryName,L"SB")) 
					nOuterConnection = USB;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"sb"))
					nOuterConnection = USB;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"DMA")) 
					nOuterConnection = USB;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"dma"))
					nOuterConnection = USB;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"FI"))
					nOuterConnection = WIFI;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"fi"))
					nOuterConnection = WIFI;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"互联"))
					nOuterConnection = CMNET;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"NET"))
					nOuterConnection = CMNET;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"net"))
					nOuterConnection = CMNET;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"梦网"))
					nOuterConnection = CMWAP;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"AP"))
					nOuterConnection = CMWAP;
				else if(NULL != zmwcsstr(RasConn[0].szEntryName,L"ap"))
					nOuterConnection = CMWAP;
				else
				{
					if(g_pLoggerNet) g_pLoggerNet->Save( L"\n UNKNOWN TYPE, CloseRasGPRSConnections [%s]", RasConn[0].szEntryName );
					BOOL bClose = CloseRasGPRSConnections();
				}
			}
			else
			{
				//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n RasEnumConnections dwNumConnections==0" );
			}
		}
		else
		{
			//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n RasEnumConnections false dwEnum:%d", dwEnum );
		}
	}

	// Enum NETWORK INTERFACE, search for USB or WIFI
	//if( UNKNOWN == nOuterConnection && 5 != Version )
	//{
	//	int8 * pBuffer = NULL;
	//	ULONG nFamily = AF_INET;
	//	DWORD nFlags = 0;
	//	PVOID pReserved = NULL;
	//	IP_ADAPTER_ADDRESSES * pAdapterAddresses = NULL;
	//	ULONG nOutBufLen = 0;

	//	DWORD dwGet = GetAdaptersAddresses( nFamily, nFlags, pReserved, pAdapterAddresses, &nOutBufLen );

	//	if( ERROR_NOT_ENOUGH_MEMORY == dwGet || ERROR_BUFFER_OVERFLOW == dwGet )
	//	{
	//		if( nOutBufLen > 0 )
	//		{
	//			pBuffer = new int8[nOutBufLen];	
	//			//::MessageBoxW(g_hwnd,L"GetAdaptersAddresses",L"",0);
	//			pAdapterAddresses = (IP_ADAPTER_ADDRESSES*)pBuffer;
	//			dwGet = GetAdaptersAddresses( nFamily, nFlags, pReserved, pAdapterAddresses, &nOutBufLen );
	//		}
	//	}


	//	if( ERROR_SUCCESS == dwGet)
	//	{
	//		IP_ADAPTER_ADDRESSES* pCurrent = pAdapterAddresses;
	//		while( pCurrent )
	//		{
	//			if( MIB_IF_TYPE_ETHERNET == pCurrent->IfType && IfOperStatusUp == pCurrent->OperStatus )
	//			{
	//				if(g_pLoggerNet) g_pLoggerNet->Save( L"\n find INTERFACE:MIB_IF_TYPE_ETHERNET [IfOperStatusUp]", pCurrent->IfType, pCurrent->OperStatus );
	//				nOuterConnection = USB;
	//				//::MessageBoxW(g_hwnd,L"nOuterConnection = USB;",L"",0);
	//				break;
	//			}
	//			pCurrent = pCurrent->Next;
	//		}
	//	}
	//	BUF_DELETE(pBuffer);
	//}

	if( USB == nOuterConnection || WIFI == nOuterConnection )
	{
		ZMGStatus::GetInst()->GetFenduIni()->usbConnMode = nOuterConnection;	//USB自动识别模式
		bRet = TRUE;
	}
	else
	{
		uint8 nUserConnMode = ZMGStatus::GetInst()->GetFenduIni()->userConnMode;
		if( UNKNOWN == nUserConnMode )
		{
			if( UNKNOWN == nOuterConnection )
			{
				ZMGStatus::GetInst()->GetFenduIni()->userConnMode = CMWAP;
				bRet = GPRSCmwapConnect( pnResultStatus );
				//::MessageBoxW(g_hwnd,L"GPRSCmwapConnect( pnResultStatus )",L"",0);
			}
			else
			{
				ZMGStatus::GetInst()->GetFenduIni()->userConnMode = nOuterConnection;
				bRet = TRUE;
				//::MessageBoxW(g_hwnd,L"ZMGStatus::GetInst()->GetFenduIni()->usbConnMode = nOuterConnection",L"",0);
			}
		}
		else
		{
			if( UNKNOWN == nOuterConnection )
			{
				if( CMNET == nUserConnMode )
				{
					//::MessageBoxW(g_hwnd,L"GPRSCmnetConnect",L"",0);
					bRet = GPRSCmnetConnect( pnResultStatus );
				}
				else if( CMWAP == nUserConnMode )
				{
					//::MessageBoxW(g_hwnd,L"GPRSCmwapConnect1",L"",0);
					bRet = GPRSCmwapConnect( pnResultStatus );
				}
				else
				{
					//::MessageBoxW(g_hwnd,L"GPRSCmwapConnect2",L"",0);
					bRet = GPRSCmwapConnect( pnResultStatus );
				}
			}
			else
			{
				if( nOuterConnection == nUserConnMode )
				{
					bRet = TRUE;
				}
				else
				{
					//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n nOuterConnection[%d] nUserConnMode[%d] try to CLOSE CONNECTION", nOuterConnection, nUserConnMode );
					DWORD dwClose = CloseRasGPRSConnections();
					//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n dwClose:%d", dwClose );
					if( dwClose > 0 )
					{
						if( CMNET == nUserConnMode )
						{
							//::MessageBoxW(g_hwnd,L"GPRSCmnetConnect2",L"",0);
							bRet = GPRSCmnetConnect( pnResultStatus );
						}
						else if( CMWAP == nUserConnMode )
						{
							//::MessageBoxW(g_hwnd,L"GPRSCmwapConnect21",L"",0);
							bRet = GPRSCmwapConnect( pnResultStatus );
						}
						else
						{
							//::MessageBoxW(g_hwnd,L"GPRSCmwapConnect21",L"",0);
							bRet = GPRSCmwapConnect( pnResultStatus );
						}
					}
				}
			}
		}
	}

JUMP_OUT:
	//{
		//::MessageBoxW(g_hwnd,L"JUMP_OUT Enum NETWORK",L"",0);
		//DWORD dwNumConnectionsEnd = 0;
		//RASCONN RasConn[20]={0}; // Buffer for connection state data,Assume the maximum number of entries is 20.
		//RasConn[0].dwSize = sizeof (RASCONN); // Assume no more than 20 connections.
		//DWORD dwRasConnSize = 20 * sizeof (RASCONN);

		//DWORD dwEnumEnd = RasEnumConnections (RasConn, &dwRasConnSize,&dwNumConnectionsEnd);
		//if(!dwEnumEnd)
		//{
		//	if(dwNumConnectionsEnd) // If there are no connections, return zero.
		//	{
		//		//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n RasEnumConnections End:%d [%s]", dwNumConnections, RasConn[0].szEntryName );
		//	}
		//}
		//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n GPRSConnect end... dwEnumEnd:%d dwNumConnectionsEnd:%d", dwEnumEnd, dwNumConnectionsEnd );
	//}

	return bRet;
}

/**********************************************************************************
*Function:IsGPRSConnect( void )
*Description:IsGPRSConnect
*Input:N/A
*Output:N/A
*Return:BOOL,TRUE-Success FALSE-FAILURE
*Others:None
***********************************************************************************/
BOOL IsGPRSConnect(void)
{
	BOOL nRet = FALSE;

	{
		//for mobile 2003
		DWORD dwNumConnections=0;
		RASCONN RasConn[20]={0};
		RasConn[0].dwSize = sizeof (RASCONN);
		DWORD dwRasConnSize = 20 * sizeof (RASCONN);

		DWORD dwEnum = RasEnumConnections (RasConn, &dwRasConnSize,&dwNumConnections);

		if( 0 == dwEnum )	//Find all connections.
		{
			if(dwNumConnections) //If there are no connections, return zero.
			{
				nRet = TRUE;	
			}
			else
			{
				//::MessageBoxW(g_hwnd,L"no connections",L"",0);
			}
		}
	}

	if(!nRet)
	{
		//for mobile 5,6
		int8 * pBuffer = NULL;
		ULONG nFamily = AF_INET;
		DWORD nFlags = 0;
		PVOID pReserved = NULL;
		IP_ADAPTER_ADDRESSES * pAdapterAddresses = NULL;
		ULONG nOutBufLen = 0;

		DWORD dwGet = GetAdaptersAddresses( nFamily, nFlags, pReserved, pAdapterAddresses, &nOutBufLen );

		if( ERROR_NOT_ENOUGH_MEMORY == dwGet || ERROR_BUFFER_OVERFLOW == dwGet )
		{
			if( nOutBufLen > 0 )
			{
				pBuffer = new int8[nOutBufLen];//::MessageBoxW(g_hwnd,L"nOutBufLen > 0",L"",0);
				pAdapterAddresses = (IP_ADAPTER_ADDRESSES*)pBuffer;
				dwGet = GetAdaptersAddresses( nFamily, nFlags, pReserved, pAdapterAddresses, &nOutBufLen );
			}
		}

		if( ERROR_SUCCESS == dwGet )
		{
			IP_ADAPTER_ADDRESSES* pCurrent = pAdapterAddresses;
			while( pCurrent )
			{
				if( MIB_IF_TYPE_ETHERNET == pCurrent->IfType && IfOperStatusUp == pCurrent->OperStatus )
				{
					nRet = TRUE;//::MessageBoxW(g_hwnd,L" GPRS connections",L"",0);
					break;
				}
				pCurrent = pCurrent->Next;
			}
			//::MessageBoxW(g_hwnd,L" GPRS not connections ",L"",0);
		}
		BUF_DELETE(pBuffer);
	}

	return nRet;
}

/**********************************************************************************
*Function:GPRSCmnetConnect( pnResultStatus )
*Description:Create CMNET GPRS connection
*Input:N/A
*Output:N/A
*Return:BOOL,TRUE-Success FALSE-FAILURE
*Others:None
***********************************************************************************/


BOOL GPRSCmnetConnect( uint32* pnResultStatus )
{
	BOOL bRet = FALSE;

	if(g_pLoggerNet) g_pLoggerNet->Save( L"\n GPRSCmnetConnect start..." );

	HANDLE hConnection = NULL; 

	DWORD   dwStatus = CONNMGR_STATUS_UNKNOWN; 
	CONNMGR_CONNECTIONINFO sCI = { 0 }; 
	
	sCI.cbSize           = sizeof(sCI); 
	sCI.bExclusive		 = TRUE; 
	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; 

	HRESULT hrConnect = ConnMgrEstablishConnection( &sCI, &hConnection );
	//ConnMgrReleaseConnection(hConnection,0);
	//::MessageBoxW(g_hwnd,L"GPRSCmnetConnect",L"",0);
	//hrConnect = ConnMgrEstablishConnection( &sCI, &hConnection );
	
	//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n ConnMgrEstablishConnection hrConnect:%08x hConnection:%08x", hrConnect, hConnection );
	if( hConnection && S_OK == hrConnect )
	{
		unsigned int nOld = GetTickCount();
		unsigned int nNow = GetTickCount();
		while( (nNow - nOld) < 30000 )
		{
			HRESULT hrStatus = ConnMgrConnectionStatus( hConnection, &dwStatus );
			//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n ConnMgrConnectionStatus hConnection:%08x hrStatus:%08x dwStatus:%08x", hConnection, hrStatus, dwStatus );

			if( S_OK == hrStatus )
			{
				if( CONNMGR_STATUS_CONNECTED & dwStatus )
				{
					bRet = TRUE;
					break;
				}
				else if( (CONNMGR_STATUS_WAITINGCONNECTION & dwStatus) || (CONNMGR_STATUS_UNKNOWN == dwStatus) )
				{
					::Sleep(200);
				}
				else if( ( CONNMGR_STATUS_DISCONNECTED & dwStatus ) || ( CONNMGR_STATUS_WAITINGDISCONNECTION & dwStatus ) )
				{
					break;
				}
				else
				{
					::Sleep(200);
				}
			}
			else
			{
				break;
			}
			nNow = GetTickCount();
		}
	}
	//HRESULT hrEstablish = ConnMgrEstablishConnectionSync(&sCI, &hConnection, 30000, &dwStatus);
	//if(FAILED(hrEstablish)) 
	//{ 
	//	if( CONNMGR_STATUS_WAITINGCONNECTION == dwStatus )
	//	{
	//		//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n NET TimeOut, try again hrEstablish:%08x dwStatus:%02x", hrEstablish, dwStatus );
	//		hrEstablish = ConnMgrEstablishConnectionSync(&sCI, &hConnection, 20000, &dwStatus);
	//		//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n Try again End hrEstablish:%08x dwStatus:%02x", hrEstablish, dwStatus );
	//		if( S_OK == hrEstablish )
	//		{
	//			bRet = TRUE;
	//		}
	//	}
	//}
	//else
	//{
	//	bRet = TRUE;
	//}

	if( pnResultStatus )
	{
		*pnResultStatus = dwStatus;
	}
	if(g_pLoggerNet) g_pLoggerNet->Save( L"\n GPRSCmnetConnect end dwStatus:%02x", dwStatus );
	//if(bRet)
	//::MessageBoxW(g_hwnd,L"GPRSCmnetConnect OK",L"",0);
	return bRet;
}

/**********************************************************************************
*Function:GPRSCmwapConnect( pnResultStatus )
*Description:Create CMWAP GPRS connection
*Input:N/A
*Output:N/A
*Return:BOOL,TRUE-Success FALSE-FAILURE
*Others:None
***********************************************************************************/
/*
BOOL GPRSCmwapConnect(uint32* pnResultStatus )
{
BOOL bRet = FALSE;

unsigned int nBeginMode = ZMGStatus::GetInst()->GetFenduIni()->usbConnMode;
//if(g_pLoggerNet) g_pLoggerNet->Save( L"\n GPRSCmwapConnect2 Begin mode:%d", nBeginMode );

HANDLE hConnection = NULL; 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -