ReactOS  0.4.12-dev-43-g63b00d8
stop.c File Reference
#include "precomp.h"
Include dependency graph for stop.c:

Go to the source code of this file.

Macros

#define MAX_WAIT_TIME   30000
 

Functions

BOOL DoStopService (_In_z_ LPWSTR ServiceName, _In_opt_ HANDLE hProgress)
 

Macro Definition Documentation

◆ MAX_WAIT_TIME

#define MAX_WAIT_TIME   30000

Definition at line 12 of file stop.c.

Referenced by DoStopService().

Function Documentation

◆ DoStopService()

BOOL DoStopService ( _In_z_ LPWSTR  ServiceName,
_In_opt_ HANDLE  hProgress 
)

Definition at line 15 of file stop.c.

17 {
18  SC_HANDLE hSCManager;
19  SC_HANDLE hService;
21  DWORD BytesNeeded;
23  DWORD WaitTime;
24  DWORD Timeout;
25  BOOL bRet = FALSE;
26 
27 
28  hSCManager = OpenSCManagerW(NULL,
29  NULL,
31  if (!hSCManager) return FALSE;
32 
33  hService = OpenServiceW(hSCManager,
36  if (!hService)
37  {
38  CloseServiceHandle(hSCManager);
39  return FALSE;
40  }
41 
42  if (hProgress)
43  {
44  /* Increment the progress bar */
46  }
47 
48  /* Set the start and max wait times */
49  StartTime = GetTickCount();
50  Timeout = MAX_WAIT_TIME;
51 
52  /* Send the service the stop code */
53  if (ControlService(hService,
55  (LPSERVICE_STATUS)&ServiceStatus))
56  {
57  if (hProgress)
58  {
59  /* Increment the progress bar */
61  }
62 
63  while (ServiceStatus.dwCurrentState != SERVICE_STOPPED)
64  {
65  int i;
66  /* Fixup the wait time */
67  WaitTime = ServiceStatus.dwWaitHint / 10;
68 
69  if (WaitTime < 1000) WaitTime = 1000;
70  else if (WaitTime > 10000) WaitTime = 10000;
71 
72  /* We don't wanna wait for up to 10 secs without incrementing */
73  for (i = WaitTime / 1000; i > 0; i--)
74  {
75  Sleep(1000);
76  if (hProgress)
77  {
78  /* Increment the progress bar */
80  }
81  }
82 
83  if (QueryServiceStatusEx(hService,
85  (LPBYTE)&ServiceStatus,
86  sizeof(SERVICE_STATUS_PROCESS),
87  &BytesNeeded))
88  {
89  /* Have we exceeded our wait time? */
90  if (GetTickCount() - StartTime > Timeout)
91  {
92  /* Yep, give up */
93  break;
94  }
95  }
96  }
97 
98  /* If the service is stopped, return TRUE */
99  if (ServiceStatus.dwCurrentState == SERVICE_STOPPED)
100  {
101  bRet = TRUE;
102  }
103  }
104 
105  CloseServiceHandle(hService);
106 
107  CloseServiceHandle(hSCManager);
108 
109  return bRet;
110 }
#define TRUE
Definition: types.h:120
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
DWORD dwCurrentState
Definition: winsvc.h:100
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2829
static LARGE_INTEGER StartTime
Definition: sys_arch.c:18
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define SERVICE_STOPPED
Definition: winsvc.h:21
unsigned char * LPBYTE
Definition: typedefs.h:52
SC_HANDLE WINAPI OpenServiceW(SC_HANDLE hSCManager, LPCWSTR lpServiceName, DWORD dwDesiredAccess)
Definition: scm.c:2102
BOOL WINAPI CloseServiceHandle(SC_HANDLE hSCObject)
Definition: scm.c:576
smooth NULL
Definition: ftsmooth.c:416
#define SERVICE_QUERY_STATUS
Definition: winsvc.h:55
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTSTR ServiceName
Definition: ServiceMain.c:15
DWORD dwWaitHint
Definition: winsvc.h:105
unsigned long DWORD
Definition: ntddk_ex.h:95
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2010
SC_HANDLE hSCManager
Definition: sc.c:12
VOID IncrementProgressBar(HANDLE hProgress, UINT NewPos)
Definition: progress.c:288
static ULONG Timeout
Definition: ping.c:61
#define MAX_WAIT_TIME
Definition: stop.c:12
#define DEFAULT_STEP
Definition: precomp.h:108
#define SERVICE_STOP
Definition: winsvc.h:58
BOOL WINAPI ControlService(SC_HANDLE hService, DWORD dwControl, LPSERVICE_STATUS lpServiceStatus)
Definition: scm.c:618
SERVICE_STATUS ServiceStatus
Definition: dhcpcsvc.c:18
#define SERVICE_CONTROL_STOP
Definition: winsvc.h:36