From ea55c9d26cc6ea9473d4dd23f4a62109763873de Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 13 Nov 2013 15:32:40 +0000 Subject: [PATCH] Added override_milliseconds() helper. Helper function to retrieve a REG_DWORD value from the registry and assign it to a variable, substituting a default value if the registry entry is invalid or missing. The function is specifically tailored toward setting a value in milliseconds, hence the name. --- registry.cpp | 20 ++++++++++++++++++++ registry.h | 1 + 2 files changed, 21 insertions(+) diff --git a/registry.cpp b/registry.cpp index fbcc369..a1332b2 100644 --- a/registry.cpp +++ b/registry.cpp @@ -219,6 +219,26 @@ int get_number(HKEY key, char *value, unsigned long *number) { return get_number(key, value, number, true); } +void override_milliseconds(char *service_name, HKEY key, char *value, unsigned long *buffer, unsigned long default_value, unsigned long event) { + unsigned long type = REG_DWORD; + unsigned long buflen = sizeof(unsigned long); + bool ok = false; + unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen); + if (ret != ERROR_SUCCESS) { + if (ret != ERROR_FILE_NOT_FOUND) { + if (type != REG_DWORD) { + char milliseconds[16]; + _snprintf_s(milliseconds, sizeof(milliseconds), _TRUNCATE, "%lu", default_value); + log_event(EVENTLOG_WARNING_TYPE, event, service_name, value, milliseconds, 0); + } + else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0); + } + } + else ok = true; + + if (! ok) *buffer = default_value; +} + int get_parameters(char *service_name, char *exe, unsigned long exelen, char *flags, unsigned long flagslen, char *dir, unsigned long dirlen, char **env, unsigned long *throttle_delay, unsigned long *stop_method, STARTUPINFO *si) { unsigned long ret; diff --git a/registry.h b/registry.h index 6fd5d9c..9129b64 100644 --- a/registry.h +++ b/registry.h @@ -25,6 +25,7 @@ int expand_parameter(HKEY, char *, char *, unsigned long, bool, bool); int expand_parameter(HKEY, char *, char *, unsigned long, bool); int get_number(HKEY, char *, unsigned long *, bool); int get_number(HKEY, char *, unsigned long *); +void override_milliseconds(char *, HKEY, char *, unsigned long *, unsigned long, unsigned long); int get_parameters(char *, char *, unsigned long, char *, unsigned long, char *, unsigned long, char **, unsigned long *, unsigned long *, STARTUPINFO *); int get_exit_action(char *, unsigned long *, unsigned char *, bool *); -- 2.7.4