www.pudn.com > GPRS.rar > mainwindow.cpp, change:2010-10-27,size:2197b


#include "mainwindow.h" 
#include "ui_mainwindow.h" 
 
#include <QMessageBox> 
#include <Connmgr.h> 
#pragma comment ( lib, "Cellcore.lib" ) 
 
MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(open())); 
    connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(close())); 
} 
 
MainWindow::~MainWindow() 
{ 
    delete ui; 
} 
bool MainWindow::open() 
{ 
    ReleaseConnection (); 
    CONNMGR_DESTINATION_INFO DestInfo = {0}; 
    HRESULT hResult = ConnMgrEnumDestinations(0, &DestInfo); 
                            //函数遍历出来的那些默认单位设置,默认internet设置 
    BOOL bRet = FALSE; 
    if(SUCCEEDED(hResult)) 
    { 
 
         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 | CONNMGR_FLAG_PROXY_SOCKS5;  //选择网络 
         ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;   //连接请求的优先级 
         ConnInfo.guidDestNet = DestInfo.guid;    //连接的网络GUID。 
         ConnInfo.bExclusive	= FALSE;   //是否独占连接请求,非0值代表连接不能被其他程序共享 
         ConnInfo.bDisabled = FALSE;  //指定连接是否断开。 
 
         DWORD dwStatus = 0; 
         hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );  //同步连接请求  连接GPRS 
                    //为指定的网络标识选择和建立合适的连接。该方法直到尝试连接完成了才返回。 
         if(FAILED(hResult)) 
         { 
             m_hConnection = NULL;   //失败 
         } 
         else 
             bRet = TRUE;  //成功 
     } 
     if(bRet) 
         QMessageBox::warning(this,tr("GPRS"),tr("网络连接成功!")); 
     return bRet; 
} 
void MainWindow::ReleaseConnection() 
{ 
    if ( m_hConnection ) 
    { 
         ConnMgrReleaseConnection(m_hConnection, FALSE);   //释放指定连接,可能会关闭连接(并不保证一定关闭)。 
         m_hConnection = NULL; 
    } 
}