WINDOWS service program development

WINDOWS service program development

Development steps:

  1、New->Other->Service Application

  2, is now serving a framework program has to take up, open Service1 window, there are several attributes to explain:

  AllowPause: whether to allow suspended

  AllowStop: whether to allow the stop

  Dependencies: dependencies, start the service settings of the service is dependent on a service or group

  DisplayName: In the name of "service" window displays

  When set to true and you can: Interactive Windows for the desktop interaction, if we want to display the form in the service, then this setting must be set to true, another must stWin32 ServiceType

  Password: password

  StartType: start-up mode

  3, if we want the service to interact with the form, follow these steps:

  Create a new form in the project fmMain

  Then write code in Service1 in the OnStart

  procedure TService1.ServiceStart (Sender: TService; var Started: Boolean);

  begin

  Started := True;

  Svcmgr.Application.CreateForm(TFmMain, fmMain);

  FmMain.show;

  end;

  OnStop code

  procedure TService1.ServiceStop (Sender: TService; var Stopped: Boolean);

  begin

  Stopped := True;

  FmMain.Free;

  end;

  This will show that the new form when the service starts

  4, after the completion of the translation, we can install the service, the installation method:

  Performing appname / install cmd in the window,

  如F:\Book\DService\Project1.exe /install

  So that the service installation is complete

  5. Similarly, when you delete command is entered in the cmd window appname / uninstall

  如F:\Book\DService\Project1.exe /uninstall

  About other:

  1, about debugging service program

  If we develop the service has multiple forms, the debugger is undoubtedly a big problem

  In fact, the service program will be able to change into a bit of a standard Win32 project, in order to prevent constantly come and go, we can add a condition to compile, to switch generates a service program or a normal executable program by compiling conditions, assumptions compilation conditions NormalApp, in the following places need to be compiled with conditions

  Project file, a reference unit

  {$IFDEF NormalApp}

  Forms,

  {$ELSE}

  SvcMgr,

  {$ENDIF}

  Project Initialization

  {$IFDEF NormalApp}

  Application.Initialize;

  Application.CreateForm(TFmMain, FmMain);

  Application.Run;

  {$ELSE}

  if not Application.DelayInitialize or Application.Installing then

  Application.Initialize;

  Application.CreateForm(TService1, Service1);

  Application.Run;

  {$ENDIF}

  This way we can / delete conditions NormalApp compiled by adding to switch service procedures and the general program of the window

Guess you like

Origin www.cnblogs.com/hnxxcxg/p/11204839.html