Added get_service_description().
authorIain Patterson <me@iain.cx>
Tue, 31 Dec 2013 16:11:08 +0000 (16:11 +0000)
committerIain Patterson <me@iain.cx>
Wed, 1 Jan 2014 12:41:59 +0000 (12:41 +0000)
New function to set or unset a service's description.

service.cpp
service.h

index cccfa2b..d402ab3 100644 (file)
@@ -65,6 +65,22 @@ QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *service_name, SC_HANDLE
   return qsc;\r
 }\r
 \r
+int set_service_description(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR *buffer) {\r
+  SERVICE_DESCRIPTION description;\r
+  ZeroMemory(&description, sizeof(description));\r
+  /*\r
+    lpDescription must be NULL if we aren't changing, the new description\r
+    or "".\r
+  */\r
+  if (buffer && buffer[0]) description.lpDescription = buffer;\r
+  else description.lpDescription = _T("");\r
+\r
+  if (ChangeServiceConfig2(service_handle, SERVICE_CONFIG_DESCRIPTION, &description)) return 0;\r
+\r
+  log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_DESCRIPTION_FAILED, service_name, error_string(GetLastError()), 0);\r
+  return 1;\r
+}\r
+\r
 int get_service_description(const TCHAR *service_name, SC_HANDLE service_handle, unsigned long len, TCHAR *buffer) {\r
   if (! buffer) return 1;\r
 \r
@@ -589,13 +605,7 @@ int edit_service(nssm_service_t *service, bool editing) {
   }\r
 \r
   if (service->description[0] || editing) {\r
-    SERVICE_DESCRIPTION description;\r
-    ZeroMemory(&description, sizeof(description));\r
-    if (service->description[0]) description.lpDescription = service->description;\r
-    else description.lpDescription = 0;\r
-    if (! ChangeServiceConfig2(service->handle, SERVICE_CONFIG_DESCRIPTION, &description)) {\r
-      log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_DESCRIPTION_FAILED, service->name, error_string(GetLastError()), 0);\r
-    }\r
+    set_service_description(service->name, service->handle, service->description);\r
   }\r
 \r
   SERVICE_DELAYED_AUTO_START_INFO delayed;\r
index d4d2e0f..a7914a1 100644 (file)
--- a/service.h
+++ b/service.h
@@ -90,6 +90,7 @@ void set_nssm_service_defaults(nssm_service_t *);
 void cleanup_nssm_service(nssm_service_t *);\r
 SC_HANDLE open_service_manager();\r
 QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE);\r
+int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *);\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