Solved

detect logoff (delphi 7)

Posted on 2004-04-21
10
911 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 94
Convert Jpg /PNG To GIF 5 120
Connection between libmysql.dll and MySQL Versions 7 87
TEMBEDDEDWB how can i change its user agent ? 8 47
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

23 Experts available now in Live!

Get 1:1 Help Now