Browse Source

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.
Iain Patterson 8 years ago
parent
commit
ea55c9d26c
2 changed files with 21 additions and 0 deletions
  1. 20 0
      registry.cpp
  2. 1 0
      registry.h

+ 20 - 0
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;
 

+ 1 - 0
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 *);