How to automatically run ADSL dial when starting the machine (2)

zhaozj2021-02-17  48

The automatic dialing program is done, the next task is how to build a service running when booting, and the network Unit unit1 is detected.

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);

Function TautialService.getServiceController: TServiceController; Begin Result: = ServiceController;

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 dword: 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 BAGIN RASGETERRORSTRINGA (DWRET, @BUF [0], SIZEOF (BUF)); LogMessage (BUF); 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;

Function ChildThrd (P: POINTER): longint; stdcall; // Time detection network connection is normal VAR Threadid: integer; begin result: = 0; threadid: = tchildparam (p ^). trdid; while true do begin if run kilin If not checkconn (entryName) THEN BEGIN WINEXEC (Pchar (DirPath 'Autodial.exe'), SW_SHOW); // Run the previously made dialing program end; SLEEP (RetryInterVal); end; end; dispose (p);

Procedure tautodialService.ServicePause; var paused: boolean; begin run: = false; paused: = true;

Procedure tautodialService.ServiceContinue (Sender: TService; Var Continue); Begin Run: = true; Continue;

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', ''); retrienterval: = 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.

Ok, now you can automatically dial it when you follow the machine.

Source code download

http://ono.3322.org

转载请注明原文地址:https://www.9cbs.com/read-30258.html

New Post(0)