Browse Source

Added get_number() helper.

Retrieve a number (specifically an unsigned long) from the registry.
Allow the value to be optional by setting the must_exist parameter.
Overload the function definition such that must_exist can be omitted.
Iain Patterson 9 years ago
parent
commit
c479182f18
2 changed files with 30 additions and 0 deletions
  1. 28 0
      registry.cpp
  2. 2 0
      registry.h

+ 28 - 0
registry.cpp

@@ -181,6 +181,34 @@ int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, b
   return 0;
 }
 
+/*
+  Query an unsigned long from the registry.
+  Returns:  1 if a number was retrieved.
+            0 if none was found and must_exist is false.
+           -1 if none was found and must_exist is true.
+           -2 otherwise.
+*/
+int get_number(HKEY key, char *value, unsigned long *number, bool must_exist) {
+  unsigned long type = REG_DWORD;
+  unsigned long number_len = sizeof(unsigned long);
+
+  int ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) number, &number_len);
+  if (ret == ERROR_SUCCESS) return 1;
+
+  if (ret == ERROR_FILE_NOT_FOUND) {
+    if (! must_exist) return 0;
+  }
+
+  log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);
+  if (ret == ERROR_FILE_NOT_FOUND) return -1;
+
+  return -2;
+}
+
+int get_number(HKEY key, char *value, unsigned long *number) {
+  return get_number(key, value, number, true);
+}
+
 int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen, char **env, unsigned long *throttle_delay) {
   unsigned long ret;
 

+ 2 - 0
registry.h

@@ -14,6 +14,8 @@ int create_parameters(char *, char *, char *, char *);
 int create_exit_action(char *, const char *);
 int set_environment(char *, HKEY, char **);
 int expand_parameter(HKEY, char *, char *, unsigned long, bool);
+int get_number(HKEY, char *, unsigned long *, bool);
+int get_number(HKEY, char *, unsigned long *);
 int get_parameters(char *, char *, int, char *, int, char *, int, char **, unsigned long *);
 int get_exit_action(char *, unsigned long *, unsigned char *, bool *);