创建PPPOE连接出错,请高手看下问题出在哪里。
VS 2010 写的创建 PPPOE连接的程序,执 行 RasSetEntryProperties(vbNullString, sEntryName, re, Marshal.SizeOf(re), 0, 0) 时 提示“尝试读取或写入受保护的内存,这通常指示其它内存已损坏。”下面是错误截图和程序代码,麻烦各位高手帮忙给看一下。
程序代码:
Option Explicit On Imports System Imports System.Text Imports Microsoft.Win32 Imports System.Runtime.InteropServices Public Class FormMain Public Const MAX_PATH As Int32 = 260 Public Const RAS_MaxPATH As Int32 = 259 Public Const RAS_MaxUserName As Int32 = 256 Public Const RAS_MaxPassword As Int32 = 256 Public Const RAS_MaxDomain As Int32 = 15 Public Const RAS_MaxX25PadType As Int32 = 31 Public Const RAS_MaxDeviceType As Int32 = 16 Public Const RAS_MaxPhoneNumber As Int32 = 128 Public Const RAS_MaxIpAddress As Int32 = 15 Public Const RAS_MaxIpxAddress As Int32 = 21 Public Const RAS_MaxEntryName As Int32 = 256 Public Const RAS_MaxDeviceName As Int32 = 128 Public Const RAS_MaxCallbackNumber As Int32 = RAS_MaxPhoneNumber Public Const RAS_MaxAreaCode As Int32 = 10 Public Const RAS_MaxPadType As Int32 = 32 Public Const RAS_MaxX25Address As Int32 = 200 Public Const RAS_MaxFacilities As Int32 = 200 Public Const RAS_MaxUserData As Int32 = 200 public const RAS_MaxReplyMessage As Int32 = 1024 Public Const RAS_MaxDnsSuffix As Int32 = 256 Public Structure GUID Public Data1 As UInteger Public Data2 As UShort Public Data3 As UShort <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=8)> Public data4 As String End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Structure RASDIALPARAMS Public Size As Int32 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxEntryName + 1)> Public EntryName As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxPhoneNumber + 1)> Public PhoneNumber As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxCallbackNumber + 1)> Public CallBackNumber As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxUserName + 1)> Public UserName As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxPassword + 1)> Public Password As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxDomain + 1)> Public Domain As String Public dwSubEntry As Integer Public dwCallbackId As UInt32 End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Structure RASIPADDR Public a As Byte Public b As Byte Public c As Byte Public d As Byte End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Structure RASENTRY Public dwSize As Int32 Public dwfOptions As Int32 Public dwCountryID As Int32 Public dwCountryCode As Int32 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxAreaCode + 1)> Public szAreaCode As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxPhoneNumber + 1)> Public szLocalPhoneNumber As String Public dwAlternateOffset As Int32 Public ipaddr As RASIPADDR Public ipaddrDns As RASIPADDR Public ipaddrDnsAlt As RASIPADDR Public ipaddrWins As RASIPADDR Public ipaddrWinsAlt As RASIPADDR Public dwFrameSize As Int32 Public dwfNetProtocols As Int32 Public dwFramingProtocol As Int32 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxPATH)> Public szScript As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxPATH)> Public szAutodialDll As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxPATH)> Public szAutodialFunc As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxDeviceType + 1)> Public szDeviceType As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxDeviceName + 1)> Public szDeviceName As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxX25PadType + 1)> Public szX25PadType As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxX25Address + 1)> Public szX25Address As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxFacilities + 1)> Public szX25Facilities As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxUserData + 1)> Public szX25UserData As String Public dwChannels As Int32 Public dwReserved1 As Int32 Public dwReserved2 As Int32 Public dwSubEntries As Int32 Public dwDialMode As Int32 Public dwDialExtraPercent As Int32 Public dwDialExtraSampleSeconds As Int32 Public dwHangUpExtraPercent As Int32 Public dwHangUpExtraSampleSeconds As Int32 Public dwIdleDisconnectSeconds As Int32 Public dwType As Int32 Public dwEncryptionType As Int32 Public dwCustomAuthKey As Int32 Public guidId As GUID <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> Public szCustomDialDll As String Public dwVpnStrategy As Int32 Public dwfOptions2 As Int32 Public dwfOptions3 As Int32 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxDnsSuffix)> Public szDnsSuffix As String Public dwTcpWindowSize As Int32 <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=MAX_PATH)> Public szPrerequisitePbk As String <MarshalAs(UnmanagedType.ByValTStr, SizeConst:=RAS_MaxEntryName + 1)> Public szPrerequisiteEntry As String Public dwRedialCount As Int32 Public dwRedialPause As Int32 End Structure <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Structure RASCREDENTIALS Public dwSize As Long Public dwMask As Long Public szUserName() As Byte Public szPassword() As Byte Public szDomain() As Byte End Structure Public Declare Auto Function RasSetEntryDialParams Lib "rasapi32.dll" (ByVal PhoneBook As String, ByRef DialParams As RASDIALPARAMS, ByRef Password As Int32) As Int32 Private Sub FormMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Create_PPPoE_Connection("PPPOE1", "test", "123456") End Sub '================================================================================== '函数名称 Create_PPPoE_Connection(ByVal sEntryName As String, ByVal sUserName As String, ByVal sPassword As String) As Boolean '参 数 sEntryName 连接名称 ' sUserName 用户名 ' sPassword 密码 '描 述 创建PPPOE拨号连接 '示 例 Create_PPPoE_Connection("宽带连接","user01","123456") '================================================================================== Private Const ET_None As Long = 0 ' No encryption Private Const ET_Require As Long = 1 ' Require Encryption Private Const ET_RequireMax As Long = 2 ' Require max encryption Private Const ET_Optional As Long = 3 ' Do encryption if possible. None Ok. Private Const VS_Default As Long = 0 ' default (PPTP for now) Private Const VS_PptpOnly As Long = 1 ' Only PPTP is attempted. Private Const VS_PptpFirst As Long = 2 ' PPTP is tried first. Private Const VS_L2tpOnly As Long = 3 ' Only L2TP is attempted. Private Const VS_L2tpFirst As Long = 4 ' L2TP is tried first. Private Const RASET_Phone As Long = 1 ' Phone lines: modem, ISDN, X.25, etc Private Const RASET_Vpn As Long = 2 ' Virtual private network Private Const RASET_Direct As Long = 3 ' Direct connect: serial, parallel Private Const RASET_Internet As Long = 4 ' BaseCamp internet Private Const RASET_Broadband As Long = 5 ' Broadband Private Declare Function RasSetEntryProperties Lib "rasapi32" Alias "RasSetEntryPropertiesA" (ByVal lpszPhonebook As String, ByVal lpszEntry As String, ByVal lpRasEntry As RASENTRY, ByVal dwEntryInfoSize As Long, ByVal lpbDeviceInfo As Long, ByVal dwDeviceInfoSize As Long) As Long Private Declare Function RasSetCredentials Lib "rasapi32" Alias "RasSetCredentialsA" (ByVal lpszPhonebook As String, ByVal lpszEntry As String, ByVal lpCredentials As RASCREDENTIALS, ByVal fClearCredentials As Long) As Long Private Function Create_PPPoE_Connection(ByVal sEntryName As String, ByVal sUserName As String, ByVal sPassword As String) As Boolean Create_PPPoE_Connection = False Dim re As New RASENTRY Dim sDeviceName As String, sDeviceType As String sDeviceName = "WAN 微型端口 (PPTP)" sDeviceType = "PPPoE" With re .dwSize = Marshal.SizeOf(re) .dwCountryCode = 86 .dwCountryID = 86 .dwDialExtraPercent = 75 .dwDialExtraSampleSeconds = 120 .dwDialMode = 1 .dwEncryptionType = 3 .dwfNetProtocols = 4 .dwfOptions = 1024262928 .dwfOptions2 = 367 .dwFramingProtocol = 1 .dwHangUpExtraPercent = 10 .dwHangUpExtraSampleSeconds = 120 .dwRedialCount = 3 .dwRedialPause = 60 .dwType = RASET_Broadband .szDeviceName = sDeviceName .szDeviceType = sDeviceType End With Dim params As New RASDIALPARAMS params.Size = Marshal.SizeOf(params) params.EntryName = sEntryName params.UserName = sUserName params.Password = sPassword Dim bFlag As Int32 = 0 RasSetEntryProperties(vbNullString, sEntryName, re, Marshal.SizeOf(re), 0, 0) End Function End Class