[NET] CORE-6413
[reactos.git] / reactos / base / applications / network / net / cmdstart.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE:
5 * PURPOSE:
6 *
7 * PROGRAMMERS: Magnus Olsen (greatlord@reactos.org)
8 */
9
10 #include "net.h"
11
12 /* Enumerate all running services */
13 static
14 INT
15 EnumerateRunningServices(VOID)
16 {
17 SC_HANDLE hManager = NULL;
18 SC_HANDLE hService = NULL;
19 DWORD dwBufferSize = 0;
20 DWORD dwServiceCount;
21 DWORD dwResumeHandle = 0;
22 LPENUM_SERVICE_STATUS lpServiceBuffer = NULL;
23 INT i;
24 INT nError = 0;
25 DWORD dwError = ERROR_SUCCESS;
26
27 hManager = OpenSCManagerW(NULL,
28 SERVICES_ACTIVE_DATABASE,
29 SC_MANAGER_ENUMERATE_SERVICE);
30 if (hManager == NULL)
31 {
32 dwError = GetLastError();
33 nError = 1;
34 goto done;
35 }
36
37 EnumServicesStatusW(hManager,
38 SERVICE_WIN32,
39 SERVICE_ACTIVE,
40 NULL,
41 0,
42 &dwBufferSize,
43 &dwServiceCount,
44 &dwResumeHandle);
45
46 if (dwBufferSize != 0)
47 {
48 lpServiceBuffer = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
49 if (lpServiceBuffer != NULL)
50 {
51 if (EnumServicesStatusW(hManager,
52 SERVICE_WIN32,
53 SERVICE_ACTIVE,
54 lpServiceBuffer,
55 dwBufferSize,
56 &dwBufferSize,
57 &dwServiceCount,
58 &dwResumeHandle))
59 {
60 printf("The following services hav been started:\n\n");
61
62 for (i = 0; i < dwServiceCount; i++)
63 {
64 printf(" %S\n", lpServiceBuffer[i].lpDisplayName);
65 }
66 }
67
68 HeapFree(GetProcessHeap(), 0, lpServiceBuffer);
69 }
70 }
71
72 done:
73 if (hService != NULL)
74 CloseServiceHandle(hService);
75
76 if (hManager != NULL)
77 CloseServiceHandle(hManager);
78
79 if (dwError != ERROR_SUCCESS)
80 {
81 /* FIXME: Print proper error message */
82 printf("Error: %lu\n", dwError);
83 }
84
85 return nError;
86 }
87
88
89 /* Start the service argv[2] */
90 static
91 INT
92 StartOneService(INT argc, WCHAR **argv)
93 {
94 SC_HANDLE hManager = NULL;
95 SC_HANDLE hService = NULL;
96 LPCWSTR *lpArgVectors = NULL;
97 DWORD dwError = ERROR_SUCCESS;
98 INT nError = 0;
99 INT i;
100
101 hManager = OpenSCManagerW(NULL,
102 SERVICES_ACTIVE_DATABASE,
103 SC_MANAGER_ENUMERATE_SERVICE);
104 if (hManager == NULL)
105 {
106 dwError = GetLastError();
107 nError = 1;
108 goto done;
109 }
110
111 hService = OpenServiceW(hManager,
112 argv[2],
113 SERVICE_START);
114 if (hService == NULL)
115 {
116 dwError = GetLastError();
117 nError = 1;
118 goto done;
119 }
120
121 lpArgVectors = HeapAlloc(GetProcessHeap(),
122 0,
123 (argc - 2) * sizeof(LPCWSTR));
124 if (lpArgVectors == NULL)
125 {
126 dwError = GetLastError();
127 nError = 1;
128 goto done;
129 }
130
131 for (i = 2; i < argc; i++)
132 {
133 lpArgVectors[i] = argv[i + 2];
134 }
135
136 if (!StartServiceW(hService,
137 (DWORD)argc - 2,
138 lpArgVectors))
139 {
140 dwError = GetLastError();
141 nError = 1;
142 }
143
144 done:
145 if (lpArgVectors != NULL)
146 HeapFree(GetProcessHeap(), 0, lpArgVectors);
147
148 if (hService != NULL)
149 CloseServiceHandle(hService);
150
151 if (hManager != NULL)
152 CloseServiceHandle(hManager);
153
154 if (dwError != ERROR_SUCCESS)
155 {
156 /* FIXME: Print proper error message */
157 printf("Error: %lu\n", dwError);
158 }
159
160 return nError;
161 }
162
163
164 INT
165 cmdStart(INT argc, WCHAR **argv)
166 {
167 INT nError = 0;
168
169 if (argc == 2)
170 {
171 nError = EnumerateRunningServices();
172 }
173 else
174 {
175 nError = StartOneService(argc, argv);
176 }
177
178 return nError;
179 }