u我想做个vpn的连接器,无奈兄弟delphi水平差,做不出来,费了九牛二虎之力找到了个delphi的源代码,可是编译后怎么也
连接不上,希望各位大牛帮下忙,替小弟解决下!先谢过!!
我简单测了下这步往下没执行!
if RasSetEntryPropertiesA(PChar(''), PChar(sEntryName), @re, sizeof(RASENTRY), 0, 0) = 0 then unit Unit1;interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, StrUtils; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; type GUID = record //申明调用RAS(创建VPN连接用)函数所需结构体 Data1: integer; Data2: Shortint; Data3: Shortint; Data4: array[0..6] of byte; end; type RASIPADDR = record //申明调用RAS(创建VPN连接用)函数所需结构体 a: byte; b: byte; c: byte; d: byte; end; type RASENTRY = record //申明调用RAS(创建VPN连接用)函数所需结构体 dwSize: integer; dwfOptions: integer; dwCountryID: integer; dwCountryCode: integer; szAreaCode: array[0..9] of byte; szLocalPhoneNumber: array[0..127] of byte; dwAlternateOffset: integer; ipaddr: RASIPADDR; ipaddrDns: RASIPADDR; ipaddrDnsAlt: RASIPADDR; ipaddrWins: RASIPADDR; ipaddrWinsAlt: RASIPADDR; dwFrameSize: integer; dwfNetProtocols: integer; dwFramingProtocol: integer; szScript: array[0..258] of char; szAutodialDll: array[0..258] of char; szAutodialFunc: array[0..258] of char; szDeviceType: array[0..15] of char; szDeviceName: array[0..127] of char; szX25PadType: array[0..31] of char; szX25Address: array[0..199] of char; szX25Facilities: array[0..199] of char; szX25UserData: array[0..199] of char; dwChannels: integer; dwReserved1: integer; dwReserved2: integer; dwSubEntries: integer; dwDialMode: integer; dwDialExtraPercent: integer; dwDialExtraSampleSeconds: integer; dwHangUpExtraPercent: integer; dwHangUpExtraSampleSeconds: integer; dwIdleDisconnectSeconds: integer; dwType: integer; dwEncryptionType: integer; dwCustomAuthKey: integer; guidId: GUID; szCustomDialDll: array[0..258] of char; dwVpnStrategy: integer; dwfOptions2: integer; dwfOptions3: integer; szDnsSuffix: array[0..254] of char; dwTcpWindowSize: integer; szPrerequisitePbk: array[0..258] of char; szPrerequisiteEntry: array[0..255] of char; dwRedialCount: integer; dwRedialPause: integer; end; TRasCredentialsA = record dwSize, dwMask: Longint; szUserName: array[0..255] of AnsiChar; zPassword: array[0..255] of AnsiChar; szDomain: array[0..14] of AnsiChar; end; implementation {$R *.dfm} function RtlMoveMemory(Destination, Source: Pointer; Length: integer): integer; stdcall; external 'kernel32.dll' //function RasSetEntryPropertiesA(lpszPhonebook, lpszEntry: string; lpRasEntry: RASENTRY; dwEntryInfoSize, lpbDeviceInfo, dwDeviceInfoSize: integer): Longint; stdcall; external 'Rasapi32.dll' function RasSetEntryPropertiesA(lpszPhonebook, szEntry: PAnsiChar; lpbEntry: Pointer; dwEntrySize: Longint; lpbDeviceInfo: Pointer; dwDeviceInfoSize: Longint): Longint; stdcall; external 'Rasapi32.dll'; //function RasSetCredentialsA(lpszPhonebook, lpszEntry: PAnsiChar; lpCredentials: Pointer; fClearCredentials: integer): Longint; stdcall; external 'Rasapi32.dll'; function RasSetCredentialsA( lpszPhoneBook, lpszEntry: PAnsiChar; var lpCredentials: TRasCredentialsA; fRemovePassword: LongBool ): Longint; stdcall; external 'Rasapi32.dll'; function Create_VPN_Connection(sEntryName, sServer, sUsername, sPassword: string): Boolean; var sDeviceName, sDeviceType: string; re: RASENTRY; rc: TRasCredentialsA; begin result := false; FillChar(rc.szUserName, Length(rc.szUserName), #0); FillChar(rc.zPassword, Length(rc.zPassword), #0); FillChar(re.szScript, Length(re.szScript), #0); FillChar(re.szAutodialDll, Length(re.szAutodialDll), #0); FillChar(re.szAreaCode, Length(re.szAreaCode), #0); FillChar(re.szLocalPhoneNumber, Length(re.szLocalPhoneNumber), #0); FillChar(re.szAutodialFunc, Length(re.szAutodialFunc), #0); FillChar(re.szAutodialDll, Length(re.szAutodialDll), #0); FillChar(re.szDeviceType, Length(re.szDeviceType), #0); FillChar(re.szDeviceName, Length(re.szDeviceName), #0); FillChar(re.szX25PadType, Length(re.szX25PadType), #0); FillChar(re.szX25Address, Length(re.szX25Address), #0); FillChar(re.szX25Facilities, Length(re.szX25Facilities), #0); FillChar(re.szX25UserData, Length(re.szX25UserData), #0); FillChar(re.szCustomDialDll, Length(re.szCustomDialDll), #0); FillChar(re.szDnsSuffix, Length(re.szDnsSuffix), #0); FillChar(re.szPrerequisitePbk, Length(re.szPrerequisitePbk), #0); FillChar(re.szPrerequisiteEntry, Length(re.szPrerequisiteEntry), #0); sDeviceName := 'WAN 微型端口 (L2TP)'; sDeviceType := 'vpn'; re.dwSize := sizeof(re); re.dwCountryCode := 86; re.dwCountryID := 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; StrCopy(re.szDeviceName, PChar(sDeviceName)); StrCopy(re.szDeviceType, PChar(sDeviceType)); re.dwVpnStrategy := 0; re.dwEncryptionType := 3; FillChar(rc.szUserName, Length(rc.szUserName), #0); FillChar(rc.szDomain, Length(rc.szDomain), #0); rc.dwSize := sizeof(rc); rc.dwMask := 11; StrCopy(rc.szUserName, PChar(sUsername)); StrCopy(rc.zPassword, PChar(sPassword)); if RasSetEntryPropertiesA(PChar(''), PChar(sEntryName), @re, sizeof(RASENTRY), 0, 0) = 0 then if RasSetCredentialsA(PChar(''), PChar(sEntryName), rc, True) = 0 then result := true; end; procedure TForm1.Button1Click(Sender: TObject); var sDeviceType, sServer, sEntryName, sUsername, sPassword: string; begin sDeviceType := 'PPPoE'; sServer := '163.1.1.1'; //vpn服务器sEntryName := 'VPN连接'; sUsername := ''; sPassword := ''; if Create_VPN_Connection(sEntryName, sServer, sUsername, sPassword) = true then ShowMessage('VPN连接建立成功') else ShowMessage('VPN连接建立失败'); end; end.

解决方案 »

  1.   

    sDeviceType := 'PPPoE'; sServer := '163.1.1.1'; //vpn服务器sEntryName := 'VPN连接'; sUsername := ''; sPassword := ''; 我觉得以上几个参数有问题。确认你的VPN是否使用正常?以上参数是否正确?尤其是用户名、密码是否正确?
      

  2.   

    这是我随便加的,如果测试的话得加入自己知道的vpn和密码!
    这组好像能用
    222.218.124.77
    帐号 100000956
    密码 100000956
    sServer := '222.218.124.77'; //vpn服务器
    sUsername := '100000956'; 
    sPassword := '100000956'; 
      

  3.   

    http://blog.csdn.net/Channels_net/archive/2009/03/17/3997874.aspx原来的代码!