From 048ecc8a899c842a6d6968a67733330876a933a5 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Wed, 1 Jan 2014 16:00:17 +0000 Subject: [PATCH] Added open_service(). New function to open a service either by name or display name. Since no service's display name may conflict with any other service's display name or key name, we can safely use a display name as a key when opening a service. OpenService() expects a service name so if it fails with status ERROR_SERVICE_DOES_NOT_EXIST we can enumerate all services and look for one whose display name matches the argument passed to open_service(), and open it. As a side effect we can optionally store the canonical name in a separate (or not) buffer. --- README.txt | 9 ++++-- messages.mc | Bin 119012 -> 119752 bytes service.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----- service.h | 1 + 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/README.txt b/README.txt index 9a9a11e..6955ba4 100644 --- a/README.txt +++ b/README.txt @@ -63,11 +63,14 @@ Since version 2.22, NSSM can manage existing services. Usage ----- -In the usage notes below, arguments to the program may be written in angle -brackets and/or square brackets. means you must insert the -appropriate string and [] means the string is optional. See the +In the usage notes below, arguments to the program may be written in angle +brackets and/or square brackets. means you must insert the +appropriate string and [] means the string is optional. See the examples below... +Note that everywhere appears you may substitute the +service's display name. + Installation using the GUI -------------------------- diff --git a/messages.mc b/messages.mc index 1be1d0aaa7714d9bec06f6135647177fcf41279d..a53eee184f16bbf06da78888fbd9801fa8cdd7f9 100644 GIT binary patch delta 280 zcmaE|kp0AR_6;4SqW%m845handle = OpenService(services, service->name, SC_MANAGER_ALL_ACCESS); + service->handle = open_service(services, service->name, service->name, _countof(service->name)); if (! service->handle) { CloseServiceHandle(services); - print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED); return 3; } @@ -806,6 +884,7 @@ int edit_service(nssm_service_t *service, bool editing) { int control_service(unsigned long control, int argc, TCHAR **argv) { if (argc < 1) return usage(1); TCHAR *service_name = argv[0]; + TCHAR canonical_name[SERVICE_NAME_LENGTH]; SC_HANDLE services = open_service_manager(); if (! services) { @@ -813,9 +892,8 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { return 2; } - SC_HANDLE service_handle = OpenService(services, service_name, SC_MANAGER_ALL_ACCESS); + SC_HANDLE service_handle = open_service(services, service_name, canonical_name, _countof(canonical_name)); if (! service_handle) { - print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED); CloseServiceHandle(services); return 3; } @@ -861,7 +939,7 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { return 0; } else { - _ftprintf(stderr, _T("%s: %s\n"), service_name, error_string(error)); + _ftprintf(stderr, _T("%s: %s\n"), canonical_name, error_string(error)); return 1; } } @@ -894,9 +972,8 @@ int remove_service(nssm_service_t *service) { } /* Try to open the service */ - service->handle = OpenService(services, service->name, SC_MANAGER_ALL_ACCESS); + service->handle = open_service(services, service->name, service->name, _countof(service->name)); if (! service->handle) { - print_message(stderr, NSSM_MESSAGE_OPENSERVICE_FAILED); CloseServiceHandle(services); return 3; } diff --git a/service.h b/service.h index 29c237e..e4049db 100644 --- a/service.h +++ b/service.h @@ -96,6 +96,7 @@ nssm_service_t *alloc_nssm_service(); void set_nssm_service_defaults(nssm_service_t *); void cleanup_nssm_service(nssm_service_t *); SC_HANDLE open_service_manager(); +SC_HANDLE open_service(SC_HANDLE, TCHAR *, TCHAR *, unsigned long); QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE); int set_service_description(const TCHAR *, SC_HANDLE, TCHAR *); int get_service_description(const TCHAR *, SC_HANDLE, unsigned long, TCHAR *); -- 2.7.4