Delphi的拨号连接类

2016-02-19 13:19 34 1 收藏

每个人都希望每天都是开心的,不要因为一些琐事扰乱了心情还,闲暇的时间怎么打发,关注图老师可以让你学习更多的好东西,下面为大家推荐Delphi的拨号连接类,赶紧看过来吧!

【 tulaoshi.com - 编程语言 】

  前一阵因为工作需要写了一个类来进行windows拨号,整理了一下,多封装了几个windows ras api,放上来大家提提意见。现在支持读取windows拨号连接列表、拨号、挂断、创建/删除连接,可以适用98/2000/XP,windows me 和NT没测试过,想来应该是可以的。以后有时间写成component,加入对拨号事件的支持。

  uses
    ras, Classes, SysUtils, StrUtils, Windows, Forms;

  type
    ERasError = Exception;

  type
    TRASConnection = class
    private
      FPlatForm: integer;
      FConnected: Boolean;
      FRasEntries: TStringList;
      FRasConn: HRASCONN;
      FParams: RasDialParams;
      Ferrno: integer;
      function GetPassword: string;
      procedure SetPassword(Password: string);
      function GetPhoneNo: string;
      procedure SetPhoneNo(Number: string);
      function GetCallBack: string;
      procedure SetCallBack(Number: string);
      function GetDomain: string;
      procedure SetDomain(Domain: string);
      function GetUserName: string;
      procedure SetUserName(UserName: string);
      function GetEntryName: string;
      procedure SetEntryName(Entry: string);
      function GetConnected: Boolean;
      procedure SetConnected(AValue: Boolean);
      function GetRasEntries: TStringList;
    public
      property RasEntries: TStringList read GetRasEntries;
      property PhoneNumber: string read GetPhoneNo write SetPhoneNo;
      property CallBackNumber: string read GetCallBack write SetCallBack;
      property Domain: string read GetDomain write SetDomain;
      property EntryName: string read GetEntryName write SetEntryName;
      property username: string read GetUsername write SetUsername;
      property password: string read GetPassword write SetPassword;
      property Active: Boolean read GetConnected write SetConnected;
      procedure Connect;
      procedure DisConnect;
      function GetErrorCode: integer;
      procedure FreeAndHangup;
      constructor Create; reintroduce;
      destructor Destroy; override;
      procedure CreateRasEntry;
      procedure DeleteRasEntry(AEntryName: string);
        //function GetErrorReason: integer;
    end;

  implementation

  { TRASConnection }

  procedure TRASConnection.Connect;
  var
    i: integer;
    s: string;
  begin
    FParams.dwSize := sizeof(RasDialParams);
    i := RasDial(nil, nil, @FParams, 0, nil, @FRasConn);
    if i 0 then begin
      Ferrno := i;
      case i of
        691: s := '身分验证失败!';
        692: s := '打开端口失败!';
        676: s := '线路忙,请稍候再拨!';
        677: s := '语音响应错误!';
        678: s := '没有应答!';
        679: s := '无载波信号!';
        680: s := '无拨号音!';
      else
        s := '未知错误!';
      end;
      RasHangUp(FRasConn);
      raise ERasError.Create(s);
    end
    else FConnected := True;
  end;

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  procedure TRASConnection.DisConnect;
  begin
    RasHangup(FRasConn);
    FRasConn := 0;
  end;

  function TRASConnection.GetCallBack: string;
  begin
    Result := string(FParams.szCallbackNumber);
  end;

  function TRASConnection.GetConnected: Boolean;
  var
    i, len, num: integer;
    x: array of RASCONN;
  begin
    Result := False;
    SetLength(x, 1);
    x[0].dwSize := sizeof(RASCONN);
    len := x[0].dwSize;
    num := 0;
    RasEnumConnections(@x[0], @len, @num);
    if num 0 then begin
      SetLength(x, num);
      x[0].dwSize := sizeof(RASCONN);
      len := x[0].dwSize; num := 0;
      RasEnumConnections(@x[0], @len, @num);
      for i := 1 to num do
        if StrComp(x[i - 1].szEntryName, PChar(EntryName)) = 0 then begin
          FRasConn := x[i - 1].hrasconn;
          Result := True;
          Break;
        end;
    end;
    SetLength(x, 0);
  end;

  function TRASConnection.GetDomain: string;
  begin
    Result := string(FParams.szDomain);
  end;

  function TRASConnection.GetErrorCode: integer;
  begin
    Result := FErrno;
  end;

  function TRASConnection.GetPassword: string;
  begin
    Result := '**********';
  end;

  function TRASConnection.GetPhoneNo: string;
  begin
    Result := string(FParams.szPhoneNumber);
  end;

  function TRASConnection.GetEntryName: string;
  begin
    Result := string(FParams.szEntryName);
  end;

  function TRASConnection.GetUserName: string;
  begin
    Result := string(FParams.szUserName);
  end;

  procedure TRASConnection.SetCallBack(Number: string);
  begin
    StrLCopy(FParams.szCallbackNumber, PChar(Number), sizeof(FParams.szCallbackNumber) - 1);
  end;

  procedure TRASConnection.SetConnected(AValue: Boolean);
  begin
    if AValue and not GetConnected then Connect
    else if not AValue and GetConnected then DisConnect;
  end;

  procedure TRASConnection.SetDomain(Domain: string);
  begin
    StrLCopy(FParams.szDomain, PChar(Domain), sizeof(FParams.szDomain) - 1);
  end;

  procedure TRASConnection.SetPassword(Password: string);
  begin
    StrLCopy(FParams.szPassword, PChar(Password), sizeof(FParams.szPassword) - 1);
  end;

  procedure TRASConnection.SetPhoneNo(Number: string);
  begin
    StrLCopy(FParams.szPhoneNumber, PChar(Number), sizeof(FParams.szPhoneNumber) - 1);
  end;

  procedure TRASConnection.SetEntryName(Entry: string);
  var
    i: integer;
  begin
    for i := 0 to FRasEntries.Count - 1 do
      if FRasEntries.Strings[i] = Entry then begin
        StrCopy(FParams.szEntryName, PChar(Entry));
        Exit;
      end;
    StrCopy(FParams.szEntryName, '');
  end;

  procedure TRASConnection.SetUserName(UserName: string);
  begin
    StrLCopy(FParams.szUserName, PChar(UserName), sizeof(FPArams.szUserName) - 1);
  end;

  procedure TRASConnection.FreeAndHangup;
  begin
    if Active then DisConnect;
    Free;
  end;

  function TRASConnection.GetRasEntries: TStringList;
  var
    ren: array of RASEntryName;
    ren98: array of RASEntryName98;
    c, l: integer;
  begin
    FRasEntries.Clear;
    c := 0;
    case FPlatForm of
    VER_PLATFORM_WIN32_WINDOWS:
      begin
        setlength(ren98, 1);
        ren98[0].dwSize := sizeof(RASENTRYNAME98);
        l := sizeof(RASENTRYNAME98);
        if RasEnumEntries(nil, nil, @ren98[0], @l, @c) = ERROR_BUFFER_TOO_SMALL then begin
          setlength(ren, c);
          RasEnumEntries(nil, nil, @ren98[0], @l, @c);
        end;
        while c 0 do begin
          FRasEntries.Add(string(ren98[c - 1].szEntryName));
          Dec(c);
        end;
        SetLength(ren98, 0);
      end;
    VER_PLATFORM_WIN32_NT:
      begin
        setlength(ren, 1);
        ren[0].dwSize := sizeof(RASENTRYNAME);
        l := sizeof(RASENTRYNAME);
        if RasEnumEntries(nil, nil, @ren[0], @l, @c) = ERROR_BUFFER_TOO_SMALL then begin
          setlength(ren, c);
          RasEnumEntries(nil, nil, @ren[0], @l, @c);
        end;
        while c 0 do begin
          FRasEntries.Add(string(ren[c - 1].szEntryName));
          Dec(c);
        end;
        SetLength(ren, 0);
      end;
    end;
    if FRasEntries.Count0 then EntryName:=FRasEntries.Strings[0];
    Result := FRasEntries;
  end;

  constructor TRASConnection.Create;
  var
    OS: OSVersionInfoA;
  begin
    inherited;
    OS.dwOSVersionInfoSize:=sizeof(OSVersionInfoA);
    GetVersionEx(OS);
    FPlatForm:=OS.dwPlatformId;
    FRasEntries := TStringList.Create;
    GetRasEntries;
  end;

  destructor TRASConnection.Destroy;
  begin
    FRasEntries.Free;
    inherited;
  end;

  procedure TRASConnection.CreateRasEntry;
  begin
    RasCreatePhonebookEntry(Application.Handle,nil);
  end;

  procedure TRASConnection.DeleteRasEntry(AEntryName: string);
  var
    i: integer;
  begin
    i:=FRasEntries.IndexOf(AEntryName);
    if i=-1 then Exit;
    FRasEntries.Delete(i);
    if AEntryName=EntryName then
      if FRasEntries.Count0 then EntryName:=FRasEntries.Strings[0]
      else EntryName:='';
    RasDeleteEntry(nil,PChar(AEntryName));
  end;
  
  Api声明:
  

  function RasDial(
    lpRasDialExtensions: PRASDIALEXTENSIONS;
    lpszPhonebook: LPCTSTR;
    lpRasDialParams: LPRASDIALPARAMS;
    dwNotifierType: DWORD;
    lpvNotifier: Pointer;
    lphRasConn: LPHRASCONN): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasDialA';

  function RasHangUp(rasconn: HRASCONN): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasHangUpA';

  function RasGetEntryDialParams(
    lpszPhonebook: LPCTSTR;
    lprasdialparams: LPRASDIALPARAMS;
    lpfPassword: LPBOOL): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasGetEntryDialParamsA';

  function RasEnumEntries(
    reserved: LPCTSTR;
    lpszPhonebook: LPCTSTR;
    lprasentryname: LPRASENTRYNAME;
    lpcb: LPDWORD;
    lpcEntries: LPDWORD): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasEnumEntriesA';

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  function RasEditPhonebookEntry(
    hwnd: HWND;
    lpszPhonebook: LPCTSTR;
    lpszEntryName: LPCTSTR): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasEditPhonebookEntryA';

  function RasGetEntryProperties(
    lpszPhonebook: LPCTSTR;
    lpszEntry: LPCTSTR;
    lpRasEntry: LPRASENTRY;
    lpdwEntryInfoSize: LPDWORD;
    lpbDeviceInfo: PBYTE;
    lpdwDeviceInfoSize: LPDWORD): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasGetEntryPropertiesA';

  function RasSetEntryProperties(
    lpszPhonebook: LPCTSTR;
    lpszEntry: LPCTSTR;
    lpRasEntry: LPRASENTRY;
    dwEntryInfoSize: DWORD;
    lpbDeviceInfo: PByte;
    dwDeviceInfoSize: DWORD): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasSetEntryPropertiesA';

  function RasEnumConnections(
    lprasconn: LPRASCONN;
    lpcb: LPDWORD;
    lpcConnections: LPDWORD): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasEnumConnectionsA';

  function RasEnumDevices(
    lpRasDevInfo: LpRasDevInfo;
    lpcb: LPDWORD;
    lpcdevices: LPDWORD): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasEnumDevicesA';

  function RasCreatePhonebookEntry(
    Handle: Hwnd; LpszPhoneBook: PChar): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasCreatePhonebookEntryA';

  function RasDeleteEntry(
    lpszPhonebook: PChar;
    lpszEntry: PChar): DWORD;
  stdcall; external 'RASAPI32.dll' name 'RasDeleteEntryA';
  

   

来源:http://www.tulaoshi.com/n/20160219/1603803.html

延伸阅读
随着因特网的迅猛发展,现在编程常需要在程序中直接联网来处理一些事项,如在线注册和在线帮助,这就要求我们要在程序中建立某些连接。很多软件在不知用户是否联网的情况下不管三七二十一就启动浏览器查找网址,费了九牛二虎之力只能查出一错误页来(当然不可能有什么好的结果)。如果我们在程序编写时能自动判断用户是否已经联网,如已经联网则...
标签: 电脑入门
用windows8系统的朋友都知道每次开机都重复拨通网络连接这一步骤,很麻烦;因此分享一个小技巧,能使计算机启动后自动连接网络。 1.打开网络和共享中心→更改适配器设置→右键选择宽带连接→创建快捷方式。 2.打开计算机 C 盘→点击查看→勾选隐藏的项目。 3.打开路径:C:ProgramDataMicrosoftWindows开始菜单程序启...
win7创建宽带拨号连接方法   对于使用xp系统比较习惯的朋友来说,突然换了win7系统,可能都有些不好掌握操作的技巧。我们今天要讲解的是最简单的,win7系统如何创建宽带拨号连接的方法。 首先,我们在菜单中打开控制面板的界面。 接下来点击进入网络和共享中心。 点击设置新的连接或网络,进去之后选择连接到int...
标签: windows10
如何创建win10拨号连接?   随着用户将系统升级到win10,在win10系统中我们需要对常用的功能进行设置,而拨号上网也是一项非常重要的配置,虽然在如今大部分用户都采用路由器形式上网,但还有部分使用窗口的ADSL拨号上网来连接网络,那么在win10系统下如何设置拨号连接呢?下面我们看下本文带来的详细操作方法。 win10创建拨号连...
标签: 电脑入门
Win10拨号连接怎么设置?很多新安装Windows10系统的朋友,开始都上不了网,原因是不知道宽带连接怎么创建。图老师小编今天针对非路由器的拨号上网用户,详细介绍下Win10创建宽带连接方法步骤。 第一步:首先在Win10桌面底部右下角的网络图标上点击鼠标右键,然后在弹出的设置菜单中,点击进入网络和共享中心如图。 第二步:然后在网络和...

经验教程

950

收藏

47
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部