Solved

detect logoff (delphi 7)

Posted on 2004-04-21
10
904 Views
Last Modified: 2007-12-19
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.
0
Comment
Question by:lobo_estepario
10 Comments
 
LVL 11

Expert Comment

by:shaneholmes
ID: 10879810
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
0
 
LVL 17

Expert Comment

by:mokule
ID: 10879997
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;
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 10881101
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.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10881368

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

OnCloseQuery/OnClose/OnDestory are fired when computer shuts down
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 10881480
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:mokule
ID: 10881641
I've tested OnCloseQuery - others assuming.

BTW
Isn't it better to save settings at the moment they are changed?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10881682
Tested on Delphi 7 and Win XP
0
 
LVL 6

Expert Comment

by:MannSoft
ID: 10881747
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.
0
 

Author Comment

by:lobo_estepario
ID: 10915083
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
0
 
LVL 17

Accepted Solution

by:
mokule earned 160 total points
ID: 10915903
procedure TForm1.EndSMsg(var T : TMessage);
begin
  T.result := 1;
  if T.lparam = 0 then
    begin
    ShowMessage('System is shutting down');
    end
  else if (DWORD(T.lparam) and ENDSESSION_LOGOFF) =
                                 ENDSESSION_LOGOFF then
    begin
    ShowMessage('User is logging off');
    end;
end;
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now