好多人都在網上找PPC的撥接程式,我來發個自己的寫的代碼給大家分享下

來源:互聯網
上載者:User

       這個程式裡有是否檢測網路已斷開,然後重連,大家可以根據自己的情況進行改變,一切為了分享,希望大家也都把這個精神發揚下去,在WM5.0、WM6.0  基於MFC的智慧型裝置工程,測試通過  CDMA網路

*******************************************************************

//ConnectManager.h

 

#pragma once

 

class CConnectManager

{

 

public:

CConnectManager(void);

virtual ~CConnectManager(void);

void EnumNetIdentifier ( OUT CStringArray &StrAry );

BOOL GetConnMgrAvailable();

int MapURLAndGUID ( LPCTSTR lpszURL, GUID &guidNetworkObject, OUT CString *pcsDesc=NULL );

BOOL EstablishConnection ( DWORD dwIndex );

BOOL EstablishConnection ( GUID guid );//liu 4-29修改

BOOL WaitForConnected ( int nTimeoutSec, DWORD *pdwStatus=NULL );

private:

void ReleaseConnection ();

HANDLE m_hConnection;

};

//ConnectManager.cpp

 

#include "StdAfx.h"

#include "ConnectManager.h"

//#include "StdAfx.h"

//#include "ConnectManager.h"

// for connect manager function

 

#include <Connmgr.h>

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

 

CConnectManager::CConnectManager(void)

{

}

 

CConnectManager::~CConnectManager(void)

{

ReleaseConnection ();

}

 

BOOL CConnectManager::GetConnMgrAvailable()

{

HANDLE hConnMgr = ConnMgrApiReadyEvent ();

BOOL bAvailbale = FALSE;

DWORD dwResult = ::WaitForSingleObject ( hConnMgr, 2000 );

if ( dwResult == WAIT_OBJECT_0 )

{

bAvailbale = TRUE;

}

 

// 關閉

if ( hConnMgr ) CloseHandle ( hConnMgr );

 

return bAvailbale;

}

 

int CConnectManager::MapURLAndGUID ( LPCTSTR lpszURL, OUT GUID &guidNetworkObject, OUT CString *pcsDesc/*=NULL*/ )

{

if ( !lpszURL || lstrlen(lpszURL) < 1 )

return FALSE;

 

memset ( &guidNetworkObject, 0, sizeof(GUID) );

int nIndex = 0;

HRESULT hResult = ConnMgrMapURL ( lpszURL, &guidNetworkObject, (DWORD*)&nIndex );

if ( FAILED(hResult) )

{

nIndex = -1;

DWORD dwLastError = GetLastError ();

AfxMessageBox ( _T("Could not map a request to a network identifier") );

}

else

{

if ( pcsDesc )

{

CONNMGR_DESTINATION_INFO DestInfo = {0};

if ( SUCCEEDED(ConnMgrEnumDestinations(nIndex, &DestInfo)) )

{

*pcsDesc = DestInfo.szDescription;

}

}

}

 

return nIndex;

}

 

void CConnectManager::EnumNetIdentifier ( OUT CStringArray &StrAry )

{

CONNMGR_DESTINATION_INFO networkDestInfo = {0};

 

// 得到網路列表

for ( DWORD dwEnumIndex=0; ; dwEnumIndex++ )

{

memset ( &networkDestInfo, 0, sizeof(CONNMGR_DESTINATION_INFO) );

if ( ConnMgrEnumDestinations ( dwEnumIndex, &networkDestInfo ) == E_FAIL )

{

break;

}

StrAry.Add ( networkDestInfo.szDescription );

//AfxMessageBox (dwEnumIndex);

CString tt;

tt.Format(L"%u",dwEnumIndex);

}

}

//指定撥號連線

BOOL CConnectManager::EstablishConnection ( DWORD dwIndex )

{

// 釋放之前的串連

ReleaseConnection ();

 

// 得到正確的串連資訊

CONNMGR_DESTINATION_INFO DestInfo = {0};

HRESULT hResult = ConnMgrEnumDestinations(dwIndex, &DestInfo);

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;//參數dwFlags用於指定我們的存取點,比如我們常說的CMNER和CMWAP

ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;

ConnInfo.guidDestNet = DestInfo.guid;

ConnInfo.bExclusive= FALSE; 

ConnInfo.bDisabled = FALSE;

DWORD dwStatus = 0;

hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );

if(FAILED(hResult))

{

m_hConnection = NULL;

}

else bRet = TRUE;

}

 

return bRet;

}

//指定撥號連線liu 4-29 添加

BOOL CConnectManager::EstablishConnection ( GUID guid )

{

// 釋放之前的串連

ReleaseConnection ();

 

// 得到正確的串連資訊

HRESULT hResult ;

BOOL bRet = FALSE;

// 初始化串連結構

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 | CONNMGR_FLAG_SUSPEND_AWARE;//參數dwFlags用於指定我們的存取點,比如我們常說的CMNER和CMWAP

ConnInfo.dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;

ConnInfo.guidDestNet = guid;

ConnInfo.bExclusive= FALSE; 

ConnInfo.bDisabled = FALSE;

DWORD dwStatus = 0;

hResult = ConnMgrEstablishConnectionSync(&ConnInfo, &m_hConnection, 10*1000, &dwStatus );

if(FAILED(hResult))

{

m_hConnection = NULL;

}

else 

bRet = TRUE;

 

return bRet;

}

BOOL CConnectManager::WaitForConnected ( int nTimeoutSec, DWORD *pdwStatus/*=NULL*/ )

{

DWORD dwStartTime = GetTickCount ();//擷取開機以來的毫秒數

BOOL bRet = FALSE;

while ( GetTickCount ()-dwStartTime < (DWORD)nTimeoutSec * 1000 )//定時為2秒間隔

{

if ( m_hConnection )

{

DWORD dwStatus = 0;

HRESULT hr = ConnMgrConnectionStatus ( m_hConnection, &dwStatus );//利用此函數的傳回值dwStatus,我們可以得到很多的關於串連的資訊,例如如果我們串連成功將返回CONNMGR_STATUS_CONNECTED,中斷連線將返回CONNMGR_STATUS_DISCONNECTED,他的返回狀態資訊非常豐富

if ( pdwStatus ) *pdwStatus = dwStatus;

if ( SUCCEEDED(hr) )

{

if ( dwStatus == CONNMGR_STATUS_CONNECTED )

{

bRet = TRUE;

break;

}

}

}

Sleep ( 100 );

}

 

return bRet;

}

 

void CConnectManager::ReleaseConnection ()

{

if ( m_hConnection )

{

ConnMgrReleaseConnection(m_hConnection, FALSE);

m_hConnection = NULL;

}

}

*********************************************************************************

測試案例:

 

BOOL YourClass::ConnectManagerTest(void)

{

BOOL bRet=false;

DWORD dwStatus = 0;

CString url;

CString strDialup;

GUID guid;

 

CArray<CString, CString> ss;//第一個參數就是CArray類數組元素的變數類型,後一個是函數調用時的參數類型

if (!m_ConnectManager.WaitForConnected ( 2, &dwStatus ) )

{

m_ConnectManager.MapURLAndGUID(url,guid);//URL是指定的一個公網地址URL,隨便哪個都可以的,如www.baidu.com

if ( m_ConnectManager.EstablishConnection (guid)  )

{

bRet=true;

}

else

{

bRet=false;

}

}

else

bRet=true;

return bRet;

}

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.