日期:2014-05-17 浏览次数:21238 次
#ifndef _WIN32_WCE
LPCTSTR CVPN::m_sszVPNDeviceNamePPTP = _T("WAN 微型端口 (PPTP)");
LPCTSTR CVPN::m_sszVPNDeviceNameL2TP = _T("WAN 微型端口 (L2TP)");
#else
LPCTSTR CVPN::m_sszVPNDeviceNamePPTP = _T("RAS VPN Line 2");
#endif //_WIN32_WCE
// 记录vpn连接句柄
if (m_hRasVPNConn)
{
RasHangUp(m_hRasVPNConn);
m_hRasVPNConn = NULL;
}
// 初始化输出参数
strVPNLocalIPAddr.Empty();
RASDEVINFO rasdevinfo[100];
ZeroMemory(&rasdevinfo[0], sizeof(RASDEVINFO) * 100);
rasdevinfo[0].dwSize = sizeof(RASDEVINFO);
DWORD dwrasdevinfosize = sizeof(RASDEVINFO) * 100;
DWORD dwrasdevcount = 0;
RasEnumDevices(&rasdevinfo[0], &dwrasdevinfosize, &dwrasdevcount);
CString strallrasdevinfo;
for (int n = 0; n < dwrasdevcount; n++)
{
CString strrasdevinfo;
strrasdevinfo.Format(_T("devicetype:%s; devicename:%s"),
rasdevinfo[n].szDeviceType, rasdevinfo[n].szDeviceName);
strallrasdevinfo += strrasdevinfo + _T("\n");
}
// 删除旧的vpn连接
TCHAR szEntryName[RAS_MaxEntryName + 1];
ZeroMemory(szEntryName, RAS_MaxEntryName + 1);
_tcscpy(szEntryName, g_LangCfg.GetVpnEntryName());
RasDeleteEntry(NULL, szEntryName);
// 创建vpn连接
RASENTRY rasentry;
ZeroMemory(&rasentry, sizeof(RASENTRY));
rasentry.dwSize = sizeof(RASENTRY);
rasentry.dwfOptions |= RASEO_RemoteDefaultGateway
| RASEO_IpHeaderCompression
| RASEO_RequireEncryptedPw | RASEO_RequireDataEncryption | RASEO_RequireMsEncryptedPw
| RASEO_SwCompression;
_tcscpy(rasentry.szLocalPhoneNumber, _T("192.168.1.254"));//_T("124.227.190.253"));//g_LangCfg.GetVpnServerAddr());
rasentry.dwfNetProtocols = RASNP_Ip;
rasentry.dwFramingProtocol = RASFP_Ppp;
_tcscpy(rasentry.szDeviceType, RASDT_Vpn);
_tcscpy(rasentry.szDeviceName, m_sszVPNDeviceNamePPTP);
#ifndef _WIN32_WCE
rasentry.dwDialMode = RASEDM_DialAll;
rasentry.dwType = RASET_Vpn;
#endif
TCHAR szrasentryname[RAS_MaxEntryName + 1];
_tcscpy(szrasentryname, g_LangCfg.GetVpnEntryName());
DWORD dwResult =
RasSetEntryProperties(NULL, szrasentryname, &rasentry, sizeof(RASENTRY), NULL, 0);
if (0 != dwResult)
{
RM_LOG(LOG_ERROR, _T("创建vpn失败!"));
return FALSE;
}
// 拨号
RASDIALPARAMS dialParams;
ZeroMemory(&dialParams, sizeof(RASDIALPARAMS));
dialParams.dwSize = sizeof(RASDIALPARAMS);
_tcscpy(dialParams.szEntryName, g_LangCfg.GetVpnEntryName());
_tcscpy(dialParams.szUserName, _T("hwj"));//g_LangCfg.GetVpnUserName());
_tcscpy(dialParams.szPassword, _T("stanley"));//g_LangCfg.GetVpnPassword());
dwResult = RasDial(NULL, NULL, &dialParams, 0, NULL, &m_hRasVPNConn);
if (0 != dwResult)
{
RM_LOG(LOG_ERROR, _T("vpn拨号失败!"));
return FALSE;
}
// 获取vpn本地ip地址
RASPPPIP rasPPPIP;
ZeroMemory(&rasPPPIP, si