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!
LVL 3
raidosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.