自动连接VPN

来源:互联网 发布:mazak数控车床编程 编辑:程序博客网 时间:2024/06/07 16:14

工作需要,要弄一个自动登录VPN的小程序,网上找了一些方法,这里记录一下。
参数说明:
wzIP VPN服务器的IP
wzEntryName 客户端指定的一个目标名,用户自己随意指定
wzUserName  VPN登录用户名
wzPassWord  VPN登录密码
DWORD ConnectVPN(const wchar_t *wzIP,const wchar_t *wzEntryName,const wchar_t *wzUserName,const wchar_t *wzPassWord){DWORD dwRet = NO_ERROR;RASENTRYW theRasEntry = {0};下面这一堆参数基本都是写死的,照抄即可theRasEntry.dwType = 2;theRasEntry.dwDialMode = 1;theRasEntry.dwChannels = 0;theRasEntry.dwFrameSize= 0;theRasEntry.dwReserved1 = 0;theRasEntry.dwReserved2 = 0;theRasEntry.dwCountryID = 86;theRasEntry.dwVpnStrategy = 0;theRasEntry.dwRedialCount = 3;theRasEntry.dwfOptions2 = 367;theRasEntry.dwRedialPause = 60;theRasEntry.dwCountryCode = 86;theRasEntry.dwfNetProtocols = 4;theRasEntry.dwEncryptionType = 3;theRasEntry.dwFramingProtocol = 1;theRasEntry.dwfOptions = 1024262928;theRasEntry.dwDialExtraPercent = 75;theRasEntry.dwHangUpExtraPercent = 10;theRasEntry.dwDialExtraSampleSeconds = 120;theRasEntry.dwHangUpExtraSampleSeconds = 120;theRasEntry.dwfNetProtocols= RASNP_Ip;theRasEntry.dwFramingProtocol = RASFP_Ppp;theRasEntry.dwSize = sizeof(theRasEntry);lstrcpyW(theRasEntry.szDeviceName,L"WAN 微型端口 (L2TP)");lstrcpyW(theRasEntry.szDeviceType,L"Vpn");lstrcpyW(theRasEntry.szLocalPhoneNumber,wzIP);//创建目标名dwRet = RasSetEntryPropertiesW(NULL,wzEntryName,&theRasEntry,theRasEntry.dwSize,0,0);if (ERROR_SUCCESS != dwRet){return dwRet;} RASCREDENTIALSW theRascredentials = {0};theRascredentials.dwMask = 11;lstrcpyW(theRascredentials.szUserName,wzUserName);lstrcpyW(theRascredentials.szPassword,wzPassWord);theRascredentials.dwSize = sizeof(theRascredentials);//用户密码保存dwRet = RasSetCredentialsW(NULL,wzEntryName,&theRascredentials,FALSE);if (ERROR_SUCCESS != dwRet){return dwRet;}RASDIALPARAMSW theRasDialParams = {0};lstrcpyW(theRasDialParams.szUserName,wzUserName);lstrcpyW(theRasDialParams.szPassword,wzPassWord);lstrcpyW(theRasDialParams.szEntryName,wzEntryName);theRasDialParams.dwSize = sizeof(theRasDialParams);//开始连接vpnHRASCONN hRasConn = NULL;dwRet = RasDialW(NULL,NULL,&theRasDialParams,0,NULL,&hRasConn);if (ERROR_SUCCESS != dwRet){return dwRet;}return NO_ERROR;}






原创粉丝点击