sc.exe now supports basic starting, stopping, creation and deletion of services.
[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(LPCTSTR ServiceName, LPCTSTR *ServiceArgs, INT ArgCount)
15 {
16 SC_HANDLE hSc;
17 SERVICE_STATUS_PROCESS ServiceStatus;
18 DWORD BytesNeeded;
19
20 /* testing */
21 _tprintf(_T("service to start - %s\n\n"), ServiceName);
22 _tprintf(_T("Arguments :\n"));
23 while (*ServiceArgs)
24 {
25 printf("%s\n", *ServiceArgs);
26 ServiceArgs++;
27 }
28
29
30 /* get a handle to the service requested for starting */
31 hSc = OpenService(hSCManager, ServiceName, SERVICE_ALL_ACCESS);
32
33 if (hSc == NULL)
34 {
35 _tprintf(_T("openService failed\n"));
36 ReportLastError();
37 return FALSE;
38 }
39
40 /* start the service opened */
41 if (! StartService(hSc, ArgCount, ServiceArgs))
42 {
43 _tprintf(_T("StartService failed\n"));
44 ReportLastError();
45 return FALSE;
46 }
47
48 if (! QueryServiceStatusEx(
49 hSc,
50 SC_STATUS_PROCESS_INFO,
51 (LPBYTE)&ServiceStatus,
52 sizeof(SERVICE_STATUS_PROCESS),
53 &BytesNeeded))
54 {
55 _tprintf(_T("QueryServiceStatusEx 1 failed\n"));
56 ReportLastError();
57 return FALSE;
58 }
59
60
61 while (ServiceStatus.dwCurrentState == SERVICE_START_PENDING)
62 {
63 /* wait before checking status */
64 Sleep(ServiceStatus.dwWaitHint);
65
66 /* check status again */
67 if (! QueryServiceStatusEx(
68 hSc,
69 SC_STATUS_PROCESS_INFO,
70 (LPBYTE)&ServiceStatus,
71 sizeof(SERVICE_STATUS_PROCESS),
72 &BytesNeeded))
73 {
74 _tprintf(_T("QueryServiceStatusEx 2 failed\n"));
75 ReportLastError();
76 return FALSE;
77 }
78 }
79
80 CloseServiceHandle(hSc);
81
82 if (ServiceStatus.dwCurrentState == SERVICE_RUNNING)
83 {
84 _tprintf(_T("%s is running\n"), ServiceName);
85 return TRUE;
86 }
87 else
88 {
89 _tprintf(_T("Failed to start %s\n"), ServiceName);
90 _tprintf(_T("Curent state: %lu\n"), ServiceStatus.dwCurrentState);
91 _tprintf(_T("Exit code: %lu\n"), ServiceStatus.dwWin32ExitCode);
92 _tprintf(_T("Service Specific exit code: %lu\n"),
93 ServiceStatus.dwServiceSpecificExitCode);
94 _tprintf(_T("Check point: %lu\n"), ServiceStatus.dwCheckPoint);
95 _tprintf(_T("Wait hint: %lu\n"), ServiceStatus.dwWaitHint);
96
97 return FALSE;
98 }
99 }