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-2015 Ged Murphy <gedmurphy@reactos.org>
12 #define MAX_WAIT_TIME 30000
15 DoStopService(_In_z_ LPWSTR ServiceName
,
16 _In_opt_ HANDLE hProgress
)
20 SERVICE_STATUS_PROCESS ServiceStatus
;
28 hSCManager
= OpenSCManagerW(NULL
,
31 if (!hSCManager
) return FALSE
;
33 hService
= OpenServiceW(hSCManager
,
35 SERVICE_STOP
| SERVICE_QUERY_STATUS
);
38 CloseServiceHandle(hSCManager
);
44 /* Increment the progress bar */
45 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
48 /* Set the start and max wait times */
49 StartTime
= GetTickCount();
50 Timeout
= MAX_WAIT_TIME
;
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
)
66 /* Fixup the wait time */
67 WaitTime
= ServiceStatus
.dwWaitHint
/ 10;
69 if (WaitTime
< 1000) WaitTime
= 1000;
70 else if (WaitTime
> 10000) WaitTime
= 10000;
72 /* We don't wanna wait for up to 10 secs without incrementing */
73 for (i
= WaitTime
/ 1000; i
> 0; i
--)
78 /* Increment the progress bar */
79 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
83 if (QueryServiceStatusEx(hService
,
84 SC_STATUS_PROCESS_INFO
,
85 (LPBYTE
)&ServiceStatus
,
86 sizeof(SERVICE_STATUS_PROCESS
),
89 /* Have we exceeded our wait time? */
90 if (GetTickCount() - StartTime
> Timeout
)
98 /* If the service is stopped, return TRUE */
99 if (ServiceStatus
.dwCurrentState
== SERVICE_STOPPED
)
105 CloseServiceHandle(hService
);
107 CloseServiceHandle(hSCManager
);