2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/control.c
5 * PURPOSE: Pauses and resumes a service
6 * COPYRIGHT: Copyright 2006-2015 Ged Murphy <gedmurphy@reactos.org>
12 #define MAX_WAIT_TIME 30000
15 DoControlService(LPWSTR ServiceName
,
21 SERVICE_STATUS_PROCESS ServiceStatus
= {0};
22 SERVICE_STATUS Status
;
23 DWORD BytesNeeded
= 0;
31 /* Set the state we're interested in */
34 case SERVICE_CONTROL_PAUSE
:
35 ReqState
= SERVICE_PAUSED
;
37 case SERVICE_CONTROL_CONTINUE
:
38 ReqState
= SERVICE_RUNNING
;
41 /* Unhandled control code */
45 hSCManager
= OpenSCManagerW(NULL
,
48 if (!hSCManager
) return FALSE
;
50 hService
= OpenServiceW(hSCManager
,
52 SERVICE_PAUSE_CONTINUE
| SERVICE_INTERROGATE
| SERVICE_QUERY_STATUS
);
55 CloseServiceHandle(hSCManager
);
59 /* Send the control message to the service */
60 Result
= ControlService(hService
,
67 /* Increment the progress bar */
68 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
71 /* Get the service status */
72 Result
= QueryServiceStatusEx(hService
,
73 SC_STATUS_PROCESS_INFO
,
74 (LPBYTE
)&ServiceStatus
,
75 sizeof(SERVICE_STATUS_PROCESS
),
80 MaxWait
= MAX_WAIT_TIME
;
81 OldCheckPoint
= ServiceStatus
.dwCheckPoint
;
82 StartTickCount
= GetTickCount();
84 /* Loop until it's at the correct state */
85 while (ServiceStatus
.dwCurrentState
!= ReqState
)
87 /* Fixup the wait time */
88 WaitTime
= ServiceStatus
.dwWaitHint
/ 10;
90 if (WaitTime
< 1000) WaitTime
= 1000;
91 else if (WaitTime
> 10000) WaitTime
= 10000;
93 /* We don't wanna wait for up to 10 secs without incrementing */
94 for (i
= WaitTime
/ 1000; i
> 0; i
--)
99 /* Increment the progress bar */
100 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
104 /* Get the latest status info */
105 if (!QueryServiceStatusEx(hService
,
106 SC_STATUS_PROCESS_INFO
,
107 (LPBYTE
)&ServiceStatus
,
108 sizeof(SERVICE_STATUS_PROCESS
),
111 /* Something went wrong... */
115 /* Is the service making progress? */
116 if (ServiceStatus
.dwCheckPoint
> OldCheckPoint
)
118 /* It is, get the latest tickcount to reset the max wait time */
119 StartTickCount
= GetTickCount();
120 OldCheckPoint
= ServiceStatus
.dwCheckPoint
;
121 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
125 /* It's not, make sure we haven't exceeded our wait time */
126 if(GetTickCount() >= StartTickCount
+ MaxWait
)
128 /* We have, give up */
135 if (ServiceStatus
.dwCurrentState
== ReqState
)
141 CloseServiceHandle(hService
);
142 CloseServiceHandle(hSCManager
);