@@ -23,7 +23,8 @@ ServiceControl::SupportFlags WindowsServiceControl::supportFlags() const
23
23
SupportsAutostart |
24
24
SupportsNonBlocking |
25
25
SupportsStatus |
26
- SupportsCustomCommands;
26
+ SupportsCustomCommands |
27
+ SupportsDisable;
27
28
}
28
29
29
30
bool WindowsServiceControl::serviceExists () const
@@ -92,6 +93,29 @@ bool WindowsServiceControl::isAutostartEnabled() const
92
93
}
93
94
}
94
95
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
+
95
119
QVariant WindowsServiceControl::callGenericCommand (const QByteArray &kind, const QVariantList &args)
96
120
{
97
121
if (kind == " command" ) {
@@ -187,6 +211,14 @@ bool WindowsServiceControl::resume()
187
211
188
212
bool WindowsServiceControl::enableAutostart ()
189
213
{
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
+
190
222
HandleHolder handle {svcHandle (SERVICE_CHANGE_CONFIG)};
191
223
if (!handle)
192
224
return false ;
@@ -205,11 +237,15 @@ bool WindowsServiceControl::enableAutostart()
205
237
206
238
bool WindowsServiceControl::disableAutostart ()
207
239
{
240
+ // do not change anything on a disabled service
241
+ if (!isEnabled ())
242
+ return true ;
243
+
208
244
HandleHolder handle {svcHandle (SERVICE_CHANGE_CONFIG)};
209
- if (!handle)
245
+ if (!handle)
210
246
return false ;
211
247
212
- if (ChangeServiceConfigW (handle,
248
+ if (ChangeServiceConfigW (handle,
213
249
SERVICE_NO_CHANGE,
214
250
SERVICE_DEMAND_START, // only line that actually changes stuff
215
251
SERVICE_NO_CHANGE,
@@ -221,6 +257,27 @@ bool WindowsServiceControl::disableAutostart()
221
257
}
222
258
}
223
259
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
+
224
281
SC_HANDLE WindowsServiceControl::svcHandle (DWORD permissions) const
225
282
{
226
283
if (!_manager)
0 commit comments