Chris Muench
Windows Mobile MVP
November 2003
Applies to:
Microsoft? eMbedded Visual C++? 3.0
Pocket PC 2002
Pocket PC 2002 Phone Edition
Smartphone 2002
Summary: It is now possible to initiate data calls on Windows Mobile-based devices using Connection Manager. This article will show you what steps are necessary to initiate a data call. (6 printed pages)
Contents
Background Information
Some Sample Code
Debugging the Code
Conclusion
Background Information
Microsoft? introduced Connection Manager with Pocket PC in early 2002. The idea of Connection Manager is to abstract all outgoing calls to an application by providing an API that handles all the lower level "plumbing."
Before an application can make a connection via Connection Manager, it has to be configured. Most Pocket PCs and Smartphones bought from a telecommunication provider such as Orange or T-Mobile are pre-configured to work with their data infrastructure, such as GPRS or HSCSD.
You can verify if Connection Manager is configured correctly simply by opening any Internet site in Pocket Internet Explorer. If your device is not in a cradle, the device should automatically open the Internet connection configured in Connection Manager. If your device isn't pre-configured, you'll need to create a connection in connection manager. Please refer to your device manual and your service provider.
Pocket Internet Explorer internally uses Connection Manager to establish the connection to the Internet. If you have the device in a cradle and use ActiveSync 3.5 or higher, the connection will be automatically established via ActiveSync.
Some Sample Code
The following code will try to determine the current IP address of your device. You can just create a new simple Pocket PC project and paste this code into your main CPP file. If you are connected via the cradle, the IP address is already valid and we do not need to call out via the Connection Manager. If the IP is empty, we will initiate the connection manger to try to get to the Internet. Let's look at the code line by line:
#include "stdafx.h" #include "winsock.h" ///The following Defines are only on Smartphone and Pocket PC Phone Edition #if (WIN32_PLATFORM_PSPC>300 || WIN32_PLATFORM_WFSP ) #include#include #include //This Include contains all defines for the //connection manager typedef HRESULT (*CONNMGRCONNECTIONSTATUS) (HANDLE hConnection,DWORD *pdwStatus); typedef HRESULT (*CONNMGRRELEASECONNECTION) (HANDLE hConnection,LONG lCache ); typedef HRESULT (*CONNMGRESTABLISHCONNECTION) (CONNMGR_CONNECTIONINFO *pConnInfo, HANDLE *phConnection,DWORD dwTimeout, DWORD *pdwStatus); #endif BOOL EstablishConnection(TCHAR *IPout) { HANDLE phWebConnection; CHAR szHostname[255]; TCHAR IP[17]; HOSTENT *pHostEnt=NULL; int nAdapter = 0; IN_ADDR *tsim=NULL; BOOL tried2Connect=FALSE; IP[0]=0; // Clear the IP Address if (IPout!=NULL) IPout[0]=0; tryagain: nAdapter=0; gethostname( szHostname, sizeof( szHostname )); pHostEnt = gethostbyname( szHostname ); while ( pHostEnt!=NULL && pHostEnt->h_addr_list[nAdapter] ) { // in case a device has multiple ethernet cards // i.e. 802.11, Bluetooth, USB-Cradle // we need to go though all pHostEnt->h_addr_list[nAdapter] tsim=(IN_ADDR *)pHostEnt->h_addr_list[nAdapter]; if (tsim->S_un.S_un_b.s_b1==192 || tsim->S_un.S_un_b.s_b1==169 || tsim->S_un.S_un_b.s_b1==127 || tsim->S_un.S_un_b.s_b1==255) { // If you want to make sure you have a real Internet // connection you cannot bet on IpAddresses starting with // 127 and 255. 192 and 169 are local IP addresses and // might be routed or proxied nAdapter++; } else { wsprintf(IP,TEXT("%d.%d.%d.%d"), tsim->S_un.S_un_b.s_b1, tsim->S_un.S_un_b.s_b2, tsim->S_un.S_un_b.s_b3, tsim->S_un.S_un_b.s_b4); if (IPout!=NULL) wsprintf(IPout,IP); //Return the IP address break; } } // the next lines only work with Pocket PC Phone // and Smartphone #if (WIN32_PLATFORM_PSPC>300 || WIN32_PLATFORM_WFSP ) // Pocket PC Phone Edition has set WIN32_PLATFORM_PSPC to 310 if (IP[0]==0 && tried2Connect==FALSE) { CONNMGRCONNECTIONSTATUS g_hConnMgrConnectionStatus = NULL; CONNMGRESTABLISHCONNECTION g_hConnMgrEstablishConnectionSync=NULL; // It is good practice to load the cellcore.dll // dynamically to be able to compile the code even for // older platforms HINSTANCE hcellDll = LoadLibrary(TEXT("cellcore.dll")); if (hcellDll) { // We need the Status and a call to establish the // connection g_hConnMgrConnectionStatus = (CONNMGRCONNECTIONSTATUS)GetProcAddress( hcellDll,TEXT("ConnMgrConnectionStatus")); // The next line is just for debugging. You will have // to decide what you want to do if this call fails DWORD a=GetLastError(); g_hConnMgrEstablishConnectionSync = (CONNMGRESTABLISHCONNECTION)GetProcAddress( hcellDll,TEXT("ConnMgrEstablishConnectionSync")); a=GetLastError(); // Here comes the main code: // First we check if we might have a connection DWORD pdwStatus; (* g_hConnMgrConnectionStatus) (&phWebConnection,&pdwStatus); if (pdwStatus==CONNMGR_STATUS_CONNECTED) //We are already connected! //This code should never run since we should //have a valid IP already. //If you still get here, you probably have //stale connection. else { //We are not connected, so lets try: //The CONNECTIONINFO is the structure that //tells Connection Manager how we want //to connect CONNMGR_CONNECTIONINFO sConInfo; memset(&sConInfo,0, sizeof(CONNMGR_CONNECTIONINFO)); sConInfo.cbSize=sizeof(CONNMGR_CONNECTIONINFO); // We want to use the "guidDestNet" parameter sConInfo.dwParams=CONNMGR_PARAM_GUIDDESTNET; // This is the highest data priority. sConInfo.dwPriority= CONNMGR_PRIORITY_USERINTERACTIVE; sConInfo.dwFlags=0; // Lets be nice and share the connection with // other applications sConInfo.bExclusive=FALSE; sConInfo.bDisabled=FALSE; sConInfo.guidDestNet=IID_DestNetInternet; // We want to wait until the connection was // established successful but not longer then // 60 seconds. You can use // ConnMgrEstablishConnection to establish // an asynchronous connection. if ((*g_hConnMgrEstablishConnectionSync)( &sConInfo,&phWebConnection,60000,&pdwStatus) ==S_OK) { //We are successfully connected! //Now lets try to get the new IP address: tried2Connect=TRUE; goto tryagain; } else { tried2Connect=FALSE; //Doh! Connection failed! } } } } #endif return tried2Connect; }
Debugging the Code
If you try to debug this code while connected to your device via ActiveSync, you will find that you cannot establish a connection. The Connection Manager already has an open connection (ActiveSync) and, therefore, does not allow another connection to be established. Similarly, if you are using the emulator image, it will not establish a connection because the emulator has an Ethernet connection open by default.
Conclusion
Connection Manager makes it very simple to establish data calls. In the code above, you can see that the application at no point defines what media it wants to use. You can have CPDP, TDMA, GSM, dial-up modems or GPRS configured and the code above will work!
'Windows > MFC' 카테고리의 다른 글
Establishing Network Connectivity with the Windows Mobile Connection Manager (0) | 2013.10.02 |
---|---|
Cellular Emulator (0) | 2013.10.02 |
GPRS Conection via Proxy (0) | 2013.10.02 |
GPRS connection (0) | 2013.10.02 |
Cellular Emulator사용하기 (0) | 2013.10.02 |