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>
14 StopService(PMAIN_WND_INFO pInfo
,
16 HWND hProgress OPTIONAL
)
20 SERVICE_STATUS_PROCESS ServiceStatus
;
28 /* Set the service name and reset the progress bag */
29 InitializeProgressDialog(hProgress
, lpServiceName
);
32 hSCManager
= OpenSCManager(NULL
,
37 hService
= OpenService(hSCManager
,
39 SERVICE_STOP
| SERVICE_QUERY_STATUS
);
44 /* Increment the progress bar */
45 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
48 /* Set the wait time to 30 secs */
49 dwStartTime
= GetTickCount();
52 /* Send the service the stop code */
53 if (ControlService(hService
,
55 (LPSERVICE_STATUS
)&ServiceStatus
))
59 /* Increment the progress bar */
60 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
63 while (ServiceStatus
.dwCurrentState
!= SERVICE_STOPPED
)
65 /* Don't sleep for more than 3 seconds */
66 if (ServiceStatus
.dwWaitHint
> 3000)
67 ServiceStatus
.dwWaitHint
= 3000;
69 Sleep(ServiceStatus
.dwWaitHint
);
73 /* Increment the progress bar */
74 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
77 if (QueryServiceStatusEx(hService
,
78 SC_STATUS_PROCESS_INFO
,
79 (LPBYTE
)&ServiceStatus
,
80 sizeof(SERVICE_STATUS_PROCESS
),
83 /* Have we exceeded our wait time? */
84 if (GetTickCount() - dwStartTime
> dwTimeout
)
92 /* If the service is stopped, return TRUE */
93 if (ServiceStatus
.dwCurrentState
== SERVICE_STOPPED
)
99 CloseServiceHandle(hService
);
102 CloseServiceHandle(hSCManager
);
109 StopDependantServices(PMAIN_WND_INFO pInfo
,
110 LPWSTR lpServiceList
,
111 HWND hProgress OPTIONAL
)
116 lpStr
= lpServiceList
;
118 /* Loop through all the services in the list */
121 /* Break when we hit the double null */
122 if (*lpStr
== L
'\0' && *(lpStr
+ 1) == L
'\0')
125 /* If this isn't our first time in the loop we'll
126 have been left on a null char */
130 /* Stop the requested service */
131 bRet
= StopService(pInfo
,
135 /* Complete the progress bar if we succeeded */
138 CompleteProgressBar(hProgress
);
141 /* Move onto the next string */
142 while (*lpStr
!= L
'\0')
151 DoStop(PMAIN_WND_INFO pInfo
)
154 LPWSTR lpServiceList
;
156 BOOL bStopMainService
= TRUE
;
160 /* Does the service have any dependent services which need stopping first */
161 lpServiceList
= GetListOfServicesToStop(pInfo
->pCurrentService
->lpServiceName
);
164 /* Tag the service list to the main wnd info */
165 pInfo
->pTag
= (PVOID
)lpServiceList
;
167 /* List them and ask the user if they want to stop them */
168 if (DialogBoxParamW(hInstance
,
169 MAKEINTRESOURCEW(IDD_DLG_DEPEND_STOP
),
171 StopDependsDialogProc
,
172 (LPARAM
)pInfo
) == IDOK
)
174 /* Create a progress window to track the progress of the stopping services */
175 hProgress
= CreateProgressDialog(pInfo
->hMainWnd
,
176 IDS_PROGRESS_INFO_STOP
);
178 /* Stop all the dependant services */
179 StopDependantServices(pInfo
, lpServiceList
, hProgress
);
181 /* Now stop the requested one */
182 bRet
= StopService(pInfo
,
183 pInfo
->pCurrentService
->lpServiceName
,
186 /* We've already stopped the main service, don't try to stop it again */
187 bStopMainService
= FALSE
;
191 /* Complete and destroy the progress bar */
192 DestroyProgressDialog(hProgress
, TRUE
);
197 /* Don't stop the main service if the user selected not to */
198 bStopMainService
= FALSE
;
201 HeapFree(GetProcessHeap(),
206 /* If the service has no running dependents, then we stop it here */
207 if (bStopMainService
)
209 /* Create a progress window to track the progress of the stopping service */
210 hProgress
= CreateProgressDialog(pInfo
->hMainWnd
,
211 IDS_PROGRESS_INFO_STOP
);
213 /* Stop the requested service */
214 bRet
= StopService(pInfo
,
215 pInfo
->pCurrentService
->lpServiceName
,
220 /* Complete and destroy the progress bar */
221 DestroyProgressDialog(hProgress
, TRUE
);