tags:

views:

100

answers:

2

Hello,

I have a problem in understanding the relationship between services and registry.

I have the task of taking my windows C++ program and transform it from simple application to a service. I read that I need to produce some more functions as: start stop resume install. The problem is:

  1. Why I need the regisrty ?

  2. how I enter the new program ?

  3. Beside those method what I need to do with the registry? how I enter inside it ?

  4. Do I need to write a script for entering the service ?

I read but I just didn't understand, any answear and or some good links to explanation will be appreciated.

Thanks,

+4  A: 

I'm not aware of any documented relationship between services and the registry. Services can use the registry to store their settings, just like any other application, but they're not required to.

  1. Formally, you don't need the registry. You simply need to install the service using the relevant API functions. As part of their implementation, the API functions create registry entries that the OS uses later to know when and how to start your service, but I don't think those keys are documented with any expectation that developers would modify them manually, so don't worry about them.

    If your program uses the registry to store settings, though, you'll need to understand what account your service runs as, because that affects what areas of the registry your program has access to.

  2. Install your service by calling CreateService. Do that in your program's installer. You can also make your service install itself when it detects itself being run with a certain command-line switch, such as -i. To uninstall your service, call OpenService and then DeleteService. In either case, you'll need to call OpenSCManager first. See MSDN for more on how to call those functions.

    Alternatively, you can use the sc command to create and delete your service.

  3. As I mentioned above, you don't need to do anything with the registry. Just install and uninstall your service with the API and let the OS take care of the rest.

  4. You don't need to write any scripts to start your service. The OS already knows how to start it (because it's installed). If your service is something that users would want to start and stop frequently, then rather than use the service control panel then they can use the net or sc commands.

Rob Kennedy
Thank for the good explanation. To you know a book/site I could read on this an other microsoft issues for programmer ? Thanks
Roman Dorevich
If according to nu,ber 1 answear, if you don't use the registery, how you pass those parameters to the service ? simple using arguments (argc, argv ?) . And if use registry, so I need to get the parameters I entered from there ?
Roman Dorevich
Regarding the command line, please read about the *lpBinaryPathName* parameter in the documentation about `CreateService`. The OS will use that value to start your service. You can set it to anything you want.
Rob Kennedy
+1  A: 

We use the registry to store command line parameters. The executable is passed a special parameter saying "you are a service, and here is your service name", and then the program knows to look in the registry and read the rest of the command line parameters from there. Honestly, I don't know why it was done this way, but I suspect that there's a limit on the length of service command line.

As Rob said however, services don't have to use the registry at all.

Graeme Perrow