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-2010 Ged Murphy <gedmurphy@reactos.org>
13 DoControl(PMAIN_WND_INFO Info
,
19 SERVICE_STATUS_PROCESS ServiceStatus
= {0};
20 SERVICE_STATUS Status
;
21 DWORD BytesNeeded
= 0;
22 DWORD dwStartTickCount
;
23 DWORD dwOldCheckPoint
;
29 /* Set the state we're interested in */
32 case SERVICE_CONTROL_PAUSE
:
33 dwReqState
= SERVICE_PAUSED
;
35 case SERVICE_CONTROL_CONTINUE
:
36 dwReqState
= SERVICE_RUNNING
;
39 /* Unhandled control code */
43 hSCManager
= OpenSCManager(NULL
,
48 hService
= OpenService(hSCManager
,
49 Info
->pCurrentService
->lpServiceName
,
50 SERVICE_PAUSE_CONTINUE
| SERVICE_INTERROGATE
| SERVICE_QUERY_STATUS
);
55 /* Increment the progress bar */
56 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
59 /* Send the control message to the service */
60 if (ControlService(hService
,
64 /* Get the service status */
65 if (QueryServiceStatusEx(hService
,
66 SC_STATUS_PROCESS_INFO
,
67 (LPBYTE
)&ServiceStatus
,
68 sizeof(SERVICE_STATUS_PROCESS
),
71 /* We don't want to wait for more than 30 seconds */
73 dwStartTickCount
= GetTickCount();
75 /* Loop until it's at the correct state */
76 while (ServiceStatus
.dwCurrentState
!= dwReqState
)
78 dwOldCheckPoint
= ServiceStatus
.dwCheckPoint
;
79 dwWaitTime
= ServiceStatus
.dwWaitHint
/ 10;
81 /* Get the latest status info */
82 if (!QueryServiceStatusEx(hService
,
83 SC_STATUS_PROCESS_INFO
,
84 (LPBYTE
)&ServiceStatus
,
85 sizeof(SERVICE_STATUS_PROCESS
),
88 /* Something went wrong... */
92 /* Is the service making progress? */
93 if (ServiceStatus
.dwCheckPoint
> dwOldCheckPoint
)
95 /* It is, get the latest tickcount to reset the max wait time */
96 dwStartTickCount
= GetTickCount();
97 dwOldCheckPoint
= ServiceStatus
.dwCheckPoint
;
98 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
102 /* It's not, make sure we haven't exceeded our wait time */
103 if(GetTickCount() >= dwStartTickCount
+ dwMaxWait
)
105 /* We have, give up */
110 /* Adjust the wait hint times */
111 if (dwWaitTime
< 200)
113 else if (dwWaitTime
> 10000)
116 /* Wait before trying again */
121 if (ServiceStatus
.dwCurrentState
== dwReqState
)
127 CloseServiceHandle(hService
);
130 CloseServiceHandle(hSCManager
);
138 DoPause(PMAIN_WND_INFO Info
)
143 /* Create a progress window to track the progress of the pausing service */
144 hProgress
= CreateProgressDialog(Info
->hMainWnd
,
145 IDS_PROGRESS_INFO_PAUSE
);
148 /* Set the service name and reset the progress bag */
149 InitializeProgressDialog(hProgress
, Info
->pCurrentService
->lpServiceName
);
151 /* Resume the requested service */
152 bRet
= DoControl(Info
, hProgress
, SERVICE_CONTROL_PAUSE
);
154 /* Complete and destroy the progress bar */
155 DestroyProgressDialog(hProgress
, bRet
);
163 DoResume(PMAIN_WND_INFO Info
)
168 /* Create a progress window to track the progress of the resuming service */
169 hProgress
= CreateProgressDialog(Info
->hMainWnd
,
170 IDS_PROGRESS_INFO_RESUME
);
173 /* Set the service name and reset the progress bag */
174 InitializeProgressDialog(hProgress
, Info
->pCurrentService
->lpServiceName
);
176 /* Resume the requested service */
177 bRet
= DoControl(Info
, hProgress
, SERVICE_CONTROL_CONTINUE
);
179 /* Complete and destroy the progress bar */
180 DestroyProgressDialog(hProgress
, bRet
);