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;
}
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(

View File

@ -3,6 +3,16 @@
#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
{
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);

View File

@ -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();