0

I have a service which takes a few parameters on the image path.

eg:

ImagePath = "C:\Program Files (x86)\SomeApp\ServiceApp.exe -arg1=123 -arg2=234" 

Is it possible to store non-standard values for a service via any particular function? I would like to store -arg1=123 and -arg2=234 as values for the service.

I know services are described by keys in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services, but I don't know if adding values directly to the registry key is a good idea (not sure if it would affect loading services into the service database), or if there is another way.

Example key I would want to store:

Name: arg1

Type: REG_SZ

Data: arg1=123

I'm not even sure if storing values like this is best practice, but we have a need to separately store command line parameters provided on the image path.

I have a handle to SCManager and installation/deleting a service is fine, as is setting the description via ChangeServiceConfig2, but I would like to store a couple of other values if possible without manually modifying the registry.

Community
  • 1
  • 1
AlexH
  • 115
  • 13
  • I don't really understand what you are asking. You can specify any command line you like when registering the service. What's stopping you supplying the arguments that you need? – David Heffernan Sep 29 '14 at 09:37
  • Are these dynamic values? – Yuval Itzchakov Sep 29 '14 at 09:40
  • Command line values are supplied on the image path when registering the service. I would like to store them separately as well, rather than parse them off the path when I'm querying or changing the service (this happens a lot). – AlexH Sep 29 '14 at 09:45
  • @YuvalItzchakov They are not really dynamic - but a common scenario is installing multiple versions of the service after the initial install, and it would be good not to have to parse the previous arguments from the image path in order to work out the subsequent arguments. – AlexH Sep 29 '14 at 09:51
  • 2
    It is typical to put service parameters in the command-line or in `HKLM\SYSTEM\CurrentControlSet\services\\Parameters\` or in `HKLM\SOFTWARE\\\`. For the latter two you have to call the registry functions yourself (AFAIK). Additionally, your command-line above has a bug. The path must be quoted separately from the parameters, i.e. with C-style escaping `"\"C:\\Program Files (x86)\\SomeApp\\ServiceApp.exe\" -arg1=123 -arg2=234"` – arx Sep 29 '14 at 10:06
  • 2
    You should store these values once and once only. It's not that important where you store them. What is important is that you don't store them multiple times. The DRY principle. As soon as you store them multiple times you run the risk that they get out of sync. – David Heffernan Sep 29 '14 at 10:21
  • @AlexH wrote "it would be good not to have to parse the previous arguments from the image path in order to work out the subsequent arguments". If you have multiple versions of a service, typically each has its own name (e.g. `myservice1`, `myservice2`, etc.), its own service subkey and its own image path. So they operate independently and you won't have a problem parsing the command-line parameters. – arx Sep 29 '14 at 10:32
  • Parsing the image path, not violating DRY would be ideal, and were the first things I considered. The question however is about storing non standard values for a service without writing the registry. I thought it unlikely but I'm a C# dev and not as familiar with the advapi32 functions as I could be. – AlexH Sep 29 '14 at 10:59
  • The command line for the service is stored in the registry. So, that's where you need to write the data. I've no idea why you need to worry about advapi32. The .net framework has good support for accessing the registry. – David Heffernan Sep 29 '14 at 11:08
  • @DavidHeffernan advapi32 has a lot of functions for manipulating services, as mentioned I'm using ChangeServiceConfig2. Although I do agree that it should only be stored in the imagepath, the requirements I have are different. – AlexH Sep 29 '14 at 11:30
  • Never mind. I cannot follow these comments at all. I really have no idea what your issue is. – David Heffernan Sep 29 '14 at 11:35

0 Answers0