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
21 #include <wine/debug.h>
23 #include "resources.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(net
);
27 #define NET_START 0001
30 static int output_write(const WCHAR
* str
, int len
)
33 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, len
, &count
, NULL
);
39 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
40 * back to WriteFile(), assuming the console encoding is still the right
43 lenA
= WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, len
,
45 strA
= HeapAlloc(GetProcessHeap(), 0, lenA
);
49 WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, len
, strA
, lenA
,
51 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), strA
, lenA
, &count
, FALSE
);
52 HeapFree(GetProcessHeap(), 0, strA
);
57 static int output_vprintf(const WCHAR
* fmt
, __ms_va_list va_args
)
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
));
66 output_write(str
, len
);
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
);
80 static int WINAPIV
output_string(int msg
, ...)
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
);
92 static BOOL
output_error_string(DWORD error
)
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
));
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];
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);
126 rc
= NetUseEnum(NULL
, 2, (BYTE
**) &buffer
, 2048, &read
, &total
, &resume_handle
);
127 if (rc
!= ERROR_MORE_DATA
&& rc
!= ERROR_SUCCESS
)
134 output_string(STRING_NO_ENTRIES
);
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
]);
156 static BOOL
net_enum_services(void)
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
);
166 output_string(STRING_NO_SCM
);
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());
176 services
= HeapAlloc(GetProcessHeap(), 0, size
);
178 if(!EnumServicesStatusExW(SCManager
, SC_ENUM_PROCESS_INFO
, SERVICE_WIN32
, SERVICE_ACTIVE
, (LPBYTE
)services
, size
, &size
, &count
, &resume
, NULL
))
180 output_error_string(GetLastError());
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
);
195 CloseServiceHandle(SCManager
);
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
;
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
);
234 static BOOL
net_service(int operation
, const WCHAR
* service_name
)
236 SC_HANDLE SCManager
, serviceHandle
;
238 WCHAR service_display_name
[4096];
241 SCManager
= OpenSCManagerW(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
244 output_string(STRING_NO_SCM
);
247 serviceHandle
= OpenServiceW(SCManager
, service_name
, SC_MANAGER_ALL_ACCESS
);
250 output_string(STRING_NO_SVCHANDLE
);
251 CloseServiceHandle(SCManager
);
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
);
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
);
268 if (!output_error_string(GetLastError()))
269 output_string(STRING_START_SVC_FAIL
, service_display_name
);
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
);
279 if (!output_error_string(GetLastError()))
280 output_string(STRING_STOP_SVC_FAIL
, service_display_name
);
285 CloseServiceHandle(serviceHandle
);
286 CloseServiceHandle(SCManager
);
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
[])
299 output_string(STRING_USAGE
);
303 if(arg_is(argv
[1], L
"help"))
307 output_string(STRING_USAGE
);
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
);
317 output_string(STRING_USAGE
);
319 else if(arg_is(argv
[1], L
"start"))
323 output_string(STRING_START_USAGE
);
328 if (!net_enum_services())
331 else if(arg_is(argv
[2], L
"/help"))
332 output_string(STRING_START_USAGE
);
333 else if(!net_service(NET_START
, argv
[2]))
336 else if(arg_is(argv
[1], L
"stop"))
340 output_string(STRING_STOP_USAGE
);
343 if(arg_is(argv
[2], L
"/help"))
344 output_string(STRING_STOP_USAGE
);
345 else if(!net_service(NET_STOP
, argv
[2]))
348 else if(arg_is(argv
[1], L
"use"))
350 if(!net_use(argc
, argv
)) return 1;
353 output_string(STRING_USAGE
);