ReactOS  0.4.12-dev-75-g00dd17e
start.c File Reference
#include "precomp.h"
Include dependency graph for start.c:

Go to the source code of this file.

Macros

#define MAX_WAIT_TIME   30000
 

Functions

BOOL DoStartService (LPWSTR ServiceName, HANDLE hProgress, LPWSTR lpStartParams)
 

Macro Definition Documentation

◆ MAX_WAIT_TIME

#define MAX_WAIT_TIME   30000

Definition at line 12 of file start.c.

Referenced by DoStartService().

Function Documentation

◆ DoStartService()

BOOL DoStartService ( LPWSTR  ServiceName,
HANDLE  hProgress,
LPWSTR  lpStartParams 
)

Definition at line 15 of file start.c.

Referenced by ActionThread().

18 {
19  SC_HANDLE hSCManager;
20  SC_HANDLE hService;
22  DWORD BytesNeeded = 0;
23  DWORD StartTickCount;
24  DWORD OldCheckPoint;
25  DWORD WaitTime;
26  DWORD MaxWait;
27  BOOL Result = FALSE;
28 
29  BOOL bWhiteSpace = TRUE;
30  LPWSTR lpChar;
31  DWORD dwArgsCount = 0;
32  LPCWSTR *lpArgsVector = NULL;
33 
34  if (lpStartParams != NULL)
35  {
36  /* Count the number of arguments */
37  lpChar = lpStartParams;
38  while (*lpChar != 0)
39  {
40  if (iswspace(*lpChar))
41  {
42  bWhiteSpace = TRUE;
43  }
44  else
45  {
46  if (bWhiteSpace != FALSE)
47  {
48  dwArgsCount++;
49  bWhiteSpace = FALSE;
50  }
51  }
52 
53  lpChar++;
54  }
55 
56  /*
57  * Allocate the arguments vector.
58  * Do not add the service name here because services.exe does it for us!
59  */
60  lpArgsVector = LocalAlloc(LMEM_FIXED, dwArgsCount * sizeof(LPCWSTR));
61  if (!lpArgsVector)
62  return FALSE;
63 
64  /* Fill the arguments vector */
65  dwArgsCount = 0;
66  bWhiteSpace = TRUE;
67  lpChar = lpStartParams;
68  while (*lpChar != 0)
69  {
70  if (iswspace(*lpChar))
71  {
72  *lpChar = 0;
73  bWhiteSpace = TRUE;
74  }
75  else
76  {
77  if (bWhiteSpace != FALSE)
78  {
79  lpArgsVector[dwArgsCount] = lpChar;
80  dwArgsCount++;
81  bWhiteSpace = FALSE;
82  }
83  }
84 
85  lpChar++;
86  }
87  }
88 
89  hSCManager = OpenSCManagerW(NULL,
90  NULL,
92  if (!hSCManager)
93  {
94  if (lpArgsVector)
95  LocalFree((LPVOID)lpArgsVector);
96  return FALSE;
97  }
98 
99  hService = OpenServiceW(hSCManager,
100  ServiceName,
102  if (!hService)
103  {
104  CloseServiceHandle(hSCManager);
105  if (lpArgsVector)
106  LocalFree((LPVOID)lpArgsVector);
107  return FALSE;
108  }
109 
110  /* Start the service */
111  Result = StartServiceW(hService,
112  dwArgsCount,
113  lpArgsVector);
114  if (!Result && GetLastError() == ERROR_SERVICE_ALREADY_RUNNING)
115  {
116  /* If it's already running, just return TRUE */
117  Result = TRUE;
118  }
119  else if (Result)
120  {
121  if (hProgress)
122  {
123  /* Increment the progress bar */
125  }
126 
127  /* Get the service status to check if it's running */
128  Result = QueryServiceStatusEx(hService,
130  (LPBYTE)&ServiceStatus,
131  sizeof(SERVICE_STATUS_PROCESS),
132  &BytesNeeded);
133  if (Result)
134  {
135  Result = FALSE;
136  MaxWait = MAX_WAIT_TIME;
137  OldCheckPoint = ServiceStatus.dwCheckPoint;
138  StartTickCount = GetTickCount();
139 
140  /* Loop until it's running */
141  while (ServiceStatus.dwCurrentState != SERVICE_RUNNING)
142  {
143  int i;
144  /* Fixup the wait time */
145  WaitTime = ServiceStatus.dwWaitHint / 10;
146 
147  if (WaitTime < 1000) WaitTime = 1000;
148  else if (WaitTime > 10000) WaitTime = 10000;
149 
150  /* We don't wanna wait for up to 10 secs without incrementing */
151  for (i = WaitTime / 1000; i > 0; i--)
152  {
153  Sleep(1000);
154  if (hProgress)
155  {
156  /* Increment the progress bar */
158  }
159  }
160 
161 
162  /* Get the latest status info */
163  if (!QueryServiceStatusEx(hService,
165  (LPBYTE)&ServiceStatus,
166  sizeof(SERVICE_STATUS_PROCESS),
167  &BytesNeeded))
168  {
169  /* Something went wrong... */
170  break;
171  }
172 
173  /* Is the service making progress? */
174  if (ServiceStatus.dwCheckPoint > OldCheckPoint)
175  {
176  /* It is, get the latest tickcount to reset the max wait time */
177  StartTickCount = GetTickCount();
178  OldCheckPoint = ServiceStatus.dwCheckPoint;
179  }
180  else
181  {
182  /* It's not, make sure we haven't exceeded our wait time */
183  if (GetTickCount() >= StartTickCount + MaxWait)
184  {
185  /* We have, give up */
186  break;
187  }
188  }
189  }
190  }
191 
192  if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
193  {
194  Result = TRUE;
195  }
196  }
197 
198  CloseServiceHandle(hService);
199 
200  CloseServiceHandle(hSCManager);
201 
202  if (lpArgsVector)
203  LocalFree((LPVOID)lpArgsVector);
204 
205  return Result;
206 }
#define TRUE
Definition: types.h:120
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:736
#define SC_MANAGER_CONNECT
Definition: winsvc.h:14
DWORD WINAPI GetTickCount(VOID)
Definition: time.c:445
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
BOOL WINAPI QueryServiceStatusEx(SC_HANDLE hService, SC_STATUS_TYPE InfoLevel, LPBYTE lpBuffer, DWORD cbBufSize, LPDWORD pcbBytesNeeded)
Definition: scm.c:2829
GLenum GLclampf GLint i
Definition: glfuncs.h:14
unsigned char * LPBYTE
Definition: typedefs.h:52
#define SERVICE_RUNNING
Definition: winsvc.h:24
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
_At_(*)(_In_ PWSK_CLIENT Client, _In_opt_ PUNICODE_STRING NodeName, _In_opt_ PUNICODE_STRING ServiceName, _In_opt_ ULONG NameSpace, _In_opt_ GUID *Provider, _In_opt_ PADDRINFOEXW Hints, _Outptr_ PADDRINFOEXW *Result, _In_opt_ PEPROCESS OwningProcess, _In_opt_ PETHREAD OwningThread, _Inout_ PIRP Irp Result)(Mem)) NTSTATUS(WSKAPI *PFN_WSK_GET_ADDRESS_INFO
Definition: wsk.h:426
#define SERVICE_QUERY_STATUS
Definition: winsvc.h:55
#define LMEM_FIXED
Definition: winbase.h:349
BOOL WINAPI StartServiceW(SC_HANDLE hService, DWORD dwNumServiceArgs, LPCWSTR *lpServiceArgVectors)
Definition: scm.c:2922
unsigned int BOOL
Definition: ntddk_ex.h:94
LPTSTR ServiceName
Definition: ServiceMain.c:15
unsigned long DWORD
Definition: ntddk_ex.h:95
#define MAX_WAIT_TIME
Definition: start.c:12
#define iswspace(_c)
Definition: ctype.h:669
SC_HANDLE WINAPI OpenSCManagerW(LPCWSTR lpMachineName, LPCWSTR lpDatabaseName, DWORD dwDesiredAccess)
Definition: scm.c:2010
SC_HANDLE hSCManager
Definition: sc.c:12
#define SERVICE_START
Definition: winsvc.h:57
VOID IncrementProgressBar(HANDLE hProgress, UINT NewPos)
Definition: progress.c:288
#define DEFAULT_STEP
Definition: precomp.h:108
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
SERVICE_STATUS ServiceStatus
Definition: dhcpcsvc.c:18
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373
WCHAR * LPWSTR
Definition: xmlstorage.h:184
#define ERROR_SERVICE_ALREADY_RUNNING
Definition: winerror.h:607