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.
master
Iain Patterson 10 years ago
parent 2dbaf62e06
commit a90eade7d0

@ -457,7 +457,7 @@ void override_milliseconds(TCHAR *service_name, HKEY key, TCHAR *value, unsigned
if (! ok) *buffer = default_value; 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 */ /* Get registry */
TCHAR registry[KEY_LENGTH]; TCHAR registry[KEY_LENGTH];
HKEY key; HKEY key;
@ -477,7 +477,9 @@ HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
} }
} }
else { 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); log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OPENKEY_FAILED, registry, error_string(GetLastError()), 0);
return 0; return 0;
} }
@ -486,8 +488,12 @@ HKEY open_registry(const TCHAR *service_name, const TCHAR *sub, REGSAM sam) {
return key; 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) { 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) { int get_io_parameters(nssm_service_t *service, HKEY key) {

@ -35,6 +35,7 @@
#define NSSM_REG_NO_CONSOLE _T("AppNoConsole") #define NSSM_REG_NO_CONSOLE _T("AppNoConsole")
#define NSSM_STDIO_LENGTH 29 #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 *, const TCHAR *, REGSAM sam);
HKEY open_registry(const TCHAR *, REGSAM sam); HKEY open_registry(const TCHAR *, REGSAM sam);
int create_messages(); int create_messages();

Loading…
Cancel
Save