Solved

detect logoff (delphi 7)

Posted on 2004-04-21
10
947 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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