unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(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(nil,   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   :=   'tuping';
    sPassword   :=   '123';
    if   Create_VPN_Connection(sEntryName,   sServer,   sUsername,   sPassword)   =   true   then
        ShowMessage('VPN连接建立成功')
    else
        ShowMessage('VPN连接建立失败');
end;
end.