Moved QueryServiceConfig() to a separate function.
authorIain Patterson <me@iain.cx>
Mon, 30 Dec 2013 10:56:19 +0000 (10:56 +0000)
committerIain Patterson <me@iain.cx>
Wed, 1 Jan 2014 12:41:51 +0000 (12:41 +0000)
The new function query_service_config() will return a pointer to a
QUERY_SERVICE_CONFIG data structure.

service.cpp
service.h

index 2f1ceb0..eec2261 100644 (file)
@@ -37,6 +37,34 @@ SC_HANDLE open_service_manager() {
   return ret;\r
 }\r
 \r
+QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *service_name, SC_HANDLE service_handle) {\r
+  QUERY_SERVICE_CONFIG *qsc;\r
+  unsigned long bufsize;\r
+  unsigned long error;\r
+\r
+  QueryServiceConfig(service_handle, 0, 0, &bufsize);\r
+  error = GetLastError();\r
+  if (error == ERROR_INSUFFICIENT_BUFFER) {\r
+    qsc = (QUERY_SERVICE_CONFIG *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufsize);\r
+    if (! qsc) {\r
+      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("QUERY_SERVICE_CONFIG"), _T("query_service_config()"), 0);\r
+      return 0;\r
+    }\r
+  }\r
+  else {\r
+    print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG_FAILED, service_name, error_string(error), 0);\r
+    return 0;\r
+  }\r
+\r
+  if (! QueryServiceConfig(service_handle, qsc, bufsize, &bufsize)) {\r
+    HeapFree(GetProcessHeap(), 0, qsc);\r
+    print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG_FAILED, service_name, error_string(GetLastError()), 0);\r
+    return 0;\r
+  }\r
+\r
+  return qsc;\r
+}\r
+\r
 static int grant_logon_as_service(const TCHAR *username) {\r
   if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return 0;\r
 \r
@@ -297,29 +325,10 @@ int pre_edit_service(int argc, TCHAR **argv) {
   /* Get system details. */\r
   unsigned long bufsize;\r
   unsigned long error;\r
-  QUERY_SERVICE_CONFIG *qsc;\r
-\r
-  QueryServiceConfig(service->handle, 0, 0, &bufsize);\r
-  error = GetLastError();\r
-  if (error == ERROR_INSUFFICIENT_BUFFER) {\r
-    qsc = (QUERY_SERVICE_CONFIG *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, bufsize);\r
-    if (! qsc) {\r
-      print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("QUERY_SERVICE_CONFIG"), _T("pre_edit_service()"), 0);\r
-      return 4;\r
-    }\r
-  }\r
-  else {\r
-    CloseHandle(service->handle);\r
-    CloseServiceHandle(services);\r
-    print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG_FAILED, service->name, error_string(error), 0);\r
-    return 4;\r
-  }\r
-\r
-  if (! QueryServiceConfig(service->handle, qsc, bufsize, &bufsize)) {\r
-    HeapFree(GetProcessHeap(), 0, qsc);\r
+  QUERY_SERVICE_CONFIG *qsc = query_service_config(service->name, service->handle);\r
+  if (! qsc) {\r
     CloseHandle(service->handle);\r
     CloseServiceHandle(services);\r
-    print_message(stderr, NSSM_MESSAGE_QUERYSERVICECONFIG_FAILED, service->name, error_string(GetLastError()), 0);\r
     return 4;\r
   }\r
 \r
index d4ec8ef..596b82b 100644 (file)
--- a/service.h
+++ b/service.h
@@ -89,6 +89,7 @@ nssm_service_t *alloc_nssm_service();
 void set_nssm_service_defaults(nssm_service_t *);\r
 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 pre_install_service(int, TCHAR **);\r
 int pre_remove_service(int, TCHAR **);\r
 int pre_edit_service(int, TCHAR **);\r