hidclass.sys: Use IoRegisterDeviceInterface.
[wine.git] / programs / net / net.c
blob8a4c3f1132e5cf5a09c7549193055aafc918e7b4
1 /*
2 * Copyright 2007 Tim Schwartz
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <windows.h>
20 #include <lm.h>
21 #include <wine/unicode.h>
22 #include <wine/debug.h>
24 #include "resources.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(net);
28 #define NET_START 0001
29 #define NET_STOP 0002
31 static int output_write(const WCHAR* str, int len)
33 DWORD ret, count;
34 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL);
35 if (!ret)
37 DWORD lenA;
38 char* strA;
40 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
41 * back to WriteFile(), assuming the console encoding is still the right
42 * one in that case.
44 lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len,
45 NULL, 0, NULL, NULL);
46 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
47 if (!strA)
48 return 0;
50 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA,
51 NULL, NULL);
52 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
53 HeapFree(GetProcessHeap(), 0, strA);
55 return count;
58 static int output_vprintf(const WCHAR* fmt, __ms_va_list va_args)
60 WCHAR str[8192];
61 int len;
63 SetLastError(NO_ERROR);
64 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt, 0, 0, str, ARRAY_SIZE(str), &va_args);
65 if (len == 0 && GetLastError() != NO_ERROR)
66 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
67 else
68 output_write(str, len);
69 return 0;
72 static int WINAPIV output_printf(const WCHAR* fmt, ...)
74 __ms_va_list arguments;
76 __ms_va_start(arguments, fmt);
77 output_vprintf(fmt, arguments);
78 __ms_va_end(arguments);
79 return 0;
82 static int WINAPIV output_string(int msg, ...)
84 WCHAR fmt[8192];
85 __ms_va_list arguments;
87 LoadStringW(GetModuleHandleW(NULL), msg, fmt, ARRAY_SIZE(fmt));
88 __ms_va_start(arguments, msg);
89 output_vprintf(fmt, arguments);
90 __ms_va_end(arguments);
91 return 0;
94 static BOOL output_error_string(DWORD error)
96 LPWSTR pBuffer;
97 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
98 FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
99 NULL, error, 0, (LPWSTR)&pBuffer, 0, NULL))
101 output_write(pBuffer, lstrlenW(pBuffer));
102 LocalFree(pBuffer);
103 return TRUE;
105 return FALSE;
108 static BOOL net_use(int argc, const WCHAR* argv[])
110 USE_INFO_2 *buffer, *connection;
111 DWORD read, total, resume_handle, rc, i;
112 WCHAR* status[STRING_RECONN-STRING_OK+1];
113 resume_handle = 0;
114 buffer = NULL;
116 if(argc<3)
118 HMODULE hmod = GetModuleHandleW(NULL);
120 /* Load the status strings */
121 for (i = 0; i < ARRAY_SIZE(status); i++)
123 status[i] = HeapAlloc(GetProcessHeap(), 0, 1024 * sizeof(**status));
124 LoadStringW(hmod, STRING_OK+i, status[i], 1024);
127 do {
128 rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
129 if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
131 break;
134 if(total == 0)
136 output_string(STRING_NO_ENTRIES);
137 break;
140 output_string(STRING_USE_HEADER);
141 for (i = 0, connection = buffer; i < read; ++i, ++connection)
142 output_string(STRING_USE_ENTRY, status[connection->ui2_status], connection->ui2_local,
143 connection->ui2_remote, connection->ui2_refcount);
145 if (buffer != NULL) NetApiBufferFree(buffer);
146 } while (rc == ERROR_MORE_DATA);
148 /* Release the status strings */
149 for (i = 0; i < ARRAY_SIZE(status); i++)
150 HeapFree(GetProcessHeap(), 0, status[i]);
152 return TRUE;
155 return FALSE;
158 static BOOL net_enum_services(void)
160 static const WCHAR runningW[]={' ',' ',' ',' ','%','1','\n',0};
161 SC_HANDLE SCManager;
162 LPENUM_SERVICE_STATUS_PROCESSW services;
163 DWORD size, i, count, resume;
164 BOOL success = FALSE;
166 SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
167 if(!SCManager)
169 output_string(STRING_NO_SCM);
170 return FALSE;
173 EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0, &size, &count, NULL, NULL);
174 if(GetLastError() != ERROR_MORE_DATA)
176 output_error_string(GetLastError());
177 goto end;
179 services = HeapAlloc(GetProcessHeap(), 0, size);
180 resume = 0;
181 if(!EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, (LPBYTE)services, size, &size, &count, &resume, NULL))
183 output_error_string(GetLastError());
184 goto end;
186 output_string(STRING_RUNNING_HEADER);
187 for(i = 0; i < count; i++)
189 output_printf(runningW, services[i].lpDisplayName);
190 WINE_TRACE("service=%s state=%d controls=%x\n",
191 wine_dbgstr_w(services[i].lpServiceName),
192 services[i].ServiceStatusProcess.dwCurrentState,
193 services[i].ServiceStatusProcess.dwControlsAccepted);
195 success = TRUE;
197 end:
198 CloseServiceHandle(SCManager);
199 return success;
202 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
204 LPENUM_SERVICE_STATUSW dependencies = NULL;
205 DWORD buffer_size = 0;
206 DWORD count = 0, counter;
207 BOOL result;
208 SC_HANDLE dependent_serviceHandle;
209 SERVICE_STATUS_PROCESS ssp;
211 result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
213 if(!result && (GetLastError() == ERROR_MORE_DATA))
215 dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
216 if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
218 for(counter = 0; counter < count; counter++)
220 output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
221 dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
222 if(dependent_serviceHandle)
224 result = StopService(SCManager, dependent_serviceHandle);
225 CloseServiceHandle(dependent_serviceHandle);
227 if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
232 if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
233 HeapFree(GetProcessHeap(), 0, dependencies);
234 return result;
237 static BOOL net_service(int operation, const WCHAR* service_name)
239 SC_HANDLE SCManager, serviceHandle;
240 BOOL result = FALSE;
241 WCHAR service_display_name[4096];
242 DWORD buffer_size;
244 SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
245 if(!SCManager)
247 output_string(STRING_NO_SCM);
248 return FALSE;
250 serviceHandle = OpenServiceW(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
251 if(!serviceHandle)
253 output_string(STRING_NO_SVCHANDLE);
254 CloseServiceHandle(SCManager);
255 return FALSE;
258 buffer_size = ARRAY_SIZE(service_display_name);
259 GetServiceDisplayNameW(SCManager, service_name, service_display_name, &buffer_size);
260 if (!service_display_name[0]) lstrcpyW(service_display_name, service_name);
262 switch(operation)
264 case NET_START:
265 output_string(STRING_START_SVC, service_display_name);
266 result = StartServiceW(serviceHandle, 0, NULL);
268 if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
269 else
271 if (!output_error_string(GetLastError()))
272 output_string(STRING_START_SVC_FAIL, service_display_name);
274 break;
275 case NET_STOP:
276 output_string(STRING_STOP_SVC, service_display_name);
277 result = StopService(SCManager, serviceHandle);
279 if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
280 else
282 if (!output_error_string(GetLastError()))
283 output_string(STRING_STOP_SVC_FAIL, service_display_name);
285 break;
288 CloseServiceHandle(serviceHandle);
289 CloseServiceHandle(SCManager);
290 return result;
293 static BOOL arg_is(const WCHAR* str1, const WCHAR* str2)
295 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, str1, -1, str2, -1) == CSTR_EQUAL;
298 int wmain(int argc, const WCHAR* argv[])
300 static const WCHAR helpW[]={'h','e','l','p',0};
301 static const WCHAR shelpW[]={'/','h','e','l','p',0};
302 static const WCHAR startW[]={'s','t','a','r','t',0};
303 static const WCHAR stopW[]={'s','t','o','p',0};
304 static const WCHAR useW[]={'u','s','e',0};
306 if (argc < 2)
308 output_string(STRING_USAGE);
309 return 1;
312 if(arg_is(argv[1], helpW))
314 if(argc > 3)
316 output_string(STRING_USAGE);
317 return 1;
319 if(argc == 2)
320 output_string(STRING_USAGE);
321 else if(arg_is(argv[2], startW))
322 output_string(STRING_START_USAGE);
323 else if(arg_is(argv[2], stopW))
324 output_string(STRING_STOP_USAGE);
325 else
326 output_string(STRING_USAGE);
328 else if(arg_is(argv[1], startW))
330 if(argc > 3)
332 output_string(STRING_START_USAGE);
333 return 1;
335 if (argc == 2)
337 if (!net_enum_services())
338 return 1;
340 else if(arg_is(argv[2], shelpW))
341 output_string(STRING_START_USAGE);
342 else if(!net_service(NET_START, argv[2]))
343 return 1;
345 else if(arg_is(argv[1], stopW))
347 if(argc != 3)
349 output_string(STRING_STOP_USAGE);
350 return 1;
352 if(arg_is(argv[2], shelpW))
353 output_string(STRING_STOP_USAGE);
354 else if(!net_service(NET_STOP, argv[2]))
355 return 1;
357 else if(arg_is(argv[1], useW))
359 if(!net_use(argc, argv)) return 1;
361 else
362 output_string(STRING_USAGE);
364 return 0;