⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rasdial1.pas

📁 < Delphi网络通信协议分析与应用实现>>一书的源代码。
💻 PAS
📖 第 1 页 / 共 3 页
字号:
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *


Unit:         Remote Access Service Dialer (RAS-DIALER)
Creation:     Feb 18, 1997.
EMail:        francois.piette@pophost.eunet.be    francois.piette@rtfm.be
              http://www.rtfm.be/fpiette
Legal issues: Copyright (C) 1996, 1997, 1998 by Fran鏾is PIETTE
              Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
              <francois.piette@pophost.eunet.be>

              This software is provided 'as-is', without any express or
              implied warranty.  In no event will the author be held liable
              for any  damages arising from the use of this software.

              Permission is granted to anyone to use this software for any
              purpose, including commercial applications, and to alter it
              and redistribute it freely, subject to the following
              restrictions:

              1. The origin of this software must not be misrepresented,
                 you must not claim that you wrote the original software.
                 If you use this software in a product, an acknowledgment
                 in the product documentation would be appreciated but is
                 not required.

              2. Altered source versions must be plainly marked as such, and
                 must not be misrepresented as being the original software.

              3. This notice may not be removed or altered from any source
                 distribution.
Updates:
Sep 25, 1998  V1.10  Added RasGetIPAddress. Thanks to Jan Tomasek
              <xtomasej@fel.cvut.cz> for his help.


 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit RasDial1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IniFiles, Ras, FormAuto, LogMsg, ExtCtrls, Menus, ShellApi;


const
  WM_AUTOCONNECT = WM_USER + 1;

type
  TRasDialerForm = class(TAutoForm)
    ConnectButton: TButton;
    InfoListBox: TListBox;
    CancelButton: TButton;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    PasswordEdit: TEdit;
    SavePWCheckBox: TCheckBox;
    CheckTimer: TTimer;
    EntryNameComboBox: TComboBox;
    UserNameComboBox: TComboBox;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    MnuQuit: TMenuItem;
    MnuConnect: TMenuItem;
    MnuProperties: TMenuItem;
    MnuNew: TMenuItem;
    MnuAbout: TMenuItem;
    MnuCancel: TMenuItem;
    N1: TMenuItem;
    MnuOptions: TMenuItem;
    MnuConfigure: TMenuItem;
    RunInternetBrowser1: TMenuItem;
    RunInternetMailReader1: TMenuItem;
    RunInternetNewsReader1: TMenuItem;
    N2: TMenuItem;
    DurationLabel: TLabel;
    MnuClearduration: TMenuItem;
    TimeRestrictions1: TMenuItem;
    IPButton: TButton;
    procedure ConnectButtonClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure CancelButtonClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure CheckTimerTimer(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure EntryNameComboBoxChange(Sender: TObject);
    procedure UserNameComboBoxChange(Sender: TObject);
    procedure MnuQuitClick(Sender: TObject);
    procedure MnuConnectClick(Sender: TObject);
    procedure MnuPropertiesClick(Sender: TObject);
    procedure MnuNewClick(Sender: TObject);
    procedure MnuAboutClick(Sender: TObject);
    procedure MnuCancelClick(Sender: TObject);
    procedure MnuConfigureClick(Sender: TObject);
    procedure RunInternetNewsReader1Click(Sender: TObject);
    procedure RunInternetBrowser1Click(Sender: TObject);
    procedure RunInternetMailReader1Click(Sender: TObject);
    procedure MnuCleardurationClick(Sender: TObject);
    procedure TimeRestrictions1Click(Sender: TObject);
    procedure IPButtonClick(Sender: TObject);
  private
    { Private declarations }
    hRasConn        : THRASCONN;
    ConnectTime     : DWORD;
    aRasConn        : array [0..10] of TRASCONN;
    nRasConnCount   : DWORD;
    DialingServer   : String;
    DialingUserName : String;
    DialingPassword : String;
    procedure WMAutoConnect(var Msg : TMessage); message WM_AUTOCONNECT;
    procedure Connected;
    procedure Disconnected;
    procedure GetActiveConn;
    procedure DisplayActiveConn;
    function  GetActiveConnHandle(szName : String) : THRASCONN;
    procedure SaveSettings;
    procedure LoadPhoneBook;
    procedure SelectPhoneBookEntry(EntryName : String);
    procedure GetUserNameList;
    procedure GetUserPassword;
    procedure GetDuration;
    function  GetKeyDuration : String;
    procedure DoConnect;
    procedure DoDuration;
    procedure DoProperties;
    procedure DoNew;
    procedure DoCancel;
    procedure DoConfigure;
  public
    { Public declarations }
    procedure Dial(EntryName, UserName, Password : String);
    procedure WndProc(var Msg: TMessage); override;
    procedure LogMessage(Msg : String);
  end;

var
  RasDialerForm: TRasDialerForm;
  Log:           TLogMsg;

implementation

uses
    RasDial2, RasDial3, RasDial4;

{$R *.DFM}
const
    ProgName = 'RasDial';
    ProgVer  = 'V1.11';
    
function Encrypt(S : String) : String; forward;
function CrunchName(S : String) : String; forward;

var
  g_hWnd: HWND;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.WMAutoConnect(var Msg : TMessage);
begin
    LogMessage('AutoConnect on startup');
    DoConnect;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.LogMessage(Msg : String);
begin
    Log.Text('!', Msg);
    InfoListBox.Items.Add(Msg);
    InfoListBox.Refresh;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure RasDialFunc(unMsg : DWORD;
                      RasConnState : TRASCONNSTATE;
                      dwError : DWORD); stdcall;
begin
    PostMessage(g_hWnd,
                WM_RASDIALEVENT,
                RasConnState,
                dwError);
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.Connected;
begin
    ConnectTime          := GetTickCount;
    CancelButton.Caption := '&Disconnect';
    CheckTimer.Enabled   := TRUE;
    Caption              := ProgName + ' - Connected';
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.Disconnected;
begin
    if hRasConn <> 0 then begin
        RasHangUpA(hRasConn);
        hRasConn          := 0;
    end;
    CancelButton.Enabled  := FALSE;
    ConnectButton.Enabled := TRUE;
    CancelButton.Caption  := 'Ca&ncel';
    CheckTimer.Enabled    := FALSE;
    ConnectTime           := 0;
    Caption               := ProgName;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.WndProc(var Msg: TMessage);
var
    Buf      : array [0..255] of Char;
begin
    if Msg.Msg <> WM_RASDIALEVENT then begin
        inherited WndProc(Msg);
        Exit;
    end;
    LogMessage(RasConnectionStateToString(Msg.wParam));
    if Msg.wParam = RASCS_Connected then begin
        Connected;
        ConfigureAutoForm.ExecuteMailAuto;
        ConfigureAutoForm.ExecuteNewsAuto;
        ConfigureAutoForm.ExecuteBrowserAuto;
    end
    else if Msg.wParam = RASCS_Disconnected then begin
        RasGetErrorStringA(Msg.lParam, @Buf[0], SizeOf(Buf));
        LogMessage(Buf);
        Disconnected;
    end;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function TRasDialerForm.GetKeyDuration : String;
begin
    if ConfigureAutoForm.MonthlyDuration then
        Result := 'DurationFor' + FormatDateTime('mm', Date)
    else
        Result := 'Duration';
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.MnuCleardurationClick(Sender: TObject);
var
    Duration    : Integer;
    IniFile     : TIniFile;
    Section     : String;
    Key         : String;
begin
    if Application.MessageBox('Clear cumulated duration ?', 'Warning',
                              mb_YESNO + mb_DEFBUTTON2) <> IDYES then
        Exit;

    Key     := GetKeyDuration;
    IniFile := TIniFile.Create(FIniFileName);
    Section := 'RAS_ENTRY_' + CrunchName(EntryNameComboBox.Text);
    Duration := IniFile.ReadInteger(Section, Key, 0);
    LogMessage(Key + '''' +
               EntryNameComboBox.Text + ''' cleared. It was ' +
               IntToStr(Duration) + ' Sec');
    IniFile.WriteInteger(Section, Key, 0);
    IniFile.Free;
    GetDuration;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.GetDuration;
var
    Duration    : TDateTime;
    IniFile     : TIniFile;
    Section     : String;
    Key         : String;
begin
    Key     := GetKeyDuration;
    IniFile := TIniFile.Create(FIniFileName);
    Section := 'RAS_ENTRY_' + CrunchName(EntryNameComboBox.Text);
    Duration := IniFile.ReadInteger(Section, Key, 0) / 24 / 3600;
    if Duration >= 2.0 then
        DurationLabel.Caption := IntToStr(Trunc(Duration)) + ' days '
    else if Duration >= 1.0 then
        DurationLabel.Caption := IntToStr(Trunc(Duration)) + ' day '
    else
        DurationLabel.Caption := '';

    DurationLabel.Caption := 'Total: ' + DurationLabel.Caption +
                             TimeToStr(Frac(Duration));
    IniFile.Free;
end;


{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TRasDialerForm.DoDuration;
var
    Duration    : Integer;
    OldDuration : Integer;
    IniFile     : TIniFile;
    Section     : String;
    Key         : String;
begin
    if (ConnectTime = 0) or (ConnectTime = $FFFFFFFF) then
        Exit;

    Key      := GetKeyDuration;
    Duration := (GetTickCount - ConnectTime) div 1000;
    LogMessage(Key + ': ' + IntToStr(Duration) +
               ' Sec with ' + DialingServer);
    ConnectTime := 0;

    IniFile := TIniFile.Create(FIniFileName);
    Section := 'RAS_ENTRY_' + CrunchName(DialingServer);
    OldDuration := IniFile.ReadInteger(Section, Key, 0);
    IniFile.WriteInteger(Section, Key, OldDuration + Duration);
    LogMessage('Cumulated ' + Key + ': ' +
               IntToStr(OldDuration + Duration) + ' Sec');
    IniFile.Free;
    GetDuration;
end;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -