Bonjour,

On me demande de développer un service, qui vise à logguer l'extinction d'un ordinateur.
Mon problème provient de cette action "sous win2000, quand l'utilisateur appuie sur le bouton power (marche\arret) du pc, celui ci s'éteint dessuite.
Or je n'arrive pas à logguer cette action.

Voici mon code

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
 
unit Unit1;
 
interface
 
uses
    Windows
  , Messages
  , SysUtils
  , Classes
  , Controls
  , SvcMgr      // TService
  ;
 
type
  TSrvUpTime = class(TService)
    procedure ServiceCreate(Sender: TObject);
    procedure ServiceDestroy(Sender: TObject);
    procedure ServiceShutdown(Sender: TService);
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceStop(Sender: TService; var Stopped: Boolean);
    procedure ServiceContinue(Sender: TService; var Continued: Boolean);
  private
    { Déclarations privées }
    LogHandle : THandle;
    Procedure WMEndSession(var Msg : TWMEndSession); message WM_ENDSESSION;
    procedure WMPowerBroadcast(var Msg: TMessage); message WM_POWERBROADCAST;
  public
    function GetServiceController: TServiceController; override;
    { Déclarations publiques }
  end;
 
 
 
var
  SrvUpTime: TSrvUpTime;
 
 
implementation
 
{$R *.DFM}
 
 
const
  DebugLog : PChar = 'C:\uptime.txt';
  ServiceName : PChar = 'UpTime';
 
  PBT_APMQUERYSUSPEND             = 0000;
  PBT_APMQUERYSTANDBY             = 0001;
  PBT_APMQUERYSUSPENDFAILED       = 0002;
  PBT_APMQUERYSTANDBYFAILED       = 0003;
 
  PBT_APMSUSPEND                  = 0004;
  PBT_APMSTANDBY                  = 0005;
  PBT_APMRESUMECRITICAL           = 0006;
  PBT_APMRESUMESUSPEND            = 0007;
  PBT_APMRESUMESTANDBY            = 0008;
  PBTF_APMRESUMEFROMFAILURE       = 00000001;
 
  PBT_APMBATTERYLOW               = 0009;
  PBT_APMPOWERSTATUSCHANGE        = $000A;
  PBT_APMOEMEVENT                 = $000B;
  PBT_APMRESUMEAUTOMATIC          = $0012;
 
 
procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  SrvUpTime.Controller(CtrlCode);
end;
 
function TSrvUpTime.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;
 
 
Procedure TSrvUpTime.WMEndSession(var Msg : TWMEndSession);
var
  strdate : string;
Begin
  if Msg.EndSession then Begin
    if Self.LogHandle<>INVALID_HANDLE_VALUE then Begin
      strDate := DateTimeToStr(Now) + ' - Fermeture de la session'+ #13#10;
      FileWrite(Self.LogHandle, strdate[1], Length(strdate));
    End;
  End;
  inherited;
End;
 
 
procedure TSrvUpTime.WMPowerBroadcast(var Msg: TMessage);
var
  strdate : string;
Begin
//  if (Msg.wParam = PBT_APMQUERYSUSPEND) or (Msg.wParam = PBT_APMSUSPEND) then begin
    strDate := DateTimeToStr(Now) + ' - SUSPEND'+ #13#10;
    FileWrite(Self.LogHandle, strdate[1], Length(strdate));
//  End;
  inherited;  
End;
 
 
 
procedure TSrvUpTime.ServiceCreate(Sender: TObject);
begin
  Self.DisplayName := ServiceName;
 
  Self.LogHandle := INVALID_HANDLE_VALUE;
  Self.LogHandle := CreateFile(DebugLog, GENERIC_WRITE, FILE_SHARE_READ, nil, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
 
  if Self.LogHandle=INVALID_HANDLE_VALUE then Begin
    MessageBox(0, 'Impossible de créer le Handle sur le fichier de Log', ServiceName, MB_OK or MB_ICONERROR);
    Exit;
  End;
 
  FileSeek(Self.LogHandle,0,2);
end;
 
procedure TSrvUpTime.ServiceDestroy(Sender: TObject);
var
  strdate : string;
begin
  if Self.LogHandle=INVALID_HANDLE_VALUE then Exit;
 
  strDate := DateTimeToStr(Now) + ' - Destruction du service'+ #13#10;
  FileWrite(Self.LogHandle, strdate[1], Length(strdate));
 
  CloseHandle(Self.LogHandle);
end;
 
procedure TSrvUpTime.ServiceShutdown(Sender: TService);
var
  strdate : string;
Begin
  if Self.LogHandle<>INVALID_HANDLE_VALUE then Begin
    strDate := DateTimeToStr(Now) + ' - Shutdown du service'+ #13#10;
    FileWrite(Self.LogHandle, strdate[1], Length(strdate));
  End;
end;
 
procedure TSrvUpTime.ServiceStart(Sender: TService; var Started: Boolean);
var
  strDate : string;
begin
  strDate := DateTimeToStr(Now) + ' - Démarrage'+ #13#10;
  FileWrite(Self.LogHandle, strdate[1], Length(strdate));
end;
 
procedure TSrvUpTime.ServiceStop(Sender: TService; var Stopped: Boolean);
var
  strDate : string;
begin
  strDate := DateTimeToStr(Now) + ' - Arrêt'+ #13#10;
  FileWrite(Self.LogHandle, strdate[1], Length(strdate));
end;
 
procedure TSrvUpTime.ServiceContinue(Sender: TService; var Continued: Boolean);
var
  strDate : string;
begin
  strDate := DateTimeToStr(Now) + ' - Reprise (sortie de pause)'+ #13#10;
  FileWrite(Self.LogHandle, strdate[1], Length(strdate));
end;
 
end.
Si quelqu'un a une idée, ou des suggestions, ce serait sympa.

Merci d'avance