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 if(!result) printf("Could not stop service %s\n", dependencies[counter].lpDisplayName);
52 if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
53 HeapFree(GetProcessHeap(), 0, dependencies);
57 static BOOL net_service(int operation, char *service_name)
59 SC_HANDLE SCManager, serviceHandle;
61 char service_display_name[4096];
62 DWORD buffer_size = sizeof(service_display_name);
64 SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
67 printf("Couldn't get handle to SCManager.\n");
70 serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
73 printf("Couldn't get handle to service.\n");
74 CloseServiceHandle(SCManager);
79 GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
80 if (!service_display_name[0]) strcpy(service_display_name, service_name);
85 printf("The %s service is starting.\n", service_display_name);
86 result = StartService(serviceHandle, 0, NULL);
88 printf("The %s service ", service_display_name);
89 if(!result) printf("failed to start.\n");
90 else printf("was started successfully.\n");
93 printf("The %s service is stopping.\n", service_display_name);
94 result = StopService(SCManager, serviceHandle);
96 printf("The %s service ", service_display_name);
97 if(!result) printf("failed to stop.\n");
98 else printf("was stopped successfully.\n");
102 CloseServiceHandle(serviceHandle);
103 CloseServiceHandle(SCManager);
107 int main(int argc, char *argv[])
112 printf("The syntax of this command is:\n\n");
113 printf("NET [ HELP | START | STOP ]\n");
117 if(!strcasecmp(argv[1], "help"))
119 printf("The syntax of this command is:\n\n");
120 printf("NET HELP command\n -or-\nNET command /HELP\n\n");
121 printf(" Commands available are:\n");
122 printf(" NET HELP NET START NET STOP\n");
125 if(!strcasecmp(argv[1], "start"))
129 printf("Specify service name to start.\n");
133 if(!net_service(NET_START, argv[2]))
140 if(!strcasecmp(argv[1], "stop"))
144 printf("Specify service name to stop.\n");
148 if(!net_service(NET_STOP, argv[2]))