• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 290
  • Last Modified:

Ignoring and handling messages

My problem is that i would
really like to run a
service like application
under win95/98 that starts right before
logon and doesn't stop running until
you reboot or shutdown the machine.
Ie it needs to handle som Wm_messages:
like Wm_endsession, Wm_QueryEndsession
and not close at these messages that
occur while logging out.

The starting of the application is easy,
it's the ignorance problem that seems to
be impossible

Please help!
0
raidos
Asked:
raidos
1 Solution
 
RBertoraCommented:
Listening..
0
 
DrDelphiCommented:
if you mean that you want to be able to trap these messages, do something and then allow the message to pass through, you might want to do something like this:


Procedure WindowsIsClosing(var msg:Tmessage);message WM_QueryEndsession;



Procedure WindowsIsCLosing(var msg:Tmessage);
begin
   //// do whatever you are looking to do here;

msg.result:=ord(true);/// return the msg back

end;



0
 
karouriCommented:
Ok,raidos.
As for the starting of the program it can be in the startup menu or in the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, anyhow you said you'll handle that.

As for the handling of the logging out, the message WM_QUERYENDSESSION has a parameter that can tell you if it's a logoff or a shutdown.
The following code prevents logoff while enabling shutdown:

unit kkk;

interface

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

type
  TQESMsg= packed record
    dummy:integer;
    nSource:cardinal;
    fLogOff:cardinal;
    result:integer;
  end;
  TForm1 = class(TForm)
  private
    { Private declarations }
  public
    { Public declarations }
    procedure OnShutdown(var Msg:TQESMsg);message WM_QUERYENDSESSION;
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

{ TForm1 }

procedure TForm1.OnShutdown(var Msg: TQESMsg);
begin
     if (Msg.fLogOff=ENDSESSION_LOGOFF) then
           {do some excellent thing}
           Msg.result:=1{logout}
     else
           Msg.result:=0;{don't shut down}
end;

end.

By the way DrDelphi this was meant as a proposed answer but things go fast here in EE.I read a question and before I try to answer I find someone RBerota,simonet,DrDelphi,etc so I am not able to get a single expert point, however, let this contribution be free in the true spirit of EE
0
 
raidosAuthor Commented:
Well, the proposed answer didn't exactly do what i asked for.

It application needs to keep running until the machine actually reboots or is shutdown.

And for karouris answer, that will actually prevent the user from logging of, wont it?

the application needs to allow people log off the system, but should still run.

0
 
MadshiCommented:
Use this:

function RegisterServiceProcess(processID: cardinal; flags: integer) : integer; stdcall;
    external kernel32;
const RSP_SIMPLE_SERVICE     = 1;
      RSP_UNREGISTER_SERVICE = 0;

RegisterServiceProcess(0, RSP_SIMPLE_SERVICE);

Now your app won't be shutdown by Windows. The problem is, that Delphi itself stops our program when receiving a WM_ENDSESSION message. That's a Delphi bug, that we have to cure. Otherwise your goal can't be realized.
To fix that bug, call

Application.HookMainWindow(Form1.MsgHook);

And this Form1.MsgHook functions must look like this:

function TForm1.MsgHook(var Msg: TMessage) : boolean;
begin
  result := (Msg.Msg = WM_ENDSESSION) and (Msg.lparam <> 0);
end;

Regards, Madshi.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now