如何在启动机器时自动运行adsl拨号(2)

自动拨号的程序做完了,接下来的任务就是如何建一个服务在开机时运行,并定时检测网络
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,ras,IniFiles;

type
  TAutoDialService = class(TService)
    procedure ServicePause(Sender: TService; var Paused: Boolean);
    procedure ServiceContinue(Sender: TService; var Continued: Boolean);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;
type
  TChildParam=record
    TrdId:integer;
  end;
var
  AutoDialService: TAutoDialService;
  Run:boolean;
  DirPath,EntryName:string;
  aRasConn:array [0..10] of TRASCONN;
  nRasConnCount: DWORD;
  hRasConn:THRASCONN;
  RetryInterval:integer;
 
implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  AutoDialService.Controller(CtrlCode);
end;

function TAutoDialService.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure LogMessage(Msg:string);
var
  LogFile:TextFile;
begin
  AssignFile(LogFile,DirPath+'Log.txt');
  Append(LogFile);
  WriteLn(LogFile,DateTimeToStr(Now)+','+Msg);
  CloseFile(LogFile);
end;

procedure GetActiveConn;
var
    dwRet    : DWORD;
    nCB      : DWORD;
    Buf      : array [0..255] of Char;
begin
    aRasConn[0].dwSize := SizeOf(aRasConn[0]);
    nCB   := SizeOf(aRasConn);
    dwRet := RasEnumConnectionsA(@aRasConn, @nCB, @nRasConnCount);
    if dwRet <> 0 then begin
        RasGetErrorStringA(dwRet, @Buf[0], SizeOf(Buf));
        LogMessage(Buf);
    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 ChildThrd(p:Pointer):LongInt;stdcall;//定时检测网络连接是否正常
var
  ThreadId:integer;
begin
  Result:=0;
  ThreadId:=TChildParam(p^).TrdId;
  while True do
  begin
    if Run then
    begin
      if not CheckConn(EntryName) then
      begin
        WinExec(PChar(DirPath+'AutoDial.exe'),SW_SHOW);//运行前面制作的的拨号程序
      end;
      sleep(RetryInterval);
    end;
  end;
  Dispose(p);
end;

procedure TAutoDialService.ServicePause(Sender: TService; var Paused: Boolean);
begin
  Run:=False;
  Paused:=True;
end;

procedure TAutoDialService.ServiceContinue(Sender: TService;
  var Continued: Boolean);
begin
  Run:=True;
  Continued:=True;
end;

procedure TAutoDialService.ServiceStart(Sender: TService;
  var Started: Boolean);
var
  hChildThread:Thandle;
  ChildThreadId:DWord;
  ChildParam:^TChildParam;
  f:TIniFile;
begin
  DirPath:=ExtractFilePath(ParamStr(0));
  f:=TiniFile.Create(DirPath+'conf.ini');
  EntryName:=f.ReadString('RasDial','EntryName','');
  RetryInterval:=f.ReadInteger('RasDial','Interval',0)*1000;
  f.Free;

  Run:=True;

  new(ChildParam);
  ChildParam^.TrdId:=2;
  hChildThread:=CreateThread(nil,0,@ChildThrd,ChildParam,0,ChildThreadID);
end;

end.

好了,现在只要重起一下机器后,就能自动拨号了

源码下载

http://ono.3322.org

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 1

打赏作者

ZhuJunfeng

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值