Détecter quand un utilisateur appuie sur le bouton Power (marche\arret) du pc

Millenod Messages postés 34 Date d'inscription mardi 29 avril 2003 Statut Membre Dernière intervention 21 septembre 2005 - 21 sept. 2005 à 10:11
Millenod Messages postés 34 Date d'inscription mardi 29 avril 2003 Statut Membre Dernière intervention 21 septembre 2005 - 21 sept. 2005 à 10:20
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 :

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&#233;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&#233;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&#234;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

1 réponse

Millenod Messages postés 34 Date d'inscription mardi 29 avril 2003 Statut Membre Dernière intervention 21 septembre 2005
21 sept. 2005 à 10:20
Je suis désolé pour la presentation :S
je n'arrive pas à éditer mon post :S

j'ai aussi posté mon message sur developpez.net : http://www.developpez.net/forums/viewtopic.php?t=397890
le texte est formaté correctement sur ce lien la

encore navré :(
0
Rejoignez-nous