天天看點

RasAPI函數實作PPPOE撥号

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.      

調用方法