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/unicode.h>
23 #include "resources.h"
25 #define NET_START 0001
28 static int output_write(WCHAR
* str
, int len
)
31 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), str
, len
, &count
, NULL
);
37 /* On Windows WriteConsoleW() fails if the output is redirected. So fall
38 * back to WriteFile(), assuming the console encoding is still the right
41 lenA
= WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, len
,
43 strA
= HeapAlloc(GetProcessHeap(), 0, lenA
);
47 WideCharToMultiByte(GetConsoleOutputCP(), 0, str
, len
, strA
, lenA
,
49 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), strA
, len
, &count
, FALSE
);
50 HeapFree(GetProcessHeap(), 0, strA
);
55 static int output_string(int msg
, ...)
62 LoadStringW(GetModuleHandleW(NULL
), msg
, fmt
, sizeof(fmt
)/sizeof(fmt
[0]));
63 va_start(arguments
, msg
);
64 len
= vsprintfW(str
, fmt
, arguments
);
65 output_write(str
, len
);
70 static BOOL
output_error_string(DWORD error
)
73 if (FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
|
74 FORMAT_MESSAGE_IGNORE_INSERTS
| FORMAT_MESSAGE_ALLOCATE_BUFFER
,
75 NULL
, error
, 0, (LPWSTR
)&pBuffer
, 0, NULL
))
77 output_write(pBuffer
, lstrlenW(pBuffer
));
84 static BOOL
net_use(int argc
, const WCHAR
* argv
[])
86 USE_INFO_2
*buffer
, *connection
;
87 DWORD read
, total
, resume_handle
, rc
, i
;
88 WCHAR
* status
[STRING_RECONN
-STRING_OK
+1];
94 HMODULE hmod
= GetModuleHandleW(NULL
);
96 /* Load the status strings */
97 for (i
= 0; i
< sizeof(status
)/sizeof(*status
); i
++)
99 status
[i
] = HeapAlloc(GetProcessHeap(), 0, 1024 * sizeof(**status
));
100 LoadStringW(hmod
, STRING_OK
+i
, status
[i
], 1024);
104 rc
= NetUseEnum(NULL
, 2, (BYTE
**) &buffer
, 2048, &read
, &total
, &resume_handle
);
105 if (rc
!= ERROR_MORE_DATA
&& rc
!= ERROR_SUCCESS
)
112 output_string(STRING_NO_ENTRIES
);
116 output_string(STRING_USE_HEADER
);
117 for (i
= 0, connection
= buffer
; i
< read
; ++i
, ++connection
)
118 output_string(STRING_USE_ENTRY
, status
[connection
->ui2_status
], connection
->ui2_local
,
119 connection
->ui2_remote
, connection
->ui2_refcount
);
121 if (buffer
!= NULL
) NetApiBufferFree(buffer
);
122 } while (rc
== ERROR_MORE_DATA
);
124 /* Release the status strings */
125 for (i
= 0; i
< sizeof(status
)/sizeof(*status
); i
++)
126 HeapFree(GetProcessHeap(), 0, status
[i
]);
134 static BOOL
StopService(SC_HANDLE SCManager
, SC_HANDLE serviceHandle
)
136 LPENUM_SERVICE_STATUSW dependencies
= NULL
;
137 DWORD buffer_size
= 0;
138 DWORD count
= 0, counter
;
140 SC_HANDLE dependent_serviceHandle
;
141 SERVICE_STATUS_PROCESS ssp
;
143 result
= EnumDependentServicesW(serviceHandle
, SERVICE_ACTIVE
, dependencies
, buffer_size
, &buffer_size
, &count
);
145 if(!result
&& (GetLastError() == ERROR_MORE_DATA
))
147 dependencies
= HeapAlloc(GetProcessHeap(), 0, buffer_size
);
148 if(EnumDependentServicesW(serviceHandle
, SERVICE_ACTIVE
, dependencies
, buffer_size
, &buffer_size
, &count
))
150 for(counter
= 0; counter
< count
; counter
++)
152 output_string(STRING_STOP_DEP
, dependencies
[counter
].lpDisplayName
);
153 dependent_serviceHandle
= OpenServiceW(SCManager
, dependencies
[counter
].lpServiceName
, SC_MANAGER_ALL_ACCESS
);
154 if(dependent_serviceHandle
) result
= StopService(SCManager
, dependent_serviceHandle
);
155 CloseServiceHandle(dependent_serviceHandle
);
156 if(!result
) output_string(STRING_CANT_STOP
, dependencies
[counter
].lpDisplayName
);
161 if(result
) result
= ControlService(serviceHandle
, SERVICE_CONTROL_STOP
, (LPSERVICE_STATUS
)&ssp
);
162 HeapFree(GetProcessHeap(), 0, dependencies
);
166 static BOOL
net_service(int operation
, const WCHAR
* service_name
)
168 SC_HANDLE SCManager
, serviceHandle
;
170 WCHAR service_display_name
[4096];
173 SCManager
= OpenSCManagerW(NULL
, NULL
, SC_MANAGER_ALL_ACCESS
);
176 output_string(STRING_NO_SCM
);
179 serviceHandle
= OpenServiceW(SCManager
, service_name
, SC_MANAGER_ALL_ACCESS
);
182 output_string(STRING_NO_SVCHANDLE
);
183 CloseServiceHandle(SCManager
);
187 buffer_size
= sizeof(service_display_name
)/sizeof(*service_display_name
);
188 GetServiceDisplayNameW(SCManager
, service_name
, service_display_name
, &buffer_size
);
189 if (!service_display_name
[0]) lstrcpyW(service_display_name
, service_name
);
194 output_string(STRING_START_SVC
, service_display_name
);
195 result
= StartServiceW(serviceHandle
, 0, NULL
);
197 if(result
) output_string(STRING_START_SVC_SUCCESS
, service_display_name
);
200 if (!output_error_string(GetLastError()))
201 output_string(STRING_START_SVC_FAIL
, service_display_name
);
205 output_string(STRING_STOP_SVC
, service_display_name
);
206 result
= StopService(SCManager
, serviceHandle
);
208 if(result
) output_string(STRING_STOP_SVC_SUCCESS
, service_display_name
);
211 if (!output_error_string(GetLastError()))
212 output_string(STRING_STOP_SVC_FAIL
, service_display_name
);
217 CloseServiceHandle(serviceHandle
);
218 CloseServiceHandle(SCManager
);
222 static int arg_is(const WCHAR
* str1
, const WCHAR
* str2
)
224 return CompareStringW(LOCALE_USER_DEFAULT
, NORM_IGNORECASE
, str1
, -1, str2
, -1) == CSTR_EQUAL
;
227 int wmain(int argc
, const WCHAR
* argv
[])
229 static const WCHAR helpW
[]={'h','e','l','p',0};
230 static const WCHAR startW
[]={'s','t','a','r','t',0};
231 static const WCHAR stopW
[]={'s','t','o','p',0};
232 static const WCHAR useW
[]={'u','s','e',0};
235 output_string(STRING_USAGE
);
239 if(arg_is(argv
[1], helpW
))
241 output_string(STRING_HELP_USAGE
);
244 if(arg_is(argv
[1], startW
))
248 output_string(STRING_START_USAGE
);
252 if(!net_service(NET_START
, argv
[2]))
259 if(arg_is(argv
[1], stopW
))
263 output_string(STRING_STOP_USAGE
);
267 if(!net_service(NET_STOP
, argv
[2]))
274 if(arg_is(argv
[1], useW
))
276 if(!net_use(argc
, argv
)) return 1;