d3d10/effect: Implement GetString().
[wine.git] / programs / net / net.c
blobdbc4bf2bfc6b53d4e75f0bec587e8d100891a8e2
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/debug.h>
23 #include "resources.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(net);
27 #define NET_START 0001
28 #define NET_STOP 0002
30 static int output_write(const WCHAR* str, int len)
32 DWORD ret, count;
33 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), str, len, &count, NULL);
34 if (!ret)
36 DWORD lenA;
37 char* strA;
39 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
40 * back to WriteFile(), assuming the console encoding is still the right
41 * one in that case.
43 lenA = WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len,
44 NULL, 0, NULL, NULL);
45 strA = HeapAlloc(GetProcessHeap(), 0, lenA);
46 if (!strA)
47 return 0;
49 WideCharToMultiByte(GetConsoleOutputCP(), 0, str, len, strA, lenA,
50 NULL, NULL);
51 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), strA, lenA, &count, FALSE);
52 HeapFree(GetProcessHeap(), 0, strA);
54 return count;
57 static int output_vprintf(const WCHAR* fmt, __ms_va_list va_args)
59 WCHAR str[8192];
60 int len;
62 len = FormatMessageW(FORMAT_MESSAGE_FROM_STRING, fmt, 0, 0, str, ARRAY_SIZE(str), &va_args);
63 if (len == 0 && GetLastError() != ERROR_NO_WORK_DONE)
64 WINE_FIXME("Could not format string: le=%u, fmt=%s\n", GetLastError(), wine_dbgstr_w(fmt));
65 else
66 output_write(str, len);
67 return 0;
70 static int WINAPIV output_printf(const WCHAR* fmt, ...)
72 __ms_va_list arguments;
74 __ms_va_start(arguments, fmt);
75 output_vprintf(fmt, arguments);
76 __ms_va_end(arguments);
77 return 0;
80 static int WINAPIV output_string(int msg, ...)
82 WCHAR fmt[8192];
83 __ms_va_list arguments;
85 LoadStringW(GetModuleHandleW(NULL), msg, fmt, ARRAY_SIZE(fmt));
86 __ms_va_start(arguments, msg);
87 output_vprintf(fmt, arguments);
88 __ms_va_end(arguments);
89 return 0;
92 static BOOL output_error_string(DWORD error)
94 LPWSTR pBuffer;
95 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
96 FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
97 NULL, error, 0, (LPWSTR)&pBuffer, 0, NULL))
99 output_write(pBuffer, lstrlenW(pBuffer));
100 LocalFree(pBuffer);
101 return TRUE;
103 return FALSE;
106 static BOOL net_use(int argc, const WCHAR* argv[])
108 USE_INFO_2 *buffer, *connection;
109 DWORD read, total, resume_handle, rc, i;
110 WCHAR* status[STRING_RECONN-STRING_OK+1];
111 resume_handle = 0;
112 buffer = NULL;
114 if(argc<3)
116 HMODULE hmod = GetModuleHandleW(NULL);
118 /* Load the status strings */
119 for (i = 0; i < ARRAY_SIZE(status); i++)
121 status[i] = HeapAlloc(GetProcessHeap(), 0, 1024 * sizeof(**status));
122 LoadStringW(hmod, STRING_OK+i, status[i], 1024);
125 do {
126 rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
127 if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
129 break;
132 if(total == 0)
134 output_string(STRING_NO_ENTRIES);
135 break;
138 output_string(STRING_USE_HEADER);
139 for (i = 0, connection = buffer; i < read; ++i, ++connection)
140 output_string(STRING_USE_ENTRY, status[connection->ui2_status], connection->ui2_local,
141 connection->ui2_remote, connection->ui2_refcount);
143 if (buffer != NULL) NetApiBufferFree(buffer);
144 } while (rc == ERROR_MORE_DATA);
146 /* Release the status strings */
147 for (i = 0; i < ARRAY_SIZE(status); i++)
148 HeapFree(GetProcessHeap(), 0, status[i]);
150 return TRUE;
153 return FALSE;
156 static BOOL net_enum_services(void)
158 SC_HANDLE SCManager;
159 LPENUM_SERVICE_STATUS_PROCESSW services;
160 DWORD size, i, count, resume;
161 BOOL success = FALSE;
163 SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
164 if(!SCManager)
166 output_string(STRING_NO_SCM);
167 return FALSE;
170 EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, NULL, 0, &size, &count, NULL, NULL);
171 if(GetLastError() != ERROR_MORE_DATA)
173 output_error_string(GetLastError());
174 goto end;
176 services = HeapAlloc(GetProcessHeap(), 0, size);
177 resume = 0;
178 if(!EnumServicesStatusExW(SCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_ACTIVE, (LPBYTE)services, size, &size, &count, &resume, NULL))
180 output_error_string(GetLastError());
181 goto end;
183 output_string(STRING_RUNNING_HEADER);
184 for(i = 0; i < count; i++)
186 output_printf(L" %1\n", services[i].lpDisplayName);
187 WINE_TRACE("service=%s state=%d controls=%x\n",
188 wine_dbgstr_w(services[i].lpServiceName),
189 services[i].ServiceStatusProcess.dwCurrentState,
190 services[i].ServiceStatusProcess.dwControlsAccepted);
192 success = TRUE;
194 end:
195 CloseServiceHandle(SCManager);
196 return success;
199 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
201 LPENUM_SERVICE_STATUSW dependencies = NULL;
202 DWORD buffer_size = 0;
203 DWORD count = 0, counter;
204 BOOL result;
205 SC_HANDLE dependent_serviceHandle;
206 SERVICE_STATUS_PROCESS ssp;
208 result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
210 if(!result && (GetLastError() == ERROR_MORE_DATA))
212 dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
213 if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
215 for(counter = 0; counter < count; counter++)
217 output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
218 dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
219 if(dependent_serviceHandle)
221 result = StopService(SCManager, dependent_serviceHandle);
222 CloseServiceHandle(dependent_serviceHandle);
224 if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
229 if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
230 HeapFree(GetProcessHeap(), 0, dependencies);
231 return result;
234 static BOOL net_service(int operation, const WCHAR* service_name)
236 SC_HANDLE SCManager, serviceHandle;
237 BOOL result = FALSE;
238 WCHAR service_display_name[4096];
239 DWORD buffer_size;
241 SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
242 if(!SCManager)
244 output_string(STRING_NO_SCM);
245 return FALSE;
247 serviceHandle = OpenServiceW(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
248 if(!serviceHandle)
250 output_string(STRING_NO_SVCHANDLE);
251 CloseServiceHandle(SCManager);
252 return FALSE;
255 buffer_size = ARRAY_SIZE(service_display_name);
256 GetServiceDisplayNameW(SCManager, service_name, service_display_name, &buffer_size);
257 if (!service_display_name[0]) lstrcpyW(service_display_name, service_name);
259 switch(operation)
261 case NET_START:
262 output_string(STRING_START_SVC, service_display_name);
263 result = StartServiceW(serviceHandle, 0, NULL);
265 if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
266 else
268 if (!output_error_string(GetLastError()))
269 output_string(STRING_START_SVC_FAIL, service_display_name);
271 break;
272 case NET_STOP:
273 output_string(STRING_STOP_SVC, service_display_name);
274 result = StopService(SCManager, serviceHandle);
276 if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
277 else
279 if (!output_error_string(GetLastError()))
280 output_string(STRING_STOP_SVC_FAIL, service_display_name);
282 break;
285 CloseServiceHandle(serviceHandle);
286 CloseServiceHandle(SCManager);
287 return result;
290 static BOOL arg_is(const WCHAR* str1, const WCHAR* str2)
292 return CompareStringW(LOCALE_USER_DEFAULT, NORM_IGNORECASE, str1, -1, str2, -1) == CSTR_EQUAL;
295 int __cdecl wmain(int argc, const WCHAR* argv[])
297 if (argc < 2)
299 output_string(STRING_USAGE);
300 return 1;
303 if(arg_is(argv[1], L"help"))
305 if(argc > 3)
307 output_string(STRING_USAGE);
308 return 1;
310 if(argc == 2)
311 output_string(STRING_USAGE);
312 else if(arg_is(argv[2], L"start"))
313 output_string(STRING_START_USAGE);
314 else if(arg_is(argv[2], L"stop"))
315 output_string(STRING_STOP_USAGE);
316 else
317 output_string(STRING_USAGE);
319 else if(arg_is(argv[1], L"start"))
321 if(argc > 3)
323 output_string(STRING_START_USAGE);
324 return 1;
326 if (argc == 2)
328 if (!net_enum_services())
329 return 1;
331 else if(arg_is(argv[2], L"/help"))
332 output_string(STRING_START_USAGE);
333 else if(!net_service(NET_START, argv[2]))
334 return 1;
336 else if(arg_is(argv[1], L"stop"))
338 if(argc != 3)
340 output_string(STRING_STOP_USAGE);
341 return 1;
343 if(arg_is(argv[2], L"/help"))
344 output_string(STRING_STOP_USAGE);
345 else if(!net_service(NET_STOP, argv[2]))
346 return 1;
348 else if(arg_is(argv[1], L"use"))
350 if(!net_use(argc, argv)) return 1;
352 else
353 output_string(STRING_USAGE);
355 return 0;