Can anyone shed some light on this, because I've tried to convert George Tokas's Net Communications port server (that just works fine as a normal application!) to a service and some variables IN THE SAME ROUTINE seem initialized while others obviously are NOT.

As I want an array of pointers to point to objects created with new I want to be absolutely sure that I'm using the right variables pointing to the correct objects ...... Uptill now I don't get this right obviously.

Maybe I'm not the right person to comment about services but I think that the OnCreate event is fired by the RunTimeLibrary which is linked to every project by the compiler to create the final executable, dll.
And since there is a call there every line is executed.
Anyway there was a warning at the article about the service...
There are 2 more bugs residing at server side of the article....
It was meant for this month to explain those but didn't make it...
One of the bugs have to do with a VERY SPECIAL case but has to be dealed.
Anyway both has to do with the TServerSocket::Socket->Connections[] array.

Thanks George, but as far as I am concerned, I do think I gathered some understanding of classes. That's thanks to your effort!

I'm looking forward to the article you mentioned.

Meanwhile I just moved the whole initialization into the ServiceExecute thread. I expanded the User class with a ServerSocketNumber property, and the Service class with a ServerSocketCount. It neatly sums up like this:
2007-03-28 15:59:19.396 Server socket #1 created
2007-03-28 15:59:19.406 Server socket #2 created
2007-03-28 15:59:19.406 Server socket #3 created
2007-03-28 15:59:19.406 Server socket #4 created
2007-03-28 15:59:19.406 Server socket #5 created
2007-03-28 15:59:19.406 Server socket #6 created
2007-03-28 15:59:19.417 Server socket #7 created
2007-03-28 15:59:19.417 Server socket #8 created
2007-03-28 15:59:20.178 Service opened

But then another thing arose, the Terminated value in the ServiceExecute eventhandler will not change to false. Thus the service will not finish up neatly.

Hi again,
The Terminated flag (boolean, in the past we called it a flag) is changing status when the service terminates. It is a flag to indicate that the service ended and so everything has to clean up.

TService is a descendant of TDataModule. An object instance has to be created and destroyed at runtime in order to do anything with the service. So yes, you will always get OnCreate and OnDestroy events regardless of whether the service is being installed/uninstalled by the user or started/stop by the SCM.

tonus wrote:An initialized static variable behaves differently.

A static variable is not persistent from one executable process to another. A static variable is initialized the first time it is reached by code, whether that be globally at process startup, or locally the first time the declaring function is called. Once a static variable is initialized, its value is persistent for the lifetime of the calling process, and can be changed as needed, but its value is not automatically saved when the process terminates. You have to do that yourself in your own code if you need it. This applies to all project types, not just services only.

When you run the service executable with the /install or /uninstall command-line parameter, the executable creates the TService instance, then asks it to add/remove the necessary Registry values as needed, and then the process ends. The actual service is not run.

When you start the service via the SCM, the executable creates the TService instance, then sets up the callback hooks that the SCM uses to control the service, and then the executable remains running until the service is eventually stopped.

tonus wrote:As I want an array of pointers to point to objects created with new I want to be absolutely sure that I'm using the right variables pointing to the correct objects

You will - but only withing the scope of a single process. If you are expecting to create the objects when you install the service, and then use them when the service is started, then that will not work.

Tonus it trying to create a service for a custom TCP/IP server using methods described at my "Network Communications with BCB" articles...
I hope my article of this month to be helpful for tonus and everyone else want to do so something similar.

I also hope that everybody understands that the "Network Communications with BCB" series, "Cryptography with BCB" series and "Random Number Generation" are connected...

Well I started off following a too simple method, and banged against the wall straigth away!
Having built several bcb projects and being pleasantly surprised by so much already being there. (I also did assembler programming, even build some services guided by an early Peter Norton book. But that was more than a decade ago.)
I started surveying the readily available methods and events in the TService component, assuming a behaviour similar to that of TApplication. So I set up a logging routine that would write all events as they were called.
After that it would seem appropriate to setup a class and initialize that in the OnCreate event ....
Well don't know if you are still reading or laughing right out loud, but for me it took a while to find out that the framework for TService is quite different from TApplication and yes reading is my weakest point.

This thread was definitely intended to both find a solution and find a "sparring partner", not to do something odd. And I surely hope others that are trapped the same way may read your clear and concise explanations.
Now I've read them I know I should have known better.

Meanwhile I've build a bcb service that as a client communicates to remote some process applications. On the other end it is a server to delphi services that have access to databases. Finally reduced that to the minimum essential.
It runs for several weeks now ....without an itch.
Using bcb components as they came with bcb 5 pro.