Solved

Event-handler for a service application

Posted on 2002-07-24
3
261 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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