From: Iain Patterson Date: Tue, 14 Jan 2014 23:04:55 +0000 (+0000) Subject: Added get_string() and set_string(). X-Git-Tag: v2.22~53 X-Git-Url: http://git.iain.cx/?a=commitdiff_plain;h=c262633514e151f681f4d8234185e2212a959c27;p=nssm.git Added get_string() and set_string(). Abstract expand_parameter() and set_expand_string() to allow working with registry values of type REG_SZ as well a REG_EXPAND_SZ. --- diff --git a/registry.cpp b/registry.cpp index b526ee5..0cb552f 100644 --- a/registry.cpp +++ b/registry.cpp @@ -281,10 +281,10 @@ int unformat_environment(TCHAR *env, unsigned long envlen, TCHAR **unformatted, return 0; } -int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) { +int get_string(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool expand, bool sanitise, bool must_exist) { TCHAR *buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, datalen); if (! buffer) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("expand_parameter()"), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("get_string()"), 0); return 1; } @@ -309,6 +309,11 @@ int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, /* Paths aren't allowed to contain quotes. */ if (sanitise) PathUnquoteSpaces(buffer); + /* Do we want to expand the string? */ + if (! expand) { + if (type == REG_EXPAND_SZ) type = REG_SZ; + } + /* Technically we shouldn't expand environment strings from REG_SZ values */ if (type != REG_EXPAND_SZ) { memmove(data, buffer, buflen); @@ -327,6 +332,14 @@ int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, return 0; } +int get_string(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise) { + return get_string(key, value, data, datalen, false, sanitise, true); +} + +int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise, bool must_exist) { + return get_string(key, value, data, datalen, true, sanitise, must_exist); +} + int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, bool sanitise) { return expand_parameter(key, value, data, datalen, sanitise, true); } @@ -336,12 +349,23 @@ int expand_parameter(HKEY key, TCHAR *value, TCHAR *data, unsigned long datalen, Returns: 0 if it was set. 1 on error. */ -int set_expand_string(HKEY key, TCHAR *value, TCHAR *string) { - if (RegSetValueEx(key, value, 0, REG_EXPAND_SZ, (const unsigned char *) string, (unsigned long) (_tcslen(string) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS) return 0; +int set_string(HKEY key, TCHAR *value, TCHAR *string, bool expand) { + unsigned long type = expand ? REG_EXPAND_SZ : REG_SZ; + if (RegSetValueEx(key, value, 0, type, (const unsigned char *) string, (unsigned long) (_tcslen(string) + 1) * sizeof(TCHAR)) == ERROR_SUCCESS) return 0; log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, value, error_string(GetLastError()), 0); return 1; } +int set_string(HKEY key, TCHAR *value, TCHAR *string) { + return set_string(key, value, string, false); + return 1; +} + +int set_expand_string(HKEY key, TCHAR *value, TCHAR *string) { + return set_string(key, value, string, true); + return 1; +} + /* Set an unsigned long in the registry. Returns: 0 if it was set. diff --git a/registry.h b/registry.h index 162b8b3..da3c8b8 100644 --- a/registry.h +++ b/registry.h @@ -37,8 +37,12 @@ int create_exit_action(TCHAR *, const TCHAR *, bool); int set_environment(TCHAR *, HKEY, TCHAR *, TCHAR **, unsigned long *); int format_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *); int unformat_environment(TCHAR *, unsigned long, TCHAR **, unsigned long *); +int get_string(HKEY, TCHAR *, TCHAR *, unsigned long, bool, bool, bool); +int get_string(HKEY, TCHAR *, TCHAR *, unsigned long, bool); int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool, bool); int expand_parameter(HKEY, TCHAR *, TCHAR *, unsigned long, bool); +int set_string(HKEY, TCHAR *, TCHAR *, bool); +int set_string(HKEY, TCHAR *, TCHAR *); int set_expand_string(HKEY, TCHAR *, TCHAR *); int set_number(HKEY, TCHAR *, unsigned long); int get_number(HKEY, TCHAR *, unsigned long *, bool);