| 发表于:2008-01-07 20:54:449楼 得分:100 |
呵呵,退出的时候,报什么错啊? 下面的代码是我测试时的代码,退出时没有抱错。 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 := 'www.vpn.com'; sentryname := 'vpn连接'; susername := ''; spassword := ''; if create_vpn_connection(sentryname, sserver, susername, spassword) = true then showmessage('vpn连接建立成功') else showmessage('vpn连接建立失败'); end; end. | | |
|