Solved

Event-handler for a service application

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

789 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