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
22 #include "resources.h"
24 #define NET_START 0001
27 int output_string(int msg, ...)
29 char msg_buffer[8192];
32 LoadString(GetModuleHandle(NULL), msg, msg_buffer, sizeof(msg_buffer));
33 va_start(arguments, msg);
34 vprintf(msg_buffer, arguments);
39 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
41 LPENUM_SERVICE_STATUS dependencies = NULL;
42 DWORD buffer_size = 0;
43 DWORD count = 0, counter;
45 SC_HANDLE dependent_serviceHandle;
46 SERVICE_STATUS_PROCESS ssp;
48 result = EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
50 if(!result && (GetLastError() == ERROR_MORE_DATA))
52 dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
53 if(EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
55 for(counter = 0; counter < count; counter++)
57 output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
58 dependent_serviceHandle = OpenService(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
59 if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
60 CloseServiceHandle(dependent_serviceHandle);
61 if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
66 if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
67 HeapFree(GetProcessHeap(), 0, dependencies);
71 static BOOL net_service(int operation, char *service_name)
73 SC_HANDLE SCManager, serviceHandle;
75 char service_display_name[4096];
76 DWORD buffer_size = sizeof(service_display_name);
78 SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
81 output_string(STRING_NO_SCM);
84 serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
87 output_string(STRING_NO_SVCHANDLE);
88 CloseServiceHandle(SCManager);
93 GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
94 if (!service_display_name[0]) strcpy(service_display_name, service_name);
99 output_string(STRING_START_SVC, service_display_name);
100 result = StartService(serviceHandle, 0, NULL);
102 if(result) output_string(STRING_START_SVC_SUCCESS);
103 else output_string(STRING_START_SVC_FAIL);
106 output_string(STRING_STOP_SVC, service_display_name);
107 result = StopService(SCManager, serviceHandle);
109 if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
110 else output_string(STRING_STOP_SVC_FAIL, service_display_name);
114 CloseServiceHandle(serviceHandle);
115 CloseServiceHandle(SCManager);
119 int main(int argc, char *argv[])
123 output_string(STRING_USAGE);
127 if(!strcasecmp(argv[1], "help"))
129 output_string(STRING_HELP_USAGE);
132 if(!strcasecmp(argv[1], "start"))
136 output_string(STRING_START_USAGE);
140 if(!net_service(NET_START, argv[2]))
147 if(!strcasecmp(argv[1], "stop"))
151 output_string(STRING_STOP_USAGE);
155 if(!net_service(NET_STOP, argv[2]))