4

I'm a little puzzled as to why, when you create a new service application in Delphi XE2, does it include these 3 visual component units?

Vcl.Controls
Vcl.Dialogs
Vcl.Graphics

As far as I know, there's nothing in these units which a Service would require. I can see the Graphics unit possibly being used for some sort of image processing, but that's a matter of a developer's implementation of it. Is there some reason why these units are automatically included in a new service application? If I remove them, it doesn't hurt anything... Or does it?

Charles
  • 50,943
  • 13
  • 104
  • 142
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 1
    Not everything in those is visual.... – Tony Hopkinson Feb 14 '12 at 18:09
  • @TonyHopkinson Understood, but I still don't see, for example, any reason to ever have to show a dialog, because services are invisible, and should never show any dialog. And there's nothing to draw, so why is graphics included? – Jerry Dodge Feb 14 '12 at 18:10
  • 1
    I have no idea, but there's only one way to find out, comment them! –  Feb 14 '12 at 18:13
  • Already have, as mentioned, I can remove them and nothing breaks. I'm just really puzzled as to why they would be included in the first place. – Jerry Dodge Feb 14 '12 at 18:14
  • +1 good question - 'nothing breaks' is not equal to 'nothing will break later' - if the units have initializations, and they have to run for some specific code which your test did not reach yet ... – mjn Feb 14 '12 at 18:24
  • 2
    Delete them. You don't need or want them. – David Heffernan Feb 14 '12 at 18:34
  • Nothing new here. These units are also included in the default service app for D6 and D2010. – David Heffernan Feb 14 '12 at 20:57
  • So adding a dialog can be done easily and then you can ask a question on here about how to get the logged on user to see it? – Tony Hopkinson Feb 14 '12 at 22:02
  • I don't intend on making any service interact with the user at all - I plan on making a separate tray icon app which interacts with the service through sockets - and it can be used from other computers as well through the same socket connection. – Jerry Dodge Feb 15 '12 at 02:07

1 Answers1

8

This is added by the IDE code generator, "just in case"... IDE mainly creates forms, so it will add it to your service module, even if there is no need of UI in your service.

What is funny, is that since Windows Vista/Seven, the services are not able any more to send GDI messages to the desktop, i.e. interact with it. So there is definitively not even a possibility to use dialogs nor UI controls from a Windows service.

In fact, even the SvcMgr.pas links to Forms.pas + Dialogs.pas units. So deleting the reference in your own unit will continue to have those units linked.

It appears that Forms.pas + Dialogs.pas units are needed by SvcMgr.pas to display some potential error message when the service is installed on the command line.

In fact, your service .exe is not just running in the background, as a service. It can also be run from the command line, like a regular application, in order to install/uninstall/start/stop the service.

You can take a look at our lighter implementation of Windows services in Delphi - but not the same features - just something to play with the APIs. This version does not link to Forms.pas nor Dialogs.pas units.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • *It appears that Forms.pas + Dialogs.pas units are needed by SvcMgr.pas to display some potential error message when the service is installed on the command line.* It's fine for SvcMgr to use them but there's no reason for the project template code to use them. Lots and lots of other units will be used without appearing in the project template. – David Heffernan Feb 14 '12 at 21:00
  • @DavidHeffernan Of course, there is no reason to use them in the project template - but, to be honest, link whole Dialogs and Forms units add a lot of unnecessary code to the service executable! So much for so little (even more with the new RTTI which makes those units not light at all, even if only one or two functions are needed). – Arnaud Bouchez Feb 14 '12 at 21:09
  • I guess a solution (which opposes a previous question of mine) is make a complete copy of SvcMgr, remove those units, and add only the code needed by SvcMgr directly into the unit. – Jerry Dodge Feb 14 '12 at 22:04
  • 1
    The `SvcMgr.pas` unit depends on the `Forms.pas` unit because `SvcMgr.TApplication` accesses `Forms.TApplication` internally. I do not know why `SvcMgr.pas` uses `Dialogs.pas`, though. – Remy Lebeau Feb 14 '12 at 23:30
  • @jerry Why are you worrying about this? It's feels annoying that so much wasted code is in the exe, but it won't get paged in to memory if it's never called so it doesn't actually matter. – David Heffernan Feb 15 '12 at 00:15
  • I'm asking because it was a very peculiar finding. My initial question had nothing to do with what might be declared within the SvcMgr unit, I was just wondering if there was some reason why those 3 units are automatically included in a new service, because they will most likely never be used by a service. That is all. – Jerry Dodge Feb 15 '12 at 00:33
  • @RemyLebeau-TeamB Speaking of which, it's a wonder why the Forms.pas unit hasn't been split into two for reasons like this - there should be a separate Applications.pas unit for this in my opinion. But this is opinion. – Jerry Dodge Feb 15 '12 at 00:35
  • @JerryDodge: feel free to submit a feature request to QC asking for that unit separation. – Remy Lebeau Feb 15 '12 at 02:00