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-2010 Ged Murphy <gedmurphy@reactos.org>
13 DoStartService(PMAIN_WND_INFO Info
,
19 SERVICE_STATUS_PROCESS ServiceStatus
;
20 DWORD BytesNeeded
= 0;
21 DWORD dwStartTickCount
;
22 DWORD dwOldCheckPoint
;
27 BOOL bWhiteSpace
= TRUE
;
29 DWORD dwArgsCount
= 0;
30 LPCWSTR
*lpArgsVector
= NULL
;
32 if (lpStartParams
!= NULL
)
34 /* Count the number of arguments */
35 lpChar
= lpStartParams
;
38 if (iswspace(*lpChar
))
44 if (bWhiteSpace
== TRUE
)
54 /* Allocate the arguments vector and add one for the service name */
55 lpArgsVector
= LocalAlloc(LMEM_FIXED
, (dwArgsCount
+ 1) * sizeof(LPCWSTR
));
59 /* Make the service name the first argument */
60 lpArgsVector
[0] = Info
->pCurrentService
->lpServiceName
;
62 /* Fill the arguments vector */
65 lpChar
= lpStartParams
;
68 if (iswspace(*lpChar
))
75 if (bWhiteSpace
== TRUE
)
77 lpArgsVector
[dwArgsCount
] = lpChar
;
87 hSCManager
= OpenSCManager(NULL
,
92 hService
= OpenService(hSCManager
,
93 Info
->pCurrentService
->lpServiceName
,
94 SERVICE_START
| SERVICE_QUERY_STATUS
);
99 /* Increment the progress bar */
100 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
103 /* Start the service */
104 bRet
= StartService(hService
,
107 if (!bRet
&& GetLastError() == ERROR_SERVICE_ALREADY_RUNNING
)
109 /* If it's already running, just return TRUE */
116 /* Get the service status to check if it's running */
117 if (QueryServiceStatusEx(hService
,
118 SC_STATUS_PROCESS_INFO
,
119 (LPBYTE
)&ServiceStatus
,
120 sizeof(SERVICE_STATUS_PROCESS
),
123 /* We don't want to wait for more than 30 seconds */
125 dwStartTickCount
= GetTickCount();
127 /* Loop until it's running */
128 while (ServiceStatus
.dwCurrentState
!= SERVICE_RUNNING
)
130 dwOldCheckPoint
= ServiceStatus
.dwCheckPoint
;
131 dwWaitTime
= ServiceStatus
.dwWaitHint
/ 10;
133 /* Get the latest status info */
134 if (!QueryServiceStatusEx(hService
,
135 SC_STATUS_PROCESS_INFO
,
136 (LPBYTE
)&ServiceStatus
,
137 sizeof(SERVICE_STATUS_PROCESS
),
140 /* Something went wrong... */
144 /* Is the service making progress? */
145 if (ServiceStatus
.dwCheckPoint
> dwOldCheckPoint
)
147 /* It is, get the latest tickcount to reset the max wait time */
148 dwStartTickCount
= GetTickCount();
149 dwOldCheckPoint
= ServiceStatus
.dwCheckPoint
;
150 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
154 /* It's not, make sure we haven't exceeded our wait time */
155 if (GetTickCount() >= dwStartTickCount
+ dwMaxWait
)
157 /* We have, give up */
162 /* Adjust the wait hint times */
163 if (dwWaitTime
< 200)
165 else if (dwWaitTime
> 10000)
168 /* Wait before trying again */
173 if (ServiceStatus
.dwCurrentState
== SERVICE_RUNNING
)
179 CloseServiceHandle(hService
);
182 CloseServiceHandle(hSCManager
);
186 LocalFree((LPVOID
)lpArgsVector
);
192 DoStart(PMAIN_WND_INFO Info
, LPWSTR lpStartParams
)
197 /* Create a progress window to track the progress of the stopping service */
198 hProgress
= CreateProgressDialog(Info
->hMainWnd
,
199 IDS_PROGRESS_INFO_START
);
202 /* Set the service name and reset the progress bag */
203 InitializeProgressDialog(hProgress
, Info
->pCurrentService
->lpServiceName
);
205 /* Start the requested service */
206 bRet
= DoStartService(Info
, hProgress
, lpStartParams
);
208 /* Complete and destroy the progress bar */
209 DestroyProgressDialog(hProgress
, bRet
);