Controlling Windows Services

12 08 2009

This is somewhat interesting because Windows services do not exactly behave like normal programs… meaning you cannot just install/uninstall it with some simple extraction…

A service is effectively a program that can sleep. Now consider a normal program. At any instant of time, you can clearly say that the program is doing something. Services are not like that. They don’t need to do anything. They can do stuff only on some events (like Timer ticks or on some event)… ok, with new threads and asynchronous programming etc, I guess the demarcation becomes rather weak.

Anyways, a service is basically an exe written as a service. A normal CPP file cannot be a service. The easiest way is to simply code it out in C#. Visual Studio has an inbuilt template that you can use and define your own OnStart and OnStop functions etc. Now installing a service involves putting a link to that effect in the registry. Now this installation has information about the exe that is involved with the service and also the account under which the service runs (and if the service starts automatically, delayed or manually).

To install a service – assuming you have the exe… use the sc create command. You can specify the exe path and also the account (LocalSystem is default), along with startup options, name, display name etc. In shot, this is all you need to install a service.

To run/start the service use net start <Servicename>

To stop the service use net stop <servicename>

You can equivalently start/stop the service using the Services control panel thinge. Its simpler and is one click 🙂

To uninstall the service do a sc delete.

Installing and uninstalling is usually done by software installers on their own… so the technique may not be so popular.

😦 I found out about sc create very late in my life… I was using VS’s installutil for some time 😦 and at other times, using C# code… sometimes even native C dlls 😦 😦




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: