6e00c130114c967037a94ec38b41186658a1111d
[reactos.git] / reactos / subsys / system / sc / start.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS SC utility
4 * FILE: subsys/system/sc/start.c
5 * PURPOSE: control ReactOS services
6 * PROGRAMMERS: Ged Murphy (gedmurphy@gmail.com)
7 * REVISIONS:
8 * Ged Murphy 20/10/05 Created
9 *
10 */
11
12 #include "sc.h"
13
14 BOOL Start(INT ArgCount, TCHAR **Args)
15 {
16 SC_HANDLE hSc;
17 SERVICE_STATUS_PROCESS ServiceStatus;
18 LPCTSTR ServiceName = *Args++;
19 LPCTSTR *ServiceArgs = (LPCTSTR *)Args;
20 DWORD BytesNeeded;
21
22
23 /* testing */
24 printf("service to start - %s\n\n", ServiceName);
25 printf("Arguments :\n");
26 while (*ServiceArgs)
27 {
28 printf("%s\n", *ServiceArgs);
29 ServiceArgs++;
30 }
31
32 /* get a handle to the service requested for starting */
33 hSc = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS);
34
35 if (hSc == NULL)
36 {
37 _tprintf(_T("openService failed\n"));
38 ReportLastError();
39 return FALSE;
40 }
41
42 /* start the service opened */
43 if (! StartService(hSc, ArgCount, ServiceArgs))
44 {
45 _tprintf(_T("StartService failed\n"));
46 ReportLastError();
47 return FALSE;
48 }
49
50 if (! QueryServiceStatusEx(
51 hSc,
52 SC_STATUS_PROCESS_INFO,
53 (LPBYTE)&ServiceStatus,
54 sizeof(SERVICE_STATUS_PROCESS),
55 &BytesNeeded))
56 {
57 _tprintf(_T("QueryServiceStatusEx 1 failed\n"));
58 ReportLastError();
59 return FALSE;
60 }
61
62
63 while (ServiceStatus.dwCurrentState == SERVICE_START_PENDING)
64 {
65 /* wait before checking status */
66 Sleep(ServiceStatus.dwWaitHint);
67
68 /* check status again */
69 if (! QueryServiceStatusEx(
70 hSc,
71 SC_STATUS_PROCESS_INFO,
72 (LPBYTE)&ServiceStatus,
73 sizeof(SERVICE_STATUS_PROCESS),
74 &BytesNeeded))
75 {
76 _tprintf(_T("QueryServiceStatusEx 2 failed\n"));
77 ReportLastError();
78 return FALSE;
79 }
80 }
81
82 CloseServiceHandle(hSc);
83
84 if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
85 {
86 _tprintf(_T("%s is running\n"), ServiceName);
87 return TRUE;
88 }
89 else
90 {
91 _tprintf(_T("Failed to start %s\n"), ServiceName);
92 _tprintf(_T("Curent state: %lu\n"), ServiceStatus.dwCurrentState);
93 _tprintf(_T("Exit code: %lu\n"), ServiceStatus.dwWin32ExitCode);
94 _tprintf(_T("Service Specific exit code: %lu\n"),
95 ServiceStatus.dwServiceSpecificExitCode);
96 _tprintf(_T("Check point: %lu\n"), ServiceStatus.dwCheckPoint);
97 _tprintf(_T("Wait hint: %lu\n"), ServiceStatus.dwWaitHint);
98
99 return FALSE;
100 }
101 }