Tech Notes
Tech Notes |
How to design WebHub apps that can run as a Windows service using TtpProject
Checklist for WebHub applications that should eventually/sometimes run as a Windows service
Documented for WebHub v2.1+- In the DPR, make sure that MultiTypeApp is the first unit listed in the uses clause and that Forms is not listed at all.
- This step applies if and only if you want to compile with packages, and in particular with the WebHub RT (Run Time) package. In that case, you must edit your DPR and add an extra unit to the uses clause. The unit to add is:
MultiTypeAppSvcand you do have PAS source for that in your webhub\lib folder. If you fail to include that unit, you will not be able to install your service. - Use a TtpProject component. The easiest way to do this is to use the WebHub new-project wizard to initially create your project.
- Check and make sure that you do NOT have the compiler directive
PREVENTSVCMGRbecause that prevents the Windows service manager from being compiled into your application. - In , add a custom version string named
ServiceNameand set it equal to something reasonable for your application. - Use to rebuild your project such that any changes to compiler directives are taken into consideration.
- Remember that you have to install your service before it can be started and stopped.
mywhapp.exe -install net start myservicename net stop myservicename
- You can also start your application as a normal Windows app, e.g.
start mywhapp.exe /ID=showcase
- To remove the service, call
mywhapp.exe -uninstall
- If you want to prevent the service from running due to a fatal error condition
detected during startup, call
Application.PreventRun.
Customizing the TService object in Delphi
In a Delphi service application which does NOT use MultiTypeApp, you can use the Delphi Object Inspector to set properties of the TService, such as the startup mode and the dependencies. When working with the generic MultiTypeApp unit plus tpAppService.pas, the syntax is a bit more tricky (because we leave you the option of NOT compiling as a service).
Nevertheless, you can still customize all the properties of the TService. To do this, put code into the datamodule which contains TtpProject, in the OnCreate event handler. Here is an example:
// example contributed by Vseslav
// uses SvcMgr
procedure TDMSampleForWHProject.DataModuleCreate(Sender: TObject);
var
Service: TService;
i:integer;
begin
for i := 0 to SvcMgr.Application.ComponentCount - 1 do
if SvcMgr.Application.Components[i] is TService then
begin
Service := TService(SvcMgr.Application.Components[i]);
// ok to set properties now
// Service.Dependencies
end;
end;
Version-Specific Notes
For WebHub Components v1.000 to v2.097
Multiple instances of same EXE
In order to make the same EXE run as a service more than once on Windows, the service name must differ. It is possible to write a launcher, which launches N copies of a service, each with unique information as needed (www.totalweb.nl did this prior to 1999). The lower-tech solution is to copy the EXE onto N files with new names, e.g. myapp.exe becomes myapp1.exe, myapp2.exe, myapp3.exe. Those can then be installed, started, stopped or uninstalled as normal Windows services.
For WebHub Components v1.000 to v2.097
As-Service units
From about 1996 to 2008, HREF Tools provided a small group of as-service units (PAS files) which helped developers build WebHub applications that ran as a Windows service. The essential instruction was to make the DPR start like this:
//Example DPR syntax uses SvcApp in 'c:\ht\Lib\svc\Svcapp.pas', //Forms, NtObj in 'c:\ht\Lib\svc\NtObj.pas', SvcComp in 'c:\ht\Lib\svc\SvcComp.pas', SvcObj in 'c:\ht\Lib\svc\SvcObj.pas';
Further details were in webhub.hlp as shipped at that time. These units are no longer provided because they were superceded by TtpProject in November 2008.
The installation command was -install and the uninstall command was -remove.
Running DynHelp.exe v1.2.0.6 on WebHub-v2.125 built by D14
Calc time: 312 ms

