From: Iain Patterson Date: Fri, 2 Jan 2015 13:16:54 +0000 (+0000) Subject: Don't create registry keys needlessly. X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=a90eade7d022451e92bedcebe816651df7bf0d17;p=nssm.git 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. --- diff --git a/registry.cpp b/registry.cpp index 4abf595..725844b 100644 --- a/registry.cpp +++ b/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) { diff --git a/registry.h b/registry.h index bd8ee41..3e380b4 100644 --- a/registry.h +++ b/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();