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
23 #include "resources.h"
25 #define NET_START 0001
28 static int output_string(int msg, ...)
30 char msg_buffer[8192];
33 LoadStringA(GetModuleHandleW(NULL), msg, msg_buffer, sizeof(msg_buffer));
34 va_start(arguments, msg);
35 vprintf(msg_buffer, arguments);
40 static BOOL output_error_string(DWORD error)
43 if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
44 FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
45 NULL, error, 0, (LPSTR)&pBuffer, 0, NULL))
47 fputs(pBuffer, stdout);
54 static BOOL net_use(int argc, char *argv[])
56 USE_INFO_2 *buffer, *connection;
57 DWORD read, total, resume_handle, rc, i;
58 char *status[STRING_RECONN-STRING_OK+1];
64 HMODULE hmod = GetModuleHandleW(NULL);
66 /* Load the status strings */
67 for (i = 0; i < sizeof(status)/sizeof(*status); i++)
69 status[i] = HeapAlloc(GetProcessHeap(), 0, 1024);
70 LoadStringA(hmod, STRING_OK+i, status[i], 1024);
74 rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
75 if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
82 output_string(STRING_NO_ENTRIES);
86 output_string(STRING_USE_HEADER);
87 for (i = 0, connection = buffer; i < read; ++i, ++connection)
88 output_string(STRING_USE_ENTRY, status[connection->ui2_status], connection->ui2_local,
89 connection->ui2_remote, connection->ui2_refcount);
91 if (buffer != NULL) NetApiBufferFree(buffer);
92 } while (rc == ERROR_MORE_DATA);
94 /* Release the status strings */
95 for (i = 0; i < sizeof(status)/sizeof(*status); i++)
96 HeapFree(GetProcessHeap(), 0, status[i]);
104 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
106 LPENUM_SERVICE_STATUSW dependencies = NULL;
107 DWORD buffer_size = 0;
108 DWORD count = 0, counter;
110 SC_HANDLE dependent_serviceHandle;
111 SERVICE_STATUS_PROCESS ssp;
113 result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
115 if(!result && (GetLastError() == ERROR_MORE_DATA))
117 dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
118 if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
120 for(counter = 0; counter < count; counter++)
122 output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
123 dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
124 if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
125 CloseServiceHandle(dependent_serviceHandle);
126 if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
131 if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
132 HeapFree(GetProcessHeap(), 0, dependencies);
136 static BOOL net_service(int operation, char *service_name)
138 SC_HANDLE SCManager, serviceHandle;
140 char service_display_name[4096];
141 DWORD buffer_size = sizeof(service_display_name);
143 SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
146 output_string(STRING_NO_SCM);
149 serviceHandle = OpenServiceA(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
152 output_string(STRING_NO_SVCHANDLE);
153 CloseServiceHandle(SCManager);
158 GetServiceDisplayNameA(SCManager, service_name, service_display_name, &buffer_size);
159 if (!service_display_name[0]) strcpy(service_display_name, service_name);
164 output_string(STRING_START_SVC, service_display_name);
165 result = StartServiceW(serviceHandle, 0, NULL);
167 if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
170 if (!output_error_string(GetLastError()))
171 output_string(STRING_START_SVC_FAIL, service_display_name);
175 output_string(STRING_STOP_SVC, service_display_name);
176 result = StopService(SCManager, serviceHandle);
178 if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
181 if (!output_error_string(GetLastError()))
182 output_string(STRING_STOP_SVC_FAIL, service_display_name);
187 CloseServiceHandle(serviceHandle);
188 CloseServiceHandle(SCManager);
192 int main(int argc, char *argv[])
196 output_string(STRING_USAGE);
200 if(!strcasecmp(argv[1], "help"))
202 output_string(STRING_HELP_USAGE);
205 if(!strcasecmp(argv[1], "start"))
209 output_string(STRING_START_USAGE);
213 if(!net_service(NET_START, argv[2]))
220 if(!strcasecmp(argv[1], "stop"))
224 output_string(STRING_STOP_USAGE);
228 if(!net_service(NET_STOP, argv[2]))
235 if(!strcasecmp(argv[1], "use"))
237 if(!net_use(argc, argv)) return 1;