Run a Service?

Posted on 2006-04-10
Last Modified: 2010-04-04
Hi guys, I have an application which I wanted to have the ability to run as a service, I have accomplished this by creating a new Service Application and then adding my units/forms to this app, all works great as a service now, BUT and there is always a but isn't there?? now I would like it to also run as a normal app when double clicked, is it possible to have it both ways? I would hate to have to maintain 2 seperate copies of it, want to be able to run it normally as a windows app, but if people want to use it often and when pc is not logged on then I want them to have the ability also to run /install to install it as service.

I realise I will have to add some intelligence to it so it knows if it is running as a service or not when closing (disable the forms close button as service should be stopped instead etc), current problem is if I run the application (not by starting service) it opens and then closes straight away, I think the service part is causing this as I guess it thinks it has nothing to do :/

I currently have the following in the Services OnExecute event..

 while not Terminated do

which seems to keep it alive until stopped when run as a service, why wont this do the same when just run as normal?

Please let me know if you want me to clarify anything, I am sure I am doing something pretty stupid here, not that familiar with Services im afraid.

Thanks in advance!

Question by:JustinWillis
    LVL 17

    Accepted Solution

    Basically you need to edit the dpr (project-view source) and add some checking logic to see if it is being run as a service

    here's some source from a demo. let me know if you require the whole thing

    Sample of how to integrate a service and a normal applications in one executable.
    When started (either as service or as normal app), the datamodule is created and the
    program will beep every 5 seconds.

    Created by: Bas Gooijen (

      Use it at your own risk, it could contain bugs.

      Freeware for all use

      Feb 16, 2002: Created.

    If you found any bugs, have comments, or need help, just mail me.
    program SampleService;

      ServiceUnit in 'ServiceUnit.pas' {SampleSvc: TService},
      FormUnit in 'FormUnit.pas' {Form2},
      DataModuleUnit in 'DataModuleUnit.pas' {DataModule1: TDataModule};

    {$R *.RES}

    function GetUserName: string;
      buff: array[byte] of char;
      sizeNeeded: dword;
      sizeNeeded := sizeof( buff ) - 1;
      windows.GetUserName( buff, sizeNeeded ) ;
      result := copy( buff, 1, sizeNeeded ) ;

    function NeedToRunAsService: boolean;
    var a:integer;cmd:string;
    begin // this could be implemented in several ways, eg. check for a parameter with ParamStr().
      result := ( uppercase( GetUserName ) = 'SYSTEM' ) ;

      if (ansipos('INSTALL',cmd)<>0) or
         (ansipos('UNINSTALL',cmd)<>0) then
           result:=true;   // we are installing or uninstalling the service

      if NeedToRunAsService then
      begin  // we need the service
        SvcMgr.Application.CreateForm(TSampleSvc, SampleSvc);
      begin // the form needs to be created
        forms.Application.CreateForm( TForm2, Form2 ) ;
    LVL 1

    Author Comment

    Brilliant, works great, of course to stop the service closing the app just don't create it! love it.

    Thanks for your help.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    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…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    737 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

    17 Experts available now in Live!

    Get 1:1 Help Now