\r
int get_environment(TCHAR *service_name, HKEY key, TCHAR *value, TCHAR **env, unsigned long *envlen) {\r
unsigned long type = REG_MULTI_SZ;\r
+ unsigned long envsize;\r
+\r
+ *envlen = 0;\r
\r
/* Dummy test to find buffer size */\r
- unsigned long ret = RegQueryValueEx(key, value, 0, &type, NULL, envlen);\r
+ unsigned long ret = RegQueryValueEx(key, value, 0, &type, NULL, &envsize);\r
if (ret != ERROR_SUCCESS) {\r
*env = 0;\r
- *envlen = 0;\r
/* The service probably doesn't have any environment configured */\r
if (ret == ERROR_FILE_NOT_FOUND) return 0;\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);\r
}\r
\r
if (type != REG_MULTI_SZ) {\r
- *env = 0;\r
- *envlen = 0;\r
log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_INVALID_ENVIRONMENT_STRING_TYPE, value, service_name, 0);\r
+ *env = 0;\r
return 2;\r
}\r
\r
/* Probably not possible */\r
- if (! *envlen) return 0;\r
+ if (! envsize) return 0;\r
\r
/* Previously initialised? */\r
if (*env) HeapFree(GetProcessHeap(), 0, *env);\r
\r
- *env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, *envlen);\r
+ *env = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, envsize);\r
if (! *env) {\r
- *envlen = 0;\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, _T("get_environment()"), 0);\r
return 3;\r
}\r
\r
- /* Actually get the strings */\r
- ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) *env, envlen);\r
+ /* Actually get the strings. */\r
+ ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) *env, &envsize);\r
if (ret != ERROR_SUCCESS) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(ret), 0);\r
HeapFree(GetProcessHeap(), 0, *env);\r
*env = 0;\r
- *envlen = 0;\r
return 4;\r
}\r
\r
+ /* Value retrieved by RegQueryValueEx() is SIZE not COUNT. */\r
+ *envlen = (unsigned long) environment_length(env);\r
+\r
return 0;\r
}\r
\r
\r
unsigned long ret = RegQueryValueEx(key, value, 0, &type, (unsigned char *) buffer, &buflen);\r
if (ret != ERROR_SUCCESS) {\r
- unsigned long error = GetLastError();\r
HeapFree(GetProcessHeap(), 0, buffer);\r
\r
if (ret == ERROR_FILE_NOT_FOUND) {\r
if (! must_exist) return 0;\r
}\r
\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(error), 0);\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(ret), 0);\r
return 2;\r
}\r
\r
if (! must_exist) return 0;\r
}\r
\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(ret), 0);\r
if (ret == ERROR_FILE_NOT_FOUND) return -1;\r
\r
return -2;\r
_sntprintf_s(milliseconds, _countof(milliseconds), _TRUNCATE, _T("%lu"), default_value);\r
log_event(EVENTLOG_WARNING_TYPE, event, service_name, value, milliseconds, 0);\r
}\r
- else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(GetLastError()), 0);\r
+ else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, error_string(ret), 0);\r
}\r
}\r
else ok = true;\r
if (type != REG_DWORD) {\r
log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_BOGUS_STOP_METHOD_SKIP, service->name, NSSM_REG_STOP_METHOD_SKIP, NSSM, 0);\r
}\r
- else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_STOP_METHOD_SKIP, error_string(GetLastError()), 0);\r
+ else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_STOP_METHOD_SKIP, error_string(ret), 0);\r
}\r
}\r
else stop_ok = true;\r