Added get_service_description().
authorIain Patterson <me@iain.cx>
Mon, 30 Dec 2013 13:43:15 +0000 (13:43 +0000)
committerIain Patterson <me@iain.cx>
Wed, 1 Jan 2014 12:41:55 +0000 (12:41 +0000)
New function to get the service description.

service.cpp
service.h

index e3824ff..781f269 100644 (file)
@@ -65,6 +65,39 @@ QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *service_name, SC_HANDLE
   return qsc;\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
+  unsigned long bufsize;\r
+  QueryServiceConfig2(service_handle, SERVICE_CONFIG_DESCRIPTION, 0, 0, &bufsize);\r
+  unsigned long error = GetLastError();\r
+  if (error == ERROR_INSUFFICIENT_BUFFER) {\r
+    SERVICE_DESCRIPTION *description = (SERVICE_DESCRIPTION *) HeapAlloc(GetProcessHeap(), 0, bufsize);\r
+    if (! description) {\r
+      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("SERVICE_CONFIG_DESCRIPTION"), _T("get_service_description()"));\r
+      return 2;\r
+    }\r
+\r
+    if (QueryServiceConfig2(service_handle, SERVICE_CONFIG_DESCRIPTION, (unsigned char *) description, bufsize, &bufsize)) {\r
+      if (description->lpDescription) _sntprintf_s(buffer, len, _TRUNCATE, _T("%s"), description->lpDescription);\r
+      else ZeroMemory(buffer, len * sizeof(TCHAR));\r
+      HeapFree(GetProcessHeap(), 0, description);\r
+      return 0;\r
+    }\r
+    else {\r
+      HeapFree(GetProcessHeap(), 0, description);\r
+      print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service_name, _T("SERVICE_CONFIG_DESCRIPTION"), error_string(error));\r
+      return 3;\r
+    }\r
+  }\r
+  else {\r
+    print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service_name, _T("SERVICE_CONFIG_DESCRIPTION"), error_string(error));\r
+    return 4;\r
+  }\r
+\r
+  return 0;\r
+}\r
+\r
 int get_service_startup(const TCHAR *service_name, SC_HANDLE service_handle, const QUERY_SERVICE_CONFIG *qsc, unsigned long *startup) {\r
   if (! qsc) return 1;\r
 \r
@@ -368,7 +401,6 @@ int pre_edit_service(int argc, TCHAR **argv) {
 \r
   /* Get system details. */\r
   unsigned long bufsize;\r
-  unsigned long error;\r
   QUERY_SERVICE_CONFIG *qsc = query_service_config(service->name, service->handle);\r
   if (! qsc) {\r
     CloseHandle(service->handle);\r
@@ -418,33 +450,9 @@ int pre_edit_service(int argc, TCHAR **argv) {
   HeapFree(GetProcessHeap(), 0, qsc);\r
 \r
   /* Get extended system details. */\r
-  QueryServiceConfig2(service->handle, SERVICE_CONFIG_DESCRIPTION, 0, 0, &bufsize);\r
-  error = GetLastError();\r
-  if (error == ERROR_INSUFFICIENT_BUFFER) {\r
-    SERVICE_DESCRIPTION *description = (SERVICE_DESCRIPTION *) HeapAlloc(GetProcessHeap(), 0, bufsize);\r
-    if (! description) {\r
-      CloseHandle(service->handle);\r
-      CloseServiceHandle(services);\r
-      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("SERVICE_CONFIG_DESCRIPTION"), _T("pre_edit_service()"));\r
-      return 6;\r
-    }\r
-\r
-    if (QueryServiceConfig2(service->handle, SERVICE_CONFIG_DESCRIPTION, (unsigned char *) description, bufsize, &bufsize)) {\r
-     if (description->lpDescription) _sntprintf_s(service->description, _countof(service->description), _TRUNCATE, _T("%s"), description->lpDescription);\r
-      HeapFree(GetProcessHeap(), 0, description);\r
-    }\r
-    else {\r
-      HeapFree(GetProcessHeap(), 0, description);\r
-      CloseHandle(service->handle);\r
-      CloseServiceHandle(services);\r
-      print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service->name, _T("SERVICE_CONFIG_DELAYED_AUTO_START_INFO"), error_string(error));\r
-      return 6;\r
-    }\r
-  }\r
-  else {\r
+  if (get_service_description(service->name, service->handle, _countof(service->description), service->description)) {\r
     CloseHandle(service->handle);\r
     CloseServiceHandle(services);\r
-    print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG2_FAILED, service->name, _T("SERVICE_CONFIG_DELAYED_AUTO_START_INFO"), error_string(error));\r
     return 6;\r
   }\r
 \r
index ca06a2e..18a9850 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 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 pre_install_service(int, TCHAR **);\r
 int pre_remove_service(int, TCHAR **);\r