Monday, 19 August 2013

How to start my Windows Service at RunTime or at OnAfterInstall

How to start my Windows Service at RunTime or at OnAfterInstall

History
I am building a Windows Service as part of my Platform Application to
handle updates and server functions, so it can be installed in machines
different than where the Client Application is installed. It uses UDP to
send and receive broadcast messages, and TCP to to handle more sensitive
and most important messages.
Objective
I want that my Application could be easily installed by the end user, just
by copying the executable files in the computers and when executing them.
The main application should verify if the user is an administrator, create
the configuration files, and then install the Windows Service and run it,
so when non administrative users log in, they won't receive any error from
my Application regarding administrative rights. The goal is to make most
configurations without the need a present technician, since database will
be remote.
Problem
My Service is being installed normally with the command MyService.exe
/install but it is not starting automatically. The only way to start it is
to go on Control Panel > Admin Tools > Services and do it manually. I
tryed to call net start MyService through my application but I receive
invalid service name at the shell. I tryied the executable name, the
display name and the object name of the service, but none of them worked.
This is the object of my TService:
object ServiceMainController: TServiceMainController
OldCreateOrder = False
OnCreate = ServiceCreate
DisplayName = 'PlatformUpdateService'
Interactive = True
AfterInstall = ServiceAfterInstall
AfterUninstall = ServiceAfterUninstall
OnShutdown = ServiceShutdown
OnStart = ServiceStart
OnStop = ServiceStop
Height = 210
Width = 320
Question
What should I do to start my service by the code and not by the user
hands? It would be the best if I could do it inside my client application,
or when by itself after the OnServiceAfterInstall call.

No comments:

Post a Comment