会员: 密码:  免费注册 | 忘记密码 | 会员登录 网页功能: 加入收藏 设为首页 网站搜索  
技术文档 > Delphi
RasAPI函数实现PPPOE拨号
发表日期:2009-12-22 11:22:05作者: 出处:  

unit uDial;
    
  interface
  uses
        Windows,Messages, SysUtils, Ras;// Classes;

    
  var
        //EntryName,UserName,PassWord:string;

        nRasConnCount: DWORD;
        aRasConn:array [0..10] of TRASCONN;
        hRasConn:THRASCONN;
        IsConnected:boolean;
        RasDialConEnd:boolean;
        RasError:string;
    
        function Dial(FEntryName, FUserName, FPassword : String):integer;
        procedure Disconnected;
        function GetIP:string;
  implementation
    
  procedure Disconnected;//断开连接

  begin
      try
          if hRasConn <> 0 then
          begin
              RasHangUp(hRasConn);
              hRasConn:= 0;
          end;
      except
      end;
  end;
    
  function GetActiveConn:string; //读取活动连接

  var
          dwRet : DWORD;
          nCB : DWORD;
          Buf : array [0..255] of Char;
  begin
      try
          aRasConn[0].dwSize := SizeOf(aRasConn[0]);
          nCB := SizeOf(aRasConn);
          dwRet := RasEnumConnections(@aRasConn, @nCB, @nRasConnCount);//枚举活动连接

          if dwRet <> 0 then begin
                  RasGetErrorString(dwRet, @Buf[0], SizeOf(Buf));
                  result:=Buf;
          end
          else result:='';
      except
      end;
  end;
    
  function GetActiveConnHandle(szName : String) : THRASCONN; //读取活动连接的句柄

  var
          I : Integer;
  begin
          GetActiveConn;
          if nRasConnCount > 0 then begin
                  for I := 0 to nRasConnCount - 1 do begin
                          if StrIComp(PChar(szName), aRasConn[I].szEntryName) = 0 then begin
                                  Result := aRasConn[I].hRasConn;
                                  Exit;
                          end;
                  end;
          end;
          Result := 0;
  end;
    
  function CheckConn(FEntryName:string):boolean; //检测是否已经连接

  begin
          hRasConn := GetActiveConnHandle(FEntryName);
          if hRasConn <> 0 then
              result:=True
          else
              Result:=False;
  end;
    
  function GetIP:string;
  begin
        result:=RasGetIPAddress;
        if result='' then result :='Unknow';
  end;
    
  //回调函数

  procedure RasDialFunc(unMsg : DWORD;FRasConnState : TRASCONNSTATE;FdwError : DWORD); stdcall;
  var
      Buf: array [0..255] of Char;
      ConnState:string;
  begin
      try
          ConnState:=RasConnectionStateToString(FRasConnState); //连接状态

          if FRasConnState = RASCS_Connected then
          begin //连接成功

                  IsConnected:=true;
                  RasDialConEnd:=true;
          end
          else if FdwError<>0 then //连接出错

          begin
                  RasGetErrorString(FdwError, @Buf[0], SizeOf(Buf));
                  RasError:=(Buf);
                  Disconnected;
                  IsConnected:=false;
                  RasDialConEnd:=true;
          end;
      except
      end;
    
  end;
    
  function Dial(FEntryName, FUserName, FPassword : String):integer;//拨号连接

  {type TDialThread=class(TThread)
        private
        public
                constructor Create;
                procedure Execute; override;
  end;
  constructor TDialThread.Create;
  begin
        inherited Create(false); //false 创建后立即执行

        FreeOnTerminate:=true; //true 自动结束线程

  end;
    
  procedure TDialThread.Execute;
  begin
                  while Not RasDialConEnd do
                  begin
                      sleep(100);
                  end;
  end;
  }
  var
          rdParams : TRASDIALPARAMS;
          dwRet : DWORD;
          Buf : array [0..255] of Char;
          //thDial : TDialThread;

  begin
      result:=0;
      try
          hRasConn := GetActiveConnHandle(FEntryName);
          if hRasConn <> 0 then //已经在连接中

          begin
                  IsConnected:=true;
                  RasDialConEnd:=true;
                  Exit;
          end;
    
          // setup RAS Dial Parameters

          FillChar(rdParams, SizeOf(rdParams), 0);
          rdParams.dwSize := SizeOf(TRASDIALPARAMS);
          strCopy(rdParams.szUserName, PChar(FUserName));
          strCopy(rdParams.szPassword, PChar(FPassword));
          strCopy(rdParams.szEntryName, PChar(FEntryName));
          rdParams.szPhoneNumber[0] := #0;
          rdParams.szCallbackNumber[0] := '*';
          rdParams.szDomain := '*';
    
          hRasConn := 0;RasDialConEnd:=false;
          dwRet := RasDial(nil, nil, @rdParams, 0, @RasDialFunc, @hRasConn);
          result:=integer(dwRet);
          if dwRet <> 0 then
          begin
                  RasGetErrorString(dwRet, @Buf[0], SizeOf(Buf));
                  RasError:=(IntToStr(dwRet) + ' ' + Buf);
                  Disconnected;
          end
          else;
      except
      end;
  end;
    
  function DisconnectActiveConnection: Boolean;//断开活动的连接

  const
  MaxConnections = 10;//假设最多有10个活动的拨号连接

  var
                  connections : array[0..MaxConnections-1] of TRASCONN;
                  //拨号连接数组

                  longSize : dword;
                  intAvailabelConnections : dword;
                  //活动的拨号连接的实际数目

                  intIndex : integer;
                  strTemp : string;
                  dwResult : DWORD;
                  dwSize : DWORD;
                  RASpppIP : TRASPPPIP;
                  //活动的拨号连接的动态IP地址信息

  begin
                  connections[ 0 ].dwSize := sizeof(TRASCONN);
                  longSize := MaxConnections * connections[ 0 ].dwSize;
                  //接收活动连接的缓冲区大小

                  intAvailabelConnections := 0;
                  //获取所有活动的拨号连接的信息(连接句柄和设置信息)

                  dwResult := RasEnumConnections( @connections[ 0 ],@longSize,@intAvailabelConnections );
                  if 0 = dwResult then
                  begin
                                  //form1.memo1.lines.add( '现有的活动连接有' +IntToStr( intAvailabelConnections ) + '个');

                                  //显示所有活动的拨号连接的信息(设置信息和动态IP地址)

                                  for intIndex := 0 to intAvailabelConnections - 1 do
                                  begin
                                                  //显示一个活动的拨号连接的设置信息

                                                  //strTemp := '连接名称:'+ StrPAS( connections[ intIndex ].szEntryName )+ ',设备类型:'+ StrPAS( connections[ intIndex ].szDeviceType )+ ',设备名称:'+ StrPAS( connections[ intIndex ].szDeviceName );

                                                  //form1.memo1.lines.add( strTemp );

                                                  //显示一个活动的拨号连接的动态IP地址

                                                  dwSize := SizeOf(RASpppIP);
                                                  RASpppIP.dwSize := dwSize;
                                                  dwResult := RASGetProjectionInfo( connections[ intIndex ].hRasConn,RASP_PppIp,@RasPPPIP,@dwSize);//获取动态IP地址

                                                  {if 0 <> dwResult then
                                                  form1.memo1.lines.add('错误:' + inttostr( dwResult ))
                                                  else
                                                  form1.memo1.lines.add('动态地址:' + StrPas(RASpppIP.szIPAddress));
                                                  }
                                                  RasHangUp(connections[ intIndex ].hRasConn);
                                                  {if RasHangUp(connections[ intIndex ].hRasConn)=0 then
                                                        form1.memo1.lines.add('断开连接成功')
                                                  else
                                                        form1.memo1.lines.add('断开连接失败');
                                                        }
                                  end;
                  end;
  end;
    
  end.



  

调用方法

mRet:=Dial('拨号连接的名称','用户名','密码');

返回顶部】 【打印本页】 【关闭窗口

关于我们 / 给我留言 / 版权举报 / 意见建议 / 网站编程QQ群   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net 加载时间 0.00396