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 #define NET_START 0001
26 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
28 LPENUM_SERVICE_STATUS dependencies = NULL;
29 DWORD buffer_size = 0;
30 DWORD count = 0, counter;
32 SC_HANDLE dependent_serviceHandle;
33 SERVICE_STATUS_PROCESS ssp;
35 result = EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
37 if(!result && (GetLastError() == ERROR_MORE_DATA))
39 dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
40 if(EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
42 for(counter = 0; counter < count; counter++)
44 printf("Stopping dependent service: %s\n", dependencies[counter].lpDisplayName);
45 dependent_serviceHandle = OpenService(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
46 if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
47 CloseServiceHandle(dependent_serviceHandle);
48 if(!result) printf("Could not stop service %s\n", dependencies[counter].lpDisplayName);
53 if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
54 HeapFree(GetProcessHeap(), 0, dependencies);
58 static BOOL net_service(int operation, char *service_name)
60 SC_HANDLE SCManager, serviceHandle;
62 char service_display_name[4096];
63 DWORD buffer_size = sizeof(service_display_name);
65 SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
68 printf("Couldn't get handle to SCManager.\n");
71 serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
74 printf("Couldn't get handle to service.\n");
75 CloseServiceHandle(SCManager);
80 GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
81 if (!service_display_name[0]) strcpy(service_display_name, service_name);
86 printf("The %s service is starting.\n", service_display_name);
87 result = StartService(serviceHandle, 0, NULL);
89 printf("The %s service ", service_display_name);
90 if(!result) printf("failed to start.\n");
91 else printf("was started successfully.\n");
94 printf("The %s service is stopping.\n", service_display_name);
95 result = StopService(SCManager, serviceHandle);
97 printf("The %s service ", service_display_name);
98 if(!result) printf("failed to stop.\n");
99 else printf("was stopped successfully.\n");
103 CloseServiceHandle(serviceHandle);
104 CloseServiceHandle(SCManager);
108 int main(int argc, char *argv[])
113 printf("The syntax of this command is:\n\n");
114 printf("NET [ HELP | START | STOP ]\n");
118 if(!strcasecmp(argv[1], "help"))
120 printf("The syntax of this command is:\n\n");
121 printf("NET HELP command\n -or-\nNET command /HELP\n\n");
122 printf(" Commands available are:\n");
123 printf(" NET HELP NET START NET STOP\n");
126 if(!strcasecmp(argv[1], "start"))
130 printf("Specify service name to start.\n");
134 if(!net_service(NET_START, argv[2]))
141 if(!strcasecmp(argv[1], "stop"))
145 printf("Specify service name to stop.\n");
149 if(!net_service(NET_STOP, argv[2]))