Add information letting us know creation deletion of services has worked.
[reactos.git] / reactos / subsys / system / sc / sc.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS SC utility
4 * FILE: subsys/system/sc/sc.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 SC_HANDLE hSCManager;
15
16 DWORD ReportLastError(VOID)
17 {
18 LPVOID lpMsgBuf;
19 DWORD RetVal;
20
21 DWORD ErrorCode = GetLastError();
22 if (ErrorCode != ERROR_SUCCESS)
23 {
24 if ((RetVal = FormatMessage(
25 FORMAT_MESSAGE_ALLOCATE_BUFFER |
26 FORMAT_MESSAGE_FROM_SYSTEM |
27 FORMAT_MESSAGE_IGNORE_INSERTS,
28 NULL,
29 ErrorCode,
30 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */
31 (LPTSTR) &lpMsgBuf,
32 0,
33 NULL )))
34 {
35 _tprintf(_T("%s"), (LPTSTR)lpMsgBuf);
36
37 LocalFree(lpMsgBuf);
38 /* return number of TCHAR's stored in output buffer
39 * excluding '\0' - as FormatMessage does*/
40 return RetVal;
41 }
42 }
43 return 0;
44 }
45
46
47 INT ScControl(LPTSTR MachineName, // remote machine name
48 LPCTSTR Command, // sc command
49 LPCTSTR ServiceName, // name of service
50 LPCTSTR *ServiceArgs, // any options
51 DWORD ArgCount) // argument counter
52 {
53 /* count trailing arguments */
54 ArgCount -= 3;
55
56 if (MachineName)
57 {
58 _tprintf(_T("Remote service control is not yet implemented\n"));
59 return 2;
60 }
61
62 /* if we are emurating the services, we don't need administrator access */
63 if ( (_tcsicmp(Command, _T("query")) == 0) || (_tcsicmp(Command, _T("queryex")) == 0) )
64 hSCManager = OpenSCManager(MachineName, NULL, SC_MANAGER_ENUMERATE_SERVICE);
65 else
66 hSCManager = OpenSCManager(MachineName, NULL, SC_MANAGER_ALL_ACCESS);
67 if (hSCManager == NULL)
68 {
69 _tprintf(_T("[SC] OpenSCManager FAILED %lu:\n\n"), GetLastError());
70 ReportLastError();
71 return -1;
72 }
73
74 /* emurate command */
75 if (_tcsicmp(Command, _T("query")) == 0)
76 Query(ServiceName, ServiceArgs, FALSE);
77
78 else if (_tcsicmp(Command, _T("queryex")) == 0)
79 Query(ServiceName, ServiceArgs, TRUE);
80
81 else if (_tcsicmp(Command, _T("start")) == 0)
82 {
83 if (ServiceName)
84 Start(ServiceName, ServiceArgs, ArgCount);
85 else
86 StartUsage();
87 }
88 else if (_tcsicmp(Command, _T("pause")) == 0)
89 {
90 if (ServiceName)
91 Control(SERVICE_CONTROL_PAUSE, ServiceName, ServiceArgs);
92 else
93 PauseUsage();
94 }
95 else if (_tcsicmp(Command, _T("interrogate")) == 0)
96 {
97 if (ServiceName)
98 Control(SERVICE_CONTROL_INTERROGATE, ServiceName, ServiceArgs);
99 else
100 InterrogateUsage();
101 }
102 else if (_tcsicmp(Command, _T("stop")) == 0)
103 {
104 if (ServiceName)
105 Control(SERVICE_CONTROL_STOP, ServiceName, ServiceArgs);
106 else
107 StopUsage();
108 }
109 else if (_tcsicmp(Command, _T("continue")) == 0)
110 {
111 if (ServiceName)
112 Control(SERVICE_CONTROL_CONTINUE, ServiceName, ServiceArgs);
113 else
114 ContinueUsage();
115 }
116 else if (_tcsicmp(Command, _T("delete")) == 0)
117 {
118 if (ServiceName)
119 Delete(ServiceName);
120 else
121 DeleteUsage();
122 }
123 else if (_tcsicmp(Command, _T("create")) == 0)
124 {
125 if (*ServiceArgs)
126 Create(ServiceName, ServiceArgs);
127 else
128 CreateUsage();
129 }
130 else if (_tcsicmp(Command, _T("control")) == 0)
131 {
132 if (ServiceName)
133 Control((DWORD)NULL, ServiceName, ServiceArgs);
134 else
135 ContinueUsage();
136 }
137 return 0;
138 }
139
140
141 int _tmain(int argc, LPCTSTR argv[])
142 {
143 LPTSTR MachineName = NULL; // remote machine
144 LPCTSTR Command = NULL; // sc command
145 LPCTSTR ServiceName = NULL; // Name of service
146
147 if (argc < 2)
148 return MainUsage();
149
150 /* get server name */
151 if ((argv[1][0] == '\\') && (argv[1][1] == '\\'))
152 {
153 if (argc < 3)
154 return MainUsage();
155
156 _tcscpy(MachineName, argv[1]);
157 Command = argv[2];
158 if (argc > 3)
159 ServiceName = argv[3];
160 return ScControl(MachineName, Command, ServiceName, &argv[4], argc);
161 }
162 else
163 {
164 Command = argv[1];
165 if (argc > 2)
166 ServiceName = argv[2];
167 return ScControl(MachineName, Command, ServiceName, &argv[3], argc);
168 }
169
170 return MainUsage();
171 }