Clean up Parameters properly.
authorIain Patterson <me@iain.cx>
Sun, 8 Mar 2015 17:24:58 +0000 (17:24 +0000)
committerIain Patterson <me@iain.cx>
Sun, 8 Mar 2015 17:24:58 +0000 (17:24 +0000)
If create_parameters() failed to write Application, AppParameters or
AppDirectory it would try to clean up by deleting the Parameters key.
The method used to do so was flawed.  It would attempt to delete the key
defined by the NSSM_REGISTRY macro but that macro contains a printf
control string and is not suitable for standalone use.

registry.cpp

index 5953f8a..e499933 100644 (file)
@@ -32,19 +32,23 @@ int create_parameters(nssm_service_t *service, bool editing) {
   HKEY key = open_registry(service->name, KEY_WRITE);\r
   if (! key) return 1;\r
 \r
+  /* Remember parameters in case we need to delete them. */\r
+  TCHAR registry[KEY_LENGTH];\r
+  int ret = service_registry_path(service->name, true, 0, registry, _countof(registry));\r
+\r
   /* Try to create the parameters */\r
   if (set_expand_string(key, NSSM_REG_EXE, service->exe)) {\r
-    RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
+    if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry);\r
     RegCloseKey(key);\r
     return 2;\r
   }\r
   if (set_expand_string(key, NSSM_REG_FLAGS, service->flags)) {\r
-    RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
+    if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry);\r
     RegCloseKey(key);\r
     return 3;\r
   }\r
   if (set_expand_string(key, NSSM_REG_DIR, service->dir)) {\r
-    RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
+    if (ret > 0) RegDeleteKey(HKEY_LOCAL_MACHINE, registry);\r
     RegCloseKey(key);\r
     return 4;\r
   }\r