oleaut32: Fix a signed/unsigned mismatch.
[wine] / programs / net / net.c
1 /*
2  * Copyright 2007 Tim Schwartz
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <windows.h>
22 #include "resources.h"
23
24 #define NET_START 0001
25 #define NET_STOP  0002
26
27 int output_string(int msg, ...)
28 {
29     char msg_buffer[8192];
30     va_list arguments;
31
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;
37 }
38
39 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
40 {
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;
47
48     result = EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
49
50     if(!result && (GetLastError() == ERROR_MORE_DATA))
51     {
52         dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
53         if(EnumDependentServices(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
54         {
55             for(counter = 0; counter < count; counter++)
56             {
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);
62            }
63         }
64     }
65
66     if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
67     HeapFree(GetProcessHeap(), 0, dependencies);
68     return result;
69 }
70
71 static BOOL net_service(int operation, char *service_name)
72 {
73     SC_HANDLE SCManager, serviceHandle;
74     BOOL result = 0;
75     char service_display_name[4096];
76     DWORD buffer_size = sizeof(service_display_name);
77
78     SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
79     if(!SCManager)
80     {
81         output_string(STRING_NO_SCM);
82         return FALSE;
83     }
84     serviceHandle = OpenService(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
85     if(!serviceHandle)
86     {
87         output_string(STRING_NO_SVCHANDLE);
88         CloseServiceHandle(SCManager);
89         return FALSE;
90     }
91
92
93     GetServiceDisplayName(SCManager, service_name, service_display_name, &buffer_size);
94     if (!service_display_name[0]) strcpy(service_display_name, service_name);
95
96     switch(operation)
97     {
98     case NET_START:
99         output_string(STRING_START_SVC, service_display_name);
100         result = StartService(serviceHandle, 0, NULL);
101
102         if(result) output_string(STRING_START_SVC_SUCCESS);
103         else output_string(STRING_START_SVC_FAIL);
104         break;
105     case NET_STOP:
106         output_string(STRING_STOP_SVC, service_display_name);
107         result = StopService(SCManager, serviceHandle);
108
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;
112     }
113
114     CloseServiceHandle(serviceHandle);
115     CloseServiceHandle(SCManager);
116     return result;
117 }
118
119 int main(int argc, char *argv[])
120 {
121     if (argc < 2)
122     {
123         output_string(STRING_USAGE);
124         return 1;
125     }
126
127     if(!strcasecmp(argv[1], "help"))
128     {
129         output_string(STRING_HELP_USAGE);
130     }
131
132     if(!strcasecmp(argv[1], "start"))
133     {
134         if(argc < 3)
135         {
136             output_string(STRING_START_USAGE);
137             return 1;
138         }
139
140         if(!net_service(NET_START, argv[2]))
141         {
142             return 1;
143         }
144         return 0;
145     }
146
147     if(!strcasecmp(argv[1], "stop"))
148     {
149         if(argc < 3)
150         {
151             output_string(STRING_STOP_USAGE);
152             return 1;
153         }
154
155         if(!net_service(NET_STOP, argv[2]))
156         {
157             return 1;
158         }
159         return 0;
160     }
161
162     return 0;
163 }