判断GPRS是否连接的代码

 1.判断gprs是否连接,如果连接则跳过。

 2.如果没连接则连接gprs.

用到的类库 connmgr.h

#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 <objbase.h>
#include <initguid.h>
#include <connmgr.h>   //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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值