unsigned long type = REG_EXPAND_SZ;\r
unsigned long buflen = datalen;\r
\r
- if (RegQueryValueEx(key, value, 0, &type, buffer, &buflen) != ERROR_SUCCESS) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, GetLastError(), 0);\r
+ unsigned long ret = RegQueryValueEx(key, value, 0, &type, buffer, &buflen);\r
+ if (ret != ERROR_SUCCESS) {\r
+ if (ret != ERROR_FILE_NOT_FOUND) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, value, GetLastError(), 0);\r
HeapFree(GetProcessHeap(), 0, buffer);\r
return 2;\r
}\r
return 0;\r
}\r
\r
- unsigned long ret = ExpandEnvironmentStrings((char *) buffer, data, datalen);\r
+ ret = ExpandEnvironmentStrings((char *) buffer, data, datalen);\r
if (! ret || ret > datalen) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, value, buffer, GetLastError(), 0);\r
HeapFree(GetProcessHeap(), 0, buffer);\r
return 3;\r
}\r
\r
- /* Try to get flags - may fail */\r
+ /* Try to get flags - may fail and we don't care */\r
if (expand_parameter(key, NSSM_REG_FLAGS, flags, flagslen)) {\r
- RegCloseKey(key);\r
- return 4;\r
- }\r
-\r
- /* Try to get startup directory - may fail */\r
- if (expand_parameter(key, NSSM_REG_DIR, dir, dirlen)) {\r
- RegCloseKey(key);\r
- return 5;\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_FLAGS, NSSM_REG_FLAGS, service_name, exe, 0);\r
+ ZeroMemory(flags, flagslen);\r
+ }\r
+\r
+ /* Try to get startup directory - may fail and we fall back to a default */\r
+ if (expand_parameter(key, NSSM_REG_DIR, dir, dirlen) || ! dir[0]) {\r
+ /* Our buffers are defined to be long enough for this to be safe */\r
+ size_t i;\r
+ for (i = strlen(exe); i && exe[i] != '\\' && exe[i] != '/'; i--);\r
+ if (i) {\r
+ memmove(dir, exe, i);\r
+ dir[i] = '\0';\r
+ }\r
+ else {\r
+ /* Help! */\r
+ unsigned long ret = ExpandEnvironmentStrings("%SYSTEMROOT%", dir, dirlen);\r
+ if (! ret || ret > dirlen) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_NO_DIR_AND_NO_FALLBACK, NSSM_REG_DIR, service_name, 0);\r
+ RegCloseKey(key);\r
+ return 4;\r
+ }\r
+ }\r
+ log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_DIR, NSSM_REG_DIR, service_name, dir, 0);\r
}\r
\r
/* Close registry */\r