Solved

Event-handler for a service application

Posted on 2002-07-24
3
259 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process Javascript errors with Delphi TWebBrowser 1 136
Delphi - replicating a form 8 74
Delphi Form ownership 4 90
control image tags in a string ? 12 131
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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