comdlg32: Do not crash with PageSetupDlg(NULL).
[wine/winequartzdrv.git] / programs / net / net.c
blob4bc365279dd45487d88ba2d75d6c5a7c84ba652e
1 /*
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
19 #include <stdio.h>
20 #include <string.h>
21 #include <windows.h>
22 #include "resources.h"
24 #define NET_START 0001
25 #define NET_STOP 0002
27 int output_string(int msg, ...)
29 char msg_buffer[8192];
30 va_list arguments;
32 LoadString(GetModuleHandle(NULL), msg, msg_buffer, sizeof(msg_buffer));
33 va_start(arguments, msg);
34 vprintf(msg_buffer, arguments);
35 va_end(arguments);
36 return 0;
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;
44 BOOL result;
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);
68 return result;
71 static BOOL net_service(int operation, char *service_name)
73 SC_HANDLE SCManager, serviceHandle;
74 BOOL result = 0;
75 char service_display_name[4096];
76 DWORD buffer_size = sizeof(service_display_name);
78 SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
79 if(!SCManager)
81 output_string(STRING_NO_SCM);
82 return FALSE;
84 serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
85 if(!serviceHandle)
87 output_string(STRING_NO_SVCHANDLE);
88 CloseServiceHandle(SCManager);
89 return FALSE;
93 GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
94 if (!service_display_name[0]) strcpy(service_display_name, service_name);
96 switch(operation)
98 case NET_START:
99 output_string(STRING_START_SVC, service_display_name);
100 result = StartService(serviceHandle, 0, NULL);
102 if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
103 else output_string(STRING_START_SVC_FAIL, service_display_name);
104 break;
105 case NET_STOP:
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);
111 break;
114 CloseServiceHandle(serviceHandle);
115 CloseServiceHandle(SCManager);
116 return result;
119 int main(int argc, char *argv[])
121 if (argc < 2)
123 output_string(STRING_USAGE);
124 return 1;
127 if(!strcasecmp(argv[1], "help"))
129 output_string(STRING_HELP_USAGE);
132 if(!strcasecmp(argv[1], "start"))
134 if(argc < 3)
136 output_string(STRING_START_USAGE);
137 return 1;
140 if(!net_service(NET_START, argv[2]))
142 return 1;
144 return 0;
147 if(!strcasecmp(argv[1], "stop"))
149 if(argc < 3)
151 output_string(STRING_STOP_USAGE);
152 return 1;
155 if(!net_service(NET_STOP, argv[2]))
157 return 1;
159 return 0;
162 return 0;