return 0;\r
}\r
\r
+int get_service_username(const TCHAR *service_name, const QUERY_SERVICE_CONFIG *qsc, TCHAR **username, size_t *usernamelen) {\r
+ if (! username) return 1;\r
+ if (! usernamelen) return 1;\r
+\r
+ *username = 0;\r
+ *usernamelen = 0;\r
+\r
+ if (! qsc) return 1;\r
+\r
+ if (str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) return 0;\r
+\r
+ size_t len = _tcslen(qsc->lpServiceStartName);\r
+ *username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR));\r
+ if (! *username) {\r
+ print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("username"), _T("get_service_username()"));\r
+ return 2;\r
+ }\r
+\r
+ memmove(*username, qsc->lpServiceStartName, (len + 1) * sizeof(TCHAR));\r
+ *usernamelen = len;\r
+\r
+ return 0;\r
+}\r
+\r
static int grant_logon_as_service(const TCHAR *username) {\r
if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return 0;\r
\r
return 4;\r
}\r
\r
- if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) {\r
- size_t len = _tcslen(qsc->lpServiceStartName);\r
- service->username = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR));\r
- if (service->username) {\r
- memmove(service->username, qsc->lpServiceStartName, (len + 1) * sizeof(TCHAR));\r
- service->usernamelen = (unsigned long) len;\r
- }\r
- else {\r
- HeapFree(GetProcessHeap(), 0, qsc);\r
- CloseHandle(service->handle);\r
- CloseServiceHandle(services);\r
- print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("username"), _T("pre_edit_service()"));\r
- return 5;\r
- }\r
+ if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) {\r
+ HeapFree(GetProcessHeap(), 0, qsc);\r
+ CloseHandle(service->handle);\r
+ CloseServiceHandle(services);\r
+ return 5;\r
}\r
+\r
_sntprintf_s(service->displayname, _countof(service->displayname), _TRUNCATE, _T("%s"), qsc->lpDisplayName);\r
\r
/* Get the canonical service name. We open it case insensitively. */\r
QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);\r
int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *);\r
int get_service_startup(const TCHAR *, SC_HANDLE, const QUERY_SERVICE_CONFIG *, unsigned long *);\r
+int get_service_username(const TCHAR *, const QUERY_SERVICE_CONFIG *, TCHAR **, size_t *);\r
int pre_install_service(int, TCHAR **);\r
int pre_remove_service(int, TCHAR **);\r
int pre_edit_service(int, TCHAR **);\r