Link to home
Start Free TrialLog in
Avatar of lobo_estepario
lobo_estepario

asked on

detect logoff (delphi 7)

hi i just wanna know how can a program detect the computer is going to be turned off. i want my program to make its registry key just when it knows the user is going to turn off the computer because i don't want users to see the key or delete it.
thanks.
Avatar of shaneholmes
shaneholmes

you should handle the following message to your form

  TForm1 = class(TForm)
    ...
    procedure EndSMsg(var T : TMessage);message  WM_QUERYENDSESSION;
    ...
  end;
.
procedure TForm1.EndSMsg(var T : TMessage);
begin
  // if you want to cancel logoff or shutdown, return false
  T.result := true;
  if T.lparam <> ENDSESSION_LOGOFF then
  begin
     //shutting down
  end else
  begin
     //login off
  end;
end;


Shane
Also You for sure be in this place before logoff in Your applications main form.

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  Application.MessageBox('a','b',MB_OK);
end;
mokule:

That will also run simply when the program is closed.  And actually, I had problems with my program not saving it's settings when the computer was shut down while my program was still running, so I don't think the normal OnCloseQuery/OnClose/OnDestory/etc.. events get run when the computer shuts down.  Capturing the WM_QUERYENDSESSION message as shaneholmes suggests was the only guaranteed way I found.

I don't want to argue, but to tidy things.

OnCloseQuery/OnClose/OnDestory are fired when computer shuts down
I don't want to argue either, but are you just assuming they do, or do you know for a fact?  

In my application I had it save it's settings in the onDestroy event, and if my computer was shut down while it was running, any settings changed while the application was running were lost.  Maybe the problem is specific to the version of Delphi I was using at the time, or to the version of Windows I used.  But I can say for sure that in my situation, they were not triggered.  

Just for curiosity's sake, I'll have to test again when I get home.  I use a different version of both Delphi and Windows now, so I'll see how they do.
I've tested OnCloseQuery - others assuming.

BTW
Isn't it better to save settings at the moment they are changed?
Tested on Delphi 7 and Win XP
Actually in my case it was a log file not some settings, which is why it only saved to disk when the program terminated.  And I'm also using D7+XP now, so I guess I'll forego the testing.
Avatar of lobo_estepario

ASKER

well maybe there is something i'm doing wrong because it gives me an error.
the code:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
 

       procedure EndSMsg(var T : TMessage);message  WM_QUERYENDSESSION;

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.EndSMsg(var T : TMessage);
begin

  T.result := true;
  if T.lparam <> ENDSESSION_LOGOFF then
  begin
  ShowMessage('no me apagues infeliz');
  end else
  begin
  ShowMessage('no me apagues infeliz');
  end;
end;

end.




as u can see i'm using the same code shaneholmes put in the example above, what's wrong? do i have to add something to uses or what?
it gives me two errors:
[Error]Unit1.pas(31): Incompatible types: 'Integer' and 'Boolean' ------> (1st line of the TForm1.EndSMsg procedure)
[Warning]Unit1.pas(32): Comparison always evaluates to True ------> (2nd line of the TForm1.EndSMsg procedure)

waiting for ur replies
ASKER CERTIFIED SOLUTION
Avatar of mokule
mokule
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial