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
Solved

detect logoff (delphi 7)

Posted on 2004-04-21
10
932 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 104
delphi exception 7 68
error 1.1 400 Bad request idhttp delphi 18 147
Create Database on Android via Delphi dbExpress 3 113
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

828 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