Browse Source

Don't create registry keys needlessly.

Don't log an error when trying to access a registry key which doesn't
exist when it's expected that it might not be present.

Don't open a key with RegCreateKeyEx() when we are potentially going to
delete a value.  Instead try RegOpenKeyEx() and return success if it
already isn't present.
Iain Patterson 9 years ago
parent
commit
a90eade7d0
2 changed files with 10 additions and 3 deletions
  1. 9 3
      registry.cpp
  2. 1 0
      registry.h

+ 9 - 3
registry.cpp

@@ -457,7 +457,7 @@ void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned
   if (! ok) *buffer = default_value;
 }
 
-HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
+HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam, bool must_exist) {
   /* Get registry */
   TCHAR registry[KEY_LENGTH];
   HKEY key;
@@ -477,7 +477,9 @@ HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
     }
   }
   else {
-    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, &key) != ERROR_SUCCESS) {
+    long error = RegOpenKeyEx(HKEY_LOCAL_MACHINE, registry, 0, sam, &key);
+    if (error != ERROR_SUCCESS) {
+      if (error == ERROR_FILE_NOT_FOUND && ! must_exist) return 0;
       log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
       return 0;
     }
@@ -486,8 +488,12 @@ HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
   return key;
 }
 
+HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
+  return open_registry(service_name, sub, sam, true);
+}
+
 HKEY open_registry(const TCHAR *service_name, REGSAM sam) {
-  return open_registry(service_name, 0, sam);
+  return open_registry(service_name, 0, sam, true);
 }
 
 int get_io_parameters(nssm_service_t *service, HKEY key) {

+ 1 - 0
registry.h

@@ -35,6 +35,7 @@
 #define NSSM_REG_NO_CONSOLE _T("AppNoConsole")
 #define NSSM_STDIO_LENGTH 29
 
+HKEY open_registry(const TCHAR *, const TCHAR *, REGSAM sam, bool);
 HKEY open_registry(const TCHAR *, const TCHAR *, REGSAM sam);
 HKEY open_registry(const TCHAR *, REGSAM sam);
 int create_messages();