UI: ServiceHandle: Simplify createService()

This commit is contained in:
Nodir Temirkhodjaev 2024-02-24 13:33:27 +03:00
parent 4536d858c4
commit 70ea62b220
3 changed files with 40 additions and 30 deletions

View File

@ -53,13 +53,20 @@ bool ServiceHandle::stopService()
return false; return false;
} }
void ServiceHandle::createService(const wchar_t *serviceName, const wchar_t *serviceDisplay, bool ServiceHandle::createService(const CreateServiceArg &csa)
const wchar_t *serviceGroup, const wchar_t *dependencies, const QString &command)
{ {
m_serviceHandle = qintptr(CreateServiceW(SC_HANDLE(m_managerHandle), serviceName, m_serviceHandle = qintptr(CreateServiceW(SC_HANDLE(m_managerHandle), csa.serviceName,
serviceDisplay, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, csa.serviceDisplay, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, (LPCWSTR) command.utf16(), serviceGroup, 0, dependencies, nullptr, SERVICE_ERROR_NORMAL, csa.command, csa.serviceGroup, 0, csa.dependencies, nullptr,
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() bool ServiceHandle::deleteService()
@ -67,14 +74,7 @@ bool ServiceHandle::deleteService()
return DeleteService(SC_HANDLE(m_serviceHandle)); return DeleteService(SC_HANDLE(m_serviceHandle));
} }
void ServiceHandle::setServiceDescription(const wchar_t *serviceDescription) bool ServiceHandle::setupServiceRestartConfig()
{
SERVICE_DESCRIPTION sd = { (LPWSTR) serviceDescription };
ChangeServiceConfig2(SC_HANDLE(m_serviceHandle), SERVICE_CONFIG_DESCRIPTION, &sd);
}
void ServiceHandle::setupServiceRestartConfig()
{ {
constexpr int actionsCount = 3; constexpr int actionsCount = 3;
@ -93,7 +93,7 @@ void ServiceHandle::setupServiceRestartConfig()
sfa.cActions = actionsCount; sfa.cActions = actionsCount;
sfa.lpsaActions = actions; 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( void ServiceHandle::openService(

View File

@ -3,6 +3,16 @@
#include <QObject> #include <QObject>
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 class ServiceHandle final
{ {
public: public:
@ -18,12 +28,10 @@ public:
bool startService(); bool startService();
bool stopService(); bool stopService();
void createService(const wchar_t *serviceName, const wchar_t *serviceDisplay, bool createService(const CreateServiceArg &csa);
const wchar_t *serviceGroup, const wchar_t *dependencies, const QString &command);
bool deleteService(); bool deleteService();
void setServiceDescription(const wchar_t *serviceDescription); bool setupServiceRestartConfig();
void setupServiceRestartConfig();
private: private:
void openService(const wchar_t *serviceName, quint32 managerAccess, quint32 serviceAccess); void openService(const wchar_t *serviceName, quint32 managerAccess, quint32 serviceAccess);

View File

@ -92,22 +92,16 @@ void removeAutorunForAllUsers()
removeAutorunForUser(regAllUsersRoot, regAllUsersRun); removeAutorunForUser(regAllUsersRoot, regAllUsersRun);
} }
bool installService(const wchar_t *serviceName, const wchar_t *serviceDisplay, bool installService(const CreateServiceArg &csa)
const wchar_t *serviceDescription, const wchar_t *serviceGroup, const wchar_t *dependencies,
const QString &command)
{ {
ServiceHandle svc(serviceName, SC_MANAGER_CREATE_SERVICE); ServiceHandle svc(csa.serviceName, SC_MANAGER_CREATE_SERVICE);
if (!svc.isManagerOpened()) if (!svc.isManagerOpened())
return false; return false;
svc.createService(serviceName, serviceDisplay, serviceGroup, dependencies, command); if (!svc.createService(csa))
if (!svc.isServiceOpened())
return false; return false;
svc.setServiceDescription(serviceDescription); return svc.setupServiceRestartConfig();
svc.setupServiceRestartConfig();
return true;
} }
bool uninstallService(const wchar_t *serviceName) bool uninstallService(const wchar_t *serviceName)
@ -145,8 +139,16 @@ void StartupUtil::setServiceInstalled(bool install)
const QString command = wrappedAppFilePath() + " --service"; const QString command = wrappedAppFilePath() + " --service";
installService(serviceNameStr, serviceDisplayStr, serviceDescriptionStr, serviceGroupStr, const CreateServiceArg csa = {
serviceDependenciesStr, command); .serviceName = serviceNameStr,
.serviceDisplay = serviceDisplayStr,
.serviceDescription = serviceDescriptionStr,
.serviceGroup = serviceGroupStr,
.dependencies = serviceDependenciesStr,
.command = (LPCWSTR) command.utf16(),
};
installService(csa);
startService(); startService();