From: Iain Patterson Date: Thu, 25 Oct 2012 04:47:12 +0000 (-0700) Subject: Added get_number() helper. X-Git-Tag: v2.17~15 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=c479182f184204e0f330e68b2927ce7720f7e54f;p=nssm.git 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. --- diff --git a/registry.cpp b/registry.cpp index 5172c50..54a6114 100644 --- a/registry.cpp +++ b/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; diff --git a/registry.h b/registry.h index aae45ac..561318e 100644 --- a/registry.h +++ b/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 *);