\r
return ret;\r
}\r
+\r
+int list_nssm_services() {\r
+ /* Open service manager. */\r
+ SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE);\r
+ if (! services) {\r
+ print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED);\r
+ return 1;\r
+ }\r
+\r
+ unsigned long bufsize, required, count, i;\r
+ unsigned long resume = 0;\r
+ EnumServicesStatus(services, SERVICE_WIN32, SERVICE_STATE_ALL, 0, 0, &required, &count, &resume);\r
+ unsigned long error = GetLastError();\r
+ if (error != ERROR_MORE_DATA) {\r
+ print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError()));\r
+ return 2;\r
+ }\r
+\r
+ ENUM_SERVICE_STATUS *status = (ENUM_SERVICE_STATUS *) HeapAlloc(GetProcessHeap(), 0, required);\r
+ if (! status) {\r
+ print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("ENUM_SERVICE_STATUS"), _T("list_nssm_services()"));\r
+ return 3;\r
+ }\r
+\r
+ bufsize = required;\r
+ while (true) {\r
+ int ret = EnumServicesStatus(services, SERVICE_WIN32, SERVICE_STATE_ALL, status, bufsize, &required, &count, &resume);\r
+ if (! ret) {\r
+ error = GetLastError();\r
+ if (error != ERROR_MORE_DATA) {\r
+ HeapFree(GetProcessHeap(), 0, status);\r
+ print_message(stderr, NSSM_MESSAGE_ENUMSERVICESSTATUS_FAILED, error_string(GetLastError()));\r
+ return 4;\r
+ }\r
+ }\r
+\r
+ for (i = 0; i < count; i++) {\r
+ /* Try to get the service parameters. */\r
+ nssm_service_t *service = alloc_nssm_service();\r
+ if (! service) {\r
+ HeapFree(GetProcessHeap(), 0, status);\r
+ print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("nssm_service_t"), _T("list_nssm_services()"));\r
+ return 5;\r
+ }\r
+ _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), status[i].lpServiceName);\r
+\r
+ get_parameters(service, 0);\r
+ /* We manage the service if we have an Application. */\r
+ if (service->exe[0]) _tprintf(_T("%s\n"), service->name);\r
+\r
+ cleanup_nssm_service(service);\r
+ }\r
+\r
+ if (ret) break;\r
+ }\r
+\r
+ HeapFree(GetProcessHeap(), 0, status);\r
+ return 0;\r
+}\r