Skip to content

Commit dc7dcfa

Browse files
committed
added enabling to windows service plugin
1 parent 01425d1 commit dc7dcfa

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

src/plugins/servicebackends/windows/windowsservicecontrol.cpp

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ ServiceControl::SupportFlags WindowsServiceControl::supportFlags() const
2323
SupportsAutostart |
2424
SupportsNonBlocking |
2525
SupportsStatus |
26-
SupportsCustomCommands;
26+
SupportsCustomCommands |
27+
SupportsDisable;
2728
}
2829

2930
bool WindowsServiceControl::serviceExists() const
@@ -92,6 +93,29 @@ bool WindowsServiceControl::isAutostartEnabled() const
9293
}
9394
}
9495

96+
bool WindowsServiceControl::isEnabled() const
97+
{
98+
HandleHolder handle {svcHandle(SERVICE_QUERY_CONFIG)};
99+
if(!handle)
100+
return true; // assume enabled by default
101+
102+
DWORD sizeNeeded = 0;
103+
QueryServiceConfigW(handle, nullptr, 0, &sizeNeeded);
104+
QByteArray cData{static_cast<int>(sizeNeeded), '\0'};
105+
auto config = reinterpret_cast<LPQUERY_SERVICE_CONFIGW>(cData.data());
106+
if(!QueryServiceConfigW(handle, config, cData.size(), &sizeNeeded)) {
107+
setWinError(tr("Failed to query service status with error: %1"));
108+
return true; // assume enabled by default
109+
}
110+
111+
switch(config->dwStartType) {
112+
case SERVICE_DISABLED:
113+
return false;
114+
default:
115+
return true;
116+
}
117+
}
118+
95119
QVariant WindowsServiceControl::callGenericCommand(const QByteArray &kind, const QVariantList &args)
96120
{
97121
if(kind == "command") {
@@ -187,6 +211,14 @@ bool WindowsServiceControl::resume()
187211

188212
bool WindowsServiceControl::enableAutostart()
189213
{
214+
// do not change anything on a disabled service
215+
if(!isEnabled())
216+
return false;
217+
218+
// If autostart is already enabled, keep it as is, i.e. do not change the autostart type
219+
if(isAutostartEnabled())
220+
return true;
221+
190222
HandleHolder handle {svcHandle(SERVICE_CHANGE_CONFIG)};
191223
if(!handle)
192224
return false;
@@ -205,11 +237,15 @@ bool WindowsServiceControl::enableAutostart()
205237

206238
bool WindowsServiceControl::disableAutostart()
207239
{
240+
// do not change anything on a disabled service
241+
if(!isEnabled())
242+
return true;
243+
208244
HandleHolder handle {svcHandle(SERVICE_CHANGE_CONFIG)};
209-
if(!handle)
245+
if (!handle)
210246
return false;
211247

212-
if(ChangeServiceConfigW(handle,
248+
if (ChangeServiceConfigW(handle,
213249
SERVICE_NO_CHANGE,
214250
SERVICE_DEMAND_START, // only line that actually changes stuff
215251
SERVICE_NO_CHANGE,
@@ -221,6 +257,27 @@ bool WindowsServiceControl::disableAutostart()
221257
}
222258
}
223259

260+
bool WindowsServiceControl::setEnabled(bool enabled)
261+
{
262+
if(enabled == isEnabled())
263+
return true;
264+
265+
HandleHolder handle {svcHandle(SERVICE_CHANGE_CONFIG)};
266+
if (!handle)
267+
return false;
268+
269+
if (ChangeServiceConfigW(handle,
270+
SERVICE_NO_CHANGE,
271+
enabled ? SERVICE_DEMAND_START : SERVICE_DISABLED, // only line that actually changes stuff
272+
SERVICE_NO_CHANGE,
273+
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr)) {
274+
return true;
275+
} else {
276+
setWinError(tr("Failed to enable/disable service with error: %1"));
277+
return false;
278+
}
279+
}
280+
224281
SC_HANDLE WindowsServiceControl::svcHandle(DWORD permissions) const
225282
{
226283
if(!_manager)

src/plugins/servicebackends/windows/windowsservicecontrol.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ class WindowsServiceControl : public QtService::ServiceControl
1616
bool serviceExists() const override;
1717
ServiceStatus status() const override;
1818
bool isAutostartEnabled() const override;
19+
bool isEnabled() const override;
1920
QVariant callGenericCommand(const QByteArray &kind, const QVariantList &args) override;
2021

21-
public slots:
22+
public Q_SLOTS:
2223
bool start() override;
2324
bool stop() override;
2425
bool pause() override;
2526
bool resume() override;
2627
bool enableAutostart() override;
2728
bool disableAutostart() override;
29+
bool setEnabled(bool enabled) override;
2830

2931
private:
3032
class HandleHolder {

0 commit comments

Comments
 (0)