}\r
\r
/* Try to create the parameters */\r
- if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_SZ, (const unsigned char *) exe, strlen(exe) + 1) != ERROR_SUCCESS) {\r
+ if (RegSetValueEx(key, NSSM_REG_EXE, 0, REG_EXPAND_SZ, (const unsigned char *) exe, strlen(exe) + 1) != ERROR_SUCCESS) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_EXE, GetLastError(), 0);\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
return 3;\r
}\r
- if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_SZ, (const unsigned char *) flags, strlen(flags) + 1) != ERROR_SUCCESS) {\r
+ if (RegSetValueEx(key, NSSM_REG_FLAGS, 0, REG_EXPAND_SZ, (const unsigned char *) flags, strlen(flags) + 1) != ERROR_SUCCESS) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_FLAGS, GetLastError(), 0);\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
return 4;\r
}\r
- if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_SZ, (const unsigned char *) dir, strlen(dir) + 1) != ERROR_SUCCESS) {\r
+ if (RegSetValueEx(key, NSSM_REG_DIR, 0, REG_EXPAND_SZ, (const unsigned char *) dir, strlen(dir) + 1) != ERROR_SUCCESS) {\r
log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_DIR, GetLastError(), 0);\r
RegDeleteKey(HKEY_LOCAL_MACHINE, NSSM_REGISTRY);\r
RegCloseKey(key);\r
return 0;\r
}\r
\r
+int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen) {\r
+ unsigned char *buffer = (unsigned char *) HeapAlloc(GetProcessHeap(), 0, datalen);\r
+ if (! buffer) {\r
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, "expand_parameter()", 0);\r
+ return 1;\r
+ }\r
+\r
+ 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
+ HeapFree(GetProcessHeap(), 0, buffer);\r
+ return 2;\r
+ }\r
+\r
+ ZeroMemory(data, datalen);\r
+\r
+ /* Technically we shouldn't expand environment strings from REG_SZ values */\r
+ if (type != REG_EXPAND_SZ) {\r
+ memmove(data, buffer, buflen);\r
+ return 0;\r
+ }\r
+\r
+ unsigned long 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
+ log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_EXPANDENVIRONMENTSTRINGS_FAILED, buffer, data, GetLastError(), 0);\r
+\r
+ HeapFree(GetProcessHeap(), 0, buffer);\r
+ return 0;\r
+}\r
+\r
int get_parameters(char *service_name, char *exe, int exelen, char *flags, int flagslen, char *dir, int dirlen) {\r
/* Get registry */\r
char registry[KEY_LENGTH];\r
return 2;\r
}\r
\r
- unsigned long type = REG_SZ;\r
-\r
/* Try to get executable file - MUST succeed */\r
- if (RegQueryValueEx(key, NSSM_REG_EXE, 0, &type, (unsigned char *) exe, (unsigned long *) &exelen) != ERROR_SUCCESS) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_EXE, GetLastError(), 0);\r
+ if (expand_parameter(key, NSSM_REG_EXE, exe, exelen)) {\r
RegCloseKey(key);\r
return 3;\r
}\r
\r
/* Try to get flags - may fail */\r
- if (RegQueryValueEx(key, NSSM_REG_FLAGS, 0, &type, (unsigned char *) flags, (unsigned long *) &flagslen) != ERROR_SUCCESS) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_FLAGS, GetLastError(), 0);\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 (RegQueryValueEx(key, NSSM_REG_DIR, 0, &type, (unsigned char *) dir, (unsigned long *) &dirlen) != ERROR_SUCCESS) {\r
- log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_QUERYVALUE_FAILED, NSSM_REG_DIR, GetLastError(), 0);\r
+ if (expand_parameter(key, NSSM_REG_DIR, dir, dirlen)) {\r
RegCloseKey(key);\r
return 5;\r
}\r