用c#实现建立一个VPN拨号连接并自动拨号
我过些资料,说用RAS库里的RasSetEntryProperties方法可以实现。我写了一个用C#调用rasapi32.dll方法如下:
[DllImport("rasapi32.dll", CharSet = CharSet.Auto)]
public extern static int RasSetEntryProperties(
string phonebook,
string entry,
RasEntry rasentry,
int entryinfosize,
int deviceinfo,
int deviceinfosize
);
RasEntry结构定义如下:
public struct RasEntry
{
public int dwSize;
public int dwfOptions;
public int dwCountry;
public int dwCountryCode;
public byte szAreaCode;
public byte szLocalPhoneNumber;
public int dwalternateOffset;
public RasIpAddr ipaddr;
public RasIpAddr ipaddrDns;
public RasIpAddr ipaddrDnsAlt;
public RasIpAddr ipaddrWins;
public RasIpAddr ipaddrWinsAlt;
public int dwFrameSize;
public int dwfNetProtocols;
public int dwFramingProtocol;
public byte szScript;
public byte szAutodialDll;
public byte szAutodialFunc;
public byte szDeviceType;
public byte szDeviceName;
public byte szX25PadType;
public byte szX25Address;
public byte szX25Facilities;
public byte szX25UserData;
public int dwChannels;
public int dwReserved1;
public int dwReserved2;
public int dwSubEntries;
public int dwDialMode;
public int dwDialExtraPercent;
public int dwDialExtraSampleSeconds;
public int dwHangUpExtraPercent;
public int dwHangUpExtraSampleSeconds;
public int dwIdleDisconnectSeconds;
public int dwType;
public int dwEncryptionType;
public int dwCustomAuthKey;
public GUID guidId;
public byte szCustomDialDll;
public int dwVpnStrategy;
public int dwfOptions2;
public int dwfOptions3;
public byte szDnsSuffix;
public int dwTcpWindowSize;
public byte szPrerequisitePbk;
public byte szPrerequesiteEntry;
public int dwRedialCount;
public int dwRedialPause;
}
调用方法代码如下:
RasEntry re = new RasEntry();
re.dwSize = Marshal.SizeOf(typeof(RasEntry));
re.dwCountryCode = 86;
re.dwDialExtraPercent = 75;
re.dwDialExtraSampleSeconds = 120;
re.dwDialMode = 1;
re.dwEncryptionType = 3;
re.dwfNetProtocols = 4;
re.dwfOptions = 1024262928;
re.dwfOptions2 = 367;
re.dwFramingProtocol = 1;
re.dwHangUpExtraPercent = 10;
re.dwHangUpExtraSampleSeconds = 120;
re.dwRedialCount = 3;
re.dwRedialPause = 60;
re.dwType = 5;
re.szAreaCode = 10;
re.szLocalPhoneNumber = 128;
re.szScript = 259;
re.szAutodialDll = 259;
re.szAutodialFunc = 259;
re.szDeviceType = 16;
re.szDeviceName = 128;
re.