First of all, what are Windows services?
well, wiki said that
Well, lets simplify that a little bit: a service is an application. But, this application has no UI of itself, and no windows. Some services can interact with the desktop, but this is rare and this is not something that I’ll do as a programmer.
So what is the big difference?
There are two big differences between a service and a command line utility (I’m using command here, since commands doesn’t have any UI either) that does the same job:
- In a command program you have to start it in order for that command to work. you have to be logged in and start the command. In a service, Windows (using something that called SCM) does that for you. You don’t need to start the service manually (but you can if you need) and you don’t even have to be logged on. If the computer booted correctly, the service is up and running, unless some problem occurred.
- Commands run under the user that is running them. True, you can “Run as” a command, but it is usually used to downgrade command priority (run it with less permissions). Services on the other hand, will usually be run as system or administrator users. and so, even if a regular user is logged into the system, some tasks will be performed with higher level of permissions.
So why do we need services?
There are many tasks we want to be started without someone at the machine,like a web server, file server, print server and so on. Yes, services are usually written for server programs. But, there are some other applications as well. As mentioned, Service can run with different users credentials then the user that is actually logged on, so, services can be used to grant some level of access to the user that normally can not be done under the OS. This will sound better with example: Lets say we have a file that every employ needs to fill his id every time he or she comes to work. Now we can not give the employees write access to that file, because they might be tempted to add more hours, or delete other people records. We can’t even let them have read access since they should not read other people records.
So? we write a service, and a client program. Every time that a user runs the client program (under his privileges) the program interacts with the service (there are many ways to do that) and sends it the user data. the service then updates the file with its own time stamp and with the user data from the client program. Mission accomplished! Database server are based on a scheme like this (but much, much more complicated).