Salut tout le monde,
voila mon probleme jai creé un service avec Delphi7(voir Code), puis je lai installer avec la commande: c:\service\ping.exe -install, ca a bien marcher linstallation, le probleme c qu'il ne ce passe rien du tout, le progi doi faire juste un ping a google toutes les 30 secondes et ecrire le resultat ds un fichier log, jai aucune ideé pourquoi ca ne marche pas??
tout ideé est la bien venue,
merci
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
 
unit PingCheck;
 
interface
 
uses
  Windows, Messages, SysUtils,
  Classes, Graphics, Controls,
  SvcMgr, Dialogs, IdIcmpClient,
  ExtCtrls, iniFiles;
 
type
  TServicePingCheck = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
  private
    { Private-Deklarationen }
  public
    function GetServiceController: TServiceController; override;
    procedure TimerEvent(Sender: TObject);
    { Public-Deklarationen }
  end;
 
var
  ServicePingCheck: TServicePingCheck;
 
implementation
 
{$R *.DFM}
 
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  ServicePingCheck.Controller(CtrlCode);
end;
 
function TServicePingCheck.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;
 
Function GetInterVal: Integer ;
Var
  Ini: TIniFile ;
  StrFile: String;
Begin
  StrFile:= ExtractFileName(ParamStr(0))+'Config.ini';
  Ini:= TIniFile.Create(StrFile) ;
  Try
    Result:= Ini.ReadInteger('Timer', 'interval', 30)*1000;
  Finally
    FreeAndNil(Ini);
  End ;
End ;
 
{*******************************************************************************
  Ping a Sever
*******************************************************************************}
Function MyPing2(StrServer: String): Boolean ;
var
  i : Integer;
  icmp : TidIcmpClient ;
  S: String;
begin
  Result :=False;
  icmp := TidIcmpClient.Create(nil) ;
  try
    try
      ICMP.Host := StrServer;
      ICMP.Ping;
      S:= icmp.ReplyData ;
      if s<>'' Then
        Result:= True ;
    except
   end;
  finally
    FreeAndNil(icmp);
  end ;
end ;
 
Function GetPingDestination: String;
Var
  Ini: TIniFile ;
  StrFile: String;
Begin
  StrFile:= ExtractFileName(ParamStr(0))+'Config.ini';
  Ini:= TIniFile.Create(StrFile) ;
  Try
    Result:= Ini.ReadString('Server', 'IPAddress', 'www.google.de');
  Finally
    FreeAndNil(Ini);
  End ;
End ;
 
{*******************************************************************************
  Log File
*******************************************************************************}
procedure Log(Action, comment: String);
var
  LogText, Str : String   ;
  LogFile : TextFile ;
begin
  LogText := DateTimeToStr(NOW)+#9+Action+':'+#9+comment ;
  Str:= 'c:\programme\service\Log.txt';
  AssignFile (LogFile, Str);
  {$I-}
  if FileExists(Str) Then
    Append (LogFile)
  Else
    Rewrite(LogFile);
  Try
    WriteLn (LogFile, LogText);
  Finally
    CloseFile (LogFile);
  End ;
  {$I+}
end;
 
{*******************************************************************************
  Timer Event
*******************************************************************************}
procedure TServicePingCheck.TimerEvent(Sender: TObject);
const
{$J+}
   counter : Integer = 0; 
{$J-}
Var
  ip: String;
begin
  inc(counter);
  IP:= GetPingDestination;
  Try
    MyPing2(Ip);
    Log('Ping Okay','Destination IP: '+IP+', Counter: '+inttostr(counter));
  Except on E:Exception do
    Log('ERROR Ping','Destination IP: '+IP+', Counter: '+inttostr(counter)+', '+e.Message);
  End ;
end;
 
procedure TServicePingCheck.ServiceStart(Sender: TService; var Started: Boolean);
Var
  Timer: TTimer;
begin
  Timer:= TTimer.Create(Self);
  Timer.Enabled:= False;
  Timer.Interval:= GetInterVal ;
  Timer.OnTimer:= TimerEvent ;
  Timer.Enabled:= True ;
  Started:= True ;
end;
 
end.