2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/stop.c
5 * PURPOSE: Stops running a service
6 * COPYRIGHT: Copyright 2006-2010 Ged Murphy <gedmurphy@reactos.org>
13 StopService(PMAIN_WND_INFO pInfo
,
15 HWND hProgress OPTIONAL
)
19 SERVICE_STATUS_PROCESS ServiceStatus
;
27 /* Set the service name and reset the progress bag */
28 InitializeProgressDialog(hProgress
, lpServiceName
);
31 hSCManager
= OpenSCManager(NULL
,
36 hService
= OpenService(hSCManager
,
38 SERVICE_STOP
| SERVICE_QUERY_STATUS
);
43 /* Increment the progress bar */
44 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
47 /* Set the wait time to 30 secs */
48 dwStartTime
= GetTickCount();
51 /* Send the service the stop code */
52 if (ControlService(hService
,
54 (LPSERVICE_STATUS
)&ServiceStatus
))
58 /* Increment the progress bar */
59 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
62 while (ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
)
64 /* Don't sleep for more than 3 seconds */
65 if (ServiceStatus
.dwWaitHint
> 3000)
66 ServiceStatus
.dwWaitHint
= 3000;
68 Sleep(ServiceStatus
.dwWaitHint
);
72 /* Increment the progress bar */
73 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
76 if (QueryServiceStatusEx(hService
,
77 SC_STATUS_PROCESS_INFO
,
78 (LPBYTE
)&ServiceStatus
,
79 sizeof(SERVICE_STATUS_PROCESS
),
82 /* Have we exceeded our wait time? */
83 if (GetTickCount() - dwStartTime
> dwTimeout
)
91 /* If the service is stopped, return TRUE */
92 if (ServiceStatus
.dwCurrentState
== SERVICE_STOPPED
)
98 CloseServiceHandle(hService
);
101 CloseServiceHandle(hSCManager
);
108 StopDependantServices(PMAIN_WND_INFO pInfo
,
109 LPWSTR lpServiceList
,
110 HWND hProgress OPTIONAL
)
115 lpStr
= lpServiceList
;
117 /* Loop through all the services in the list */
120 /* Break when we hit the double null */
121 if (*lpStr
== L
'\0' && *(lpStr
+ 1) == L
'\0')
124 /* If this isn't our first time in the loop we'll
125 have been left on a null char */
129 /* Stop the requested service */
130 bRet
= StopService(pInfo
,
134 /* Complete the progress bar if we succeeded */
137 CompleteProgressBar(hProgress
);
140 /* Move onto the next string */
141 while (*lpStr
!= L
'\0')
149 DoStop(PMAIN_WND_INFO pInfo
)
152 LPWSTR lpServiceList
;
154 BOOL bStopMainService
= TRUE
;
158 /* Does the service have any dependent services which need stopping first */
159 lpServiceList
= GetListOfServicesToStop(pInfo
->pCurrentService
->lpServiceName
);
162 /* Tag the service list to the main wnd info */
163 pInfo
->pTag
= (PVOID
)lpServiceList
;
165 /* List them and ask the user if they want to stop them */
166 if (DialogBoxParamW(hInstance
,
167 MAKEINTRESOURCEW(IDD_DLG_DEPEND_STOP
),
169 StopDependsDialogProc
,
170 (LPARAM
)pInfo
) == IDOK
)
172 /* Create a progress window to track the progress of the stopping services */
173 hProgress
= CreateProgressDialog(pInfo
->hMainWnd
,
174 IDS_PROGRESS_INFO_STOP
);
176 /* Stop all the dependant services */
177 StopDependantServices(pInfo
, lpServiceList
, hProgress
);
179 /* Now stop the requested one */
180 bRet
= StopService(pInfo
,
181 pInfo
->pCurrentService
->lpServiceName
,
184 /* We've already stopped the main service, don't try to stop it again */
185 bStopMainService
= FALSE
;
189 /* Complete and destroy the progress bar */
190 DestroyProgressDialog(hProgress
, TRUE
);
195 /* Don't stop the main service if the user selected not to */
196 bStopMainService
= FALSE
;
199 HeapFree(GetProcessHeap(),
204 /* If the service has no running dependents, then we stop it here */
205 if (bStopMainService
)
207 /* Create a progress window to track the progress of the stopping service */
208 hProgress
= CreateProgressDialog(pInfo
->hMainWnd
,
209 IDS_PROGRESS_INFO_STOP
);
211 /* Stop the requested service */
212 bRet
= StopService(pInfo
,
213 pInfo
->pCurrentService
->lpServiceName
,
218 /* Complete and destroy the progress bar */
219 DestroyProgressDialog(hProgress
, TRUE
);