Solved

Event-handler for a service application

Posted on 2002-07-24
3
256 Views
Last Modified: 2010-04-04
How to write an event-handler for a service application. Event = User Logoff and System Shutdown.

I want my service application to do something when a user logoff or shutdown the system.
0
Comment
Question by:mappemuppe
  • 2
3 Comments
 
LVL 2

Expert Comment

by:mullet_attack
ID: 7175890
use this api function

SetConsoleCtrlHandler
 
0
 
LVL 2

Accepted Solution

by:
mullet_attack earned 500 total points
ID: 7175997
this is the best i can do...

I expected to be able to differentiate between logoff and shutdown, but that is not the case. For both events the parameter passed in the HandlerEvent is CTRL_CLOSE_EVENT.
Hope it's close enuff for you purposes :-)



unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs;

type
  TATest = class(TService)
    procedure ServiceStart(Sender: TService; var Started: Boolean);
    procedure ServiceShutdown(Sender: TService);
  private
    { Private declarations }
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

function HandlerEvent(eType : DWORD):boolean;

var
  ATest: TATest;


implementation

{$R *.DFM}


function HandlerEvent(eType : DWORD):boolean;
begin
  ShowMessage(Format('eType %d',[eType]));
  result := true;
end;

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  ATest.Controller(CtrlCode);
end;

function TATest.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

procedure TATest.ServiceStart(Sender: TService; var Started: Boolean);
begin
  if SetConsoleCtrlHandler(@HandlerEvent, true) then showmessage('ok');
  Started := true;
end;

procedure TATest.ServiceShutdown(Sender: TService);
begin
  SetConsoleCtrlHandler(@HandlerEvent, false);
end;

end.
 
0
 

Author Comment

by:mappemuppe
ID: 7176679
Thanks that was just what I needed.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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