2 * PROJECT: ReactOS Services
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/mscutils/servman/start.c
5 * PURPOSE: Start a service
6 * COPYRIGHT: Copyright 2005-2015 Ged Murphy <gedmurphy@reactos.org>
12 #define MAX_WAIT_TIME 30000
15 DoStartService(LPWSTR ServiceName
,
21 SERVICE_STATUS_PROCESS ServiceStatus
;
22 DWORD BytesNeeded
= 0;
29 BOOL bWhiteSpace
= TRUE
;
31 DWORD dwArgsCount
= 0;
32 LPCWSTR
*lpArgsVector
= NULL
;
34 if (lpStartParams
!= NULL
)
36 /* Count the number of arguments */
37 lpChar
= lpStartParams
;
40 if (iswspace(*lpChar
))
46 if (bWhiteSpace
== TRUE
)
56 /* Allocate the arguments vector and add one for the service name */
57 lpArgsVector
= LocalAlloc(LMEM_FIXED
, (dwArgsCount
+ 1) * sizeof(LPCWSTR
));
61 /* Make the service name the first argument */
62 lpArgsVector
[0] = ServiceName
;
64 /* Fill the arguments vector */
67 lpChar
= lpStartParams
;
70 if (iswspace(*lpChar
))
77 if (bWhiteSpace
== TRUE
)
79 lpArgsVector
[dwArgsCount
] = lpChar
;
89 hSCManager
= OpenSCManagerW(NULL
,
95 LocalFree((LPVOID
)lpArgsVector
);
99 hService
= OpenServiceW(hSCManager
,
101 SERVICE_START
| SERVICE_QUERY_STATUS
);
104 CloseServiceHandle(hSCManager
);
106 LocalFree((LPVOID
)lpArgsVector
);
110 /* Start the service */
111 Result
= StartServiceW(hService
,
114 if (!Result
&& GetLastError() == ERROR_SERVICE_ALREADY_RUNNING
)
116 /* If it's already running, just return TRUE */
123 /* Increment the progress bar */
124 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
127 /* Get the service status to check if it's running */
128 Result
= QueryServiceStatusEx(hService
,
129 SC_STATUS_PROCESS_INFO
,
130 (LPBYTE
)&ServiceStatus
,
131 sizeof(SERVICE_STATUS_PROCESS
),
136 MaxWait
= MAX_WAIT_TIME
;
137 OldCheckPoint
= ServiceStatus
.dwCheckPoint
;
138 StartTickCount
= GetTickCount();
140 /* Loop until it's running */
141 while (ServiceStatus
.dwCurrentState
!= SERVICE_RUNNING
)
144 /* Fixup the wait time */
145 WaitTime
= ServiceStatus
.dwWaitHint
/ 10;
147 if (WaitTime
< 1000) WaitTime
= 1000;
148 else if (WaitTime
> 10000) WaitTime
= 10000;
150 /* We don't wanna wait for up to 10 secs without incrementing */
151 for (i
= WaitTime
/ 1000; i
> 0; i
--)
156 /* Increment the progress bar */
157 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
162 /* Get the latest status info */
163 if (!QueryServiceStatusEx(hService
,
164 SC_STATUS_PROCESS_INFO
,
165 (LPBYTE
)&ServiceStatus
,
166 sizeof(SERVICE_STATUS_PROCESS
),
169 /* Something went wrong... */
173 /* Is the service making progress? */
174 if (ServiceStatus
.dwCheckPoint
> OldCheckPoint
)
176 /* It is, get the latest tickcount to reset the max wait time */
177 StartTickCount
= GetTickCount();
178 OldCheckPoint
= ServiceStatus
.dwCheckPoint
;
182 /* It's not, make sure we haven't exceeded our wait time */
183 if (GetTickCount() >= StartTickCount
+ MaxWait
)
185 /* We have, give up */
192 if (ServiceStatus
.dwCurrentState
== SERVICE_RUNNING
)
198 CloseServiceHandle(hService
);
200 CloseServiceHandle(hSCManager
);
203 LocalFree((LPVOID
)lpArgsVector
);