diff --git a/src/ui/util/service/servicehandle.cpp b/src/ui/util/service/servicehandle.cpp index 638f4b1d..c3597d59 100644 --- a/src/ui/util/service/servicehandle.cpp +++ b/src/ui/util/service/servicehandle.cpp @@ -53,13 +53,20 @@ bool ServiceHandle::stopService() return false; } -void ServiceHandle::createService(const wchar_t *serviceName, const wchar_t *serviceDisplay, - const wchar_t *serviceGroup, const wchar_t *dependencies, const QString &command) +bool ServiceHandle::createService(const CreateServiceArg &csa) { - m_serviceHandle = qintptr(CreateServiceW(SC_HANDLE(m_managerHandle), serviceName, - serviceDisplay, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, - SERVICE_ERROR_NORMAL, (LPCWSTR) command.utf16(), serviceGroup, 0, dependencies, nullptr, + m_serviceHandle = qintptr(CreateServiceW(SC_HANDLE(m_managerHandle), csa.serviceName, + csa.serviceDisplay, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, + SERVICE_ERROR_NORMAL, csa.command, csa.serviceGroup, 0, csa.dependencies, nullptr, nullptr)); + + if (!isServiceOpened()) + return false; + + SERVICE_DESCRIPTION sd = { (LPWSTR) csa.serviceDescription }; + ChangeServiceConfig2(SC_HANDLE(m_serviceHandle), SERVICE_CONFIG_DESCRIPTION, &sd); + + return true; } bool ServiceHandle::deleteService() @@ -67,14 +74,7 @@ bool ServiceHandle::deleteService() return DeleteService(SC_HANDLE(m_serviceHandle)); } -void ServiceHandle::setServiceDescription(const wchar_t *serviceDescription) -{ - SERVICE_DESCRIPTION sd = { (LPWSTR) serviceDescription }; - - ChangeServiceConfig2(SC_HANDLE(m_serviceHandle), SERVICE_CONFIG_DESCRIPTION, &sd); -} - -void ServiceHandle::setupServiceRestartConfig() +bool ServiceHandle::setupServiceRestartConfig() { constexpr int actionsCount = 3; @@ -93,7 +93,7 @@ void ServiceHandle::setupServiceRestartConfig() sfa.cActions = actionsCount; sfa.lpsaActions = actions; - ChangeServiceConfig2(SC_HANDLE(m_serviceHandle), SERVICE_CONFIG_FAILURE_ACTIONS, &sfa); + return ChangeServiceConfig2(SC_HANDLE(m_serviceHandle), SERVICE_CONFIG_FAILURE_ACTIONS, &sfa); } void ServiceHandle::openService( diff --git a/src/ui/util/service/servicehandle.h b/src/ui/util/service/servicehandle.h index c011e1f3..2af1eeba 100644 --- a/src/ui/util/service/servicehandle.h +++ b/src/ui/util/service/servicehandle.h @@ -3,6 +3,16 @@ #include +struct CreateServiceArg +{ + const wchar_t *serviceName; + const wchar_t *serviceDisplay; + const wchar_t *serviceDescription; + const wchar_t *serviceGroup; + const wchar_t *dependencies; + const wchar_t *command; +}; + class ServiceHandle final { public: @@ -18,12 +28,10 @@ public: bool startService(); bool stopService(); - void createService(const wchar_t *serviceName, const wchar_t *serviceDisplay, - const wchar_t *serviceGroup, const wchar_t *dependencies, const QString &command); + bool createService(const CreateServiceArg &csa); bool deleteService(); - void setServiceDescription(const wchar_t *serviceDescription); - void setupServiceRestartConfig(); + bool setupServiceRestartConfig(); private: void openService(const wchar_t *serviceName, quint32 managerAccess, quint32 serviceAccess); diff --git a/src/ui/util/startuputil.cpp b/src/ui/util/startuputil.cpp index 3dc7c9d6..4e153b32 100644 --- a/src/ui/util/startuputil.cpp +++ b/src/ui/util/startuputil.cpp @@ -92,22 +92,16 @@ void removeAutorunForAllUsers() removeAutorunForUser(regAllUsersRoot, regAllUsersRun); } -bool installService(const wchar_t *serviceName, const wchar_t *serviceDisplay, - const wchar_t *serviceDescription, const wchar_t *serviceGroup, const wchar_t *dependencies, - const QString &command) +bool installService(const CreateServiceArg &csa) { - ServiceHandle svc(serviceName, SC_MANAGER_CREATE_SERVICE); + ServiceHandle svc(csa.serviceName, SC_MANAGER_CREATE_SERVICE); if (!svc.isManagerOpened()) return false; - svc.createService(serviceName, serviceDisplay, serviceGroup, dependencies, command); - if (!svc.isServiceOpened()) + if (!svc.createService(csa)) return false; - svc.setServiceDescription(serviceDescription); - svc.setupServiceRestartConfig(); - - return true; + return svc.setupServiceRestartConfig(); } bool uninstallService(const wchar_t *serviceName) @@ -145,8 +139,16 @@ void StartupUtil::setServiceInstalled(bool install) const QString command = wrappedAppFilePath() + " --service"; - installService(serviceNameStr, serviceDisplayStr, serviceDescriptionStr, serviceGroupStr, - serviceDependenciesStr, command); + const CreateServiceArg csa = { + .serviceName = serviceNameStr, + .serviceDisplay = serviceDisplayStr, + .serviceDescription = serviceDescriptionStr, + .serviceGroup = serviceGroupStr, + .dependencies = serviceDependenciesStr, + .command = (LPCWSTR) command.utf16(), + }; + + installService(csa); startService();