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
,
18 SERVICE_STATUS_PROCESS ServiceStatus
;
19 DWORD BytesNeeded
= 0;
20 DWORD dwStartTickCount
;
21 DWORD dwOldCheckPoint
;
26 hSCManager
= OpenSCManager(NULL
,
31 hService
= OpenService(hSCManager
,
32 Info
->pCurrentService
->lpServiceName
,
33 SERVICE_START
| SERVICE_QUERY_STATUS
);
38 /* Increment the progress bar */
39 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
42 /* Start the service */
43 bRet
= StartService(hService
,
46 if (!bRet
&& GetLastError() == ERROR_SERVICE_ALREADY_RUNNING
)
48 /* If it's already running, just return TRUE */
55 /* Get the service status to check if it's running */
56 if (QueryServiceStatusEx(hService
,
57 SC_STATUS_PROCESS_INFO
,
58 (LPBYTE
)&ServiceStatus
,
59 sizeof(SERVICE_STATUS_PROCESS
),
62 /* We don't want to wait for more than 30 seconds */
64 dwStartTickCount
= GetTickCount();
66 /* Loop until it's running */
67 while (ServiceStatus
.dwCurrentState
!= SERVICE_RUNNING
)
69 dwOldCheckPoint
= ServiceStatus
.dwCheckPoint
;
70 dwWaitTime
= ServiceStatus
.dwWaitHint
/ 10;
72 /* Get the latest status info */
73 if (!QueryServiceStatusEx(hService
,
74 SC_STATUS_PROCESS_INFO
,
75 (LPBYTE
)&ServiceStatus
,
76 sizeof(SERVICE_STATUS_PROCESS
),
79 /* Something went wrong... */
83 /* Is the service making progress? */
84 if (ServiceStatus
.dwCheckPoint
> dwOldCheckPoint
)
86 /* It is, get the latest tickcount to reset the max wait time */
87 dwStartTickCount
= GetTickCount();
88 dwOldCheckPoint
= ServiceStatus
.dwCheckPoint
;
89 IncrementProgressBar(hProgress
, DEFAULT_STEP
);
93 /* It's not, make sure we haven't exceeded our wait time */
94 if (GetTickCount() >= dwStartTickCount
+ dwMaxWait
)
96 /* We have, give up */
101 /* Adjust the wait hint times */
102 if (dwWaitTime
< 200)
104 else if (dwWaitTime
> 10000)
107 /* Wait before trying again */
112 if (ServiceStatus
.dwCurrentState
== SERVICE_RUNNING
)
118 CloseServiceHandle(hService
);
121 CloseServiceHandle(hSCManager
);
128 DoStart(PMAIN_WND_INFO Info
)
133 /* Create a progress window to track the progress of the stopping service */
134 hProgress
= CreateProgressDialog(Info
->hMainWnd
,
135 IDS_PROGRESS_INFO_START
);
138 /* Set the service name and reset the progress bag */
139 InitializeProgressDialog(hProgress
, Info
->pCurrentService
->lpServiceName
);
141 /* Start the requested service */
142 bRet
= DoStartService(Info
, hProgress
);
144 /* Complete and destroy the progress bar */
145 DestroyProgressDialog(hProgress
, bRet
);