baidu

[WM]n久以前写的ConnMgr类

说明: 星期天回去看了一下,能编译通过了(汗).

 

头文件: ConnectionMgr.h

 

#pragma once

#include <vector>
#include <windows.h>
#include <InitGuid.h>
#include <connmgr.h>
#pragma comment( lib, "cellcore.lib" )
#include <connmgr_status.h>

class ConnectionMgr
{
public:
	typedef CONNMGR_CONNECTION_DETAILED_STATUS STATUS;
	ConnectionMgr(void);
	~ConnectionMgr();
	 
	bool EstablishConnection(int index,bool isSync);
	bool EstablishConnection(const GUID guid,bool isSync);
	bool EstablishConnection(wchar_t* entryPointName,bool isSync);
	bool QueryConnStatus();
	void ReleaseConnection();
	void ReleaseConnection(int bCache);
	void SetCallbackParams(HWND hWnd,unsigned int uMsg);

	//#include <connmgr_status.h>
	//http://www.17feixiang.com/article/jishu/83.htm
	STATUS QueryStatus(); 
	void RegisterEvent(HWND hwnd,bool isEnable);
	GUID MapUrl(const wchar_t* wcsURL);

	const std::vector<CONNMGR_DESTINATION_INFO>* GetNetIdentifiersList();
	HANDLE GetHConnection() const;
private:
	void Init();
	std::vector<CONNMGR_DESTINATION_INFO> lstNetIdentifiers;
	HANDLE			hConnection;
	HWND			hWnd;
	unsigned int	uMsg;
};

实现: ConnectionMgr.cpp

 

 

#include "ConnectionMgr.h"
#include <stdlib.h>

ConnectionMgr::ConnectionMgr(void):
	hConnection(NULL),hWnd(NULL),uMsg(0)
{
	this->Init();
}

ConnectionMgr::~ConnectionMgr(void)
{
	if(this->hConnection)
	{
		this->ReleaseConnection();
	}
}

bool ConnectionMgr::EstablishConnection(int index,bool isSync)
{
	CONNMGR_DESTINATION_INFO DestInfo;
	HRESULT result= ConnMgrEnumDestinations(index,&DestInfo);
	if(result>=0)
	{		
		return this->EstablishConnection(DestInfo.guid,isSync);
	}
	return false;
}

bool ConnectionMgr::EstablishConnection(const GUID guid,bool isSync)
{
	CONNMGR_CONNECTIONINFO connInfo={0};

	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.bExclusive = false;
	connInfo.bDisabled = false;
	connInfo.guidDestNet = guid;//IID_DestNetInternet;
	connInfo.uMsg=this->uMsg;
	connInfo.hWnd = this->hWnd;

	HRESULT result;
	HANDLE _hConnection=NULL;
	if(isSync)
	{
		DWORD dwStatus;
		result=ConnMgrEstablishConnectionSync(&connInfo,&_hConnection,10*1000,&dwStatus);
		this->hConnection=_hConnection;
		//成功
#if DEBUG
		wchar_t str[100];
		memset(str,0,sizeof(wchar_t)*100);
		wsprintf(str,L"ResultCode:%d\nhConnection:%d",dwStatus,this->hConnection);
		MessageBox(NULL,str,L"拨号结果",MB_OK);
#endif
		if(CONNMGR_STATUS_CONNECTED==dwStatus)
			return true;
	}
	else
	{
		result=ConnMgrEstablishConnection(&connInfo,&_hConnection);
		this->hConnection=_hConnection;
		//这个只是调用结果,并非真实情况
#if DEBUG
		wchar_t str[100];
		memset(str,0,sizeof(wchar_t)*100);
		wsprintf(str,L"ResultCode:%s\nhConnection:%d",L"NULL",this->hConnection);
		MessageBox(NULL,str,L"拨号结果",MB_OK);
#endif
		if(S_OK==result)
			return true;
	}
	
	return false;
}

bool ConnectionMgr::EstablishConnection(wchar_t* entryPointName,bool isSync)
{
	int index = 0;
	for (unsigned int i = 0;i < this->lstNetIdentifiers.size();i++)
	{
		if(0==wcscmp(this->lstNetIdentifiers[i].szDescription,entryPointName))
		{
			index=i;
			break;
		}
	}

	return this->EstablishConnection(index,isSync);
}

bool ConnectionMgr::QueryConnStatus()
{
	DWORD dwStatus;

	HRESULT result = ConnMgrConnectionStatus(this->hConnection,&dwStatus);
	if(S_OK==result && CONNMGR_STATUS_CONNECTED==dwStatus)
		return true;
	return false;
}
void ConnectionMgr::RegisterEvent(HWND hwnd,bool isEnable)
{
	ConnMgrRegisterForStatusChangeNotification(isEnable,hwnd);
}

void ConnectionMgr::ReleaseConnection()
{
	this->ReleaseConnection(2);
}
GUID ConnectionMgr::MapUrl(const wchar_t* wcsURL)
{
	GUID _guid={0};
	HRESULT result=ConnMgrMapURL(wcsURL,&_guid,0);

	return _guid;
	//..
}

ConnectionMgr::STATUS ConnectionMgr::QueryStatus()
{
	STATUS status;
	STATUS* _status;
	DWORD dwSize=0;
	
	ConnMgrQueryDetailedStatus(NULL,&dwSize);
	_status=(STATUS*)new byte[dwSize];
	ConnMgrQueryDetailedStatus(_status,&dwSize);
	status=*_status;
	delete[] _status;

	return status;
}

void ConnectionMgr::ReleaseConnection(int bCache)
{
#if DEBUG
	wchar_t message[100];
	memset(message,0,sizeof(wchar_t)*100);
	wsprintf(message,L"hConnection:%d",this->hConnection);
	MessageBox(NULL,message,L"连接句柄",MB_OK);
#endif

	if(this->hConnection)
	{
		ConnMgrReleaseConnection(this->hConnection,bCache);
		CloseHandle(this->hConnection);
		this->hConnection=NULL;
	}
}

void ConnectionMgr::Init()
{
	//填充list
	lstNetIdentifiers.clear();

	for (int dwEnumIndex=0; ;++dwEnumIndex)
	{
		CONNMGR_DESTINATION_INFO tmpInfo;
		if(ConnMgrEnumDestinations(dwEnumIndex,&tmpInfo))
			break;
		lstNetIdentifiers.push_back(tmpInfo);
	}
}

void ConnectionMgr::SetCallbackParams(HWND hWnd,unsigned int uMsg)
{
	this->hWnd=hWnd;
	this->uMsg=uMsg;
}

const std::vector<CONNMGR_DESTINATION_INFO>* ConnectionMgr::GetNetIdentifiersList() 
{
	return &(this->lstNetIdentifiers);
}

HANDLE ConnectionMgr::GetHConnection() const
{
	return this->hConnection;
}

另外还有C API,供C# P/Invoke:

 

ConnectionMgr_C.h

 

#pragma once
#define EXPORT __declspec(dllexport)
#include "ConnectionMgr.h"
#include <string>

static ConnectionMgr g_Mgr;
static std::vector<wchar_t*> lst;

extern "C"{
	EXPORT void  SetFeedbackParams(HWND hWnd,unsigned int uMsg)
	{
		g_Mgr.SetCallbackParams(hWnd,uMsg);
	}

	EXPORT void GetMappingGUID(const wchar_t* wcsURL,GUID* pGuid)
	{
		*pGuid = g_Mgr.MapUrl(wcsURL);
	}

	EXPORT bool EstablishDefaultNET(bool isSync)
	{
		return g_Mgr.EstablishConnection(IID_DestNetInternet,isSync);
	}
	EXPORT bool EstablishDefaultWAP(bool isSync)
	{
		return g_Mgr.EstablishConnection(IID_DestNetWAP,isSync);
	}

	EXPORT bool EstablishUseName(wchar_t* entryPointName,bool isSync)
	{
		return g_Mgr.EstablishConnection(entryPointName,isSync);
	}

	EXPORT bool EstablishUseIndex(int index,bool isSync)
	{
		return g_Mgr.EstablishConnection(index,isSync);
	}

	EXPORT bool EstablishUseGUID(GUID guid,bool isSync)
	{
		return g_Mgr.EstablishConnection(guid,isSync);
	}

	EXPORT size_t GetNetIndentifiersSize()
	{
		return g_Mgr.GetNetIdentifiersList()->size();
	}
	EXPORT const wchar_t* GetIndentifierDescription(size_t index)
	{
		const wchar_t* result=NULL;
		result= g_Mgr.GetNetIdentifiersList()->operator [](index).szDescription;
		return result;
	}

	EXPORT void ReleaseIdentifierList(wchar_t** pStrings)
	{
		delete[] pStrings;
	}

	EXPORT void ReleaseCurrentConnection()
	{
		g_Mgr.ReleaseConnection();
	}
};

PS:

 

ReleaseConnection能否正常断网看你的机器对ConnMgr实现的标准程度,在WM 5中,ReleaseConnection的参数是bCache,只有两种状态;在WM 6中,那个参数换成了lCache,Document里面说lCache>1,那么缓存lCache秒之后断网,但是好像很多机器都不是那么实现.如果不能工作的话,就需要用pknife的RAS办法断网了.....

posted @ 2010-04-06 11:40  egmkang  阅读(1583)  评论(0编辑  收藏  举报