windows.gaming.input: Fake empty IGamepadStatics::Gamepads vector.
[wine/zf.git] / dlls / wbemprox / service.c
blob1ee0c9903ec415c266da88a89959cee032102c01
1 /*
2 * Win32_Service methods implementation
4 * Copyright 2012 Hans Leidekker for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "wbemcli.h"
28 #include "winsvc.h"
30 #include "wine/debug.h"
31 #include "wbemprox_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
35 static UINT map_error( DWORD error )
37 switch (error)
39 case ERROR_SUCCESS: return 0;
40 case ERROR_ACCESS_DENIED: return 2;
41 case ERROR_DEPENDENT_SERVICES_RUNNING: return 3;
42 case ERROR_INVALID_SERVICE_CONTROL: return 4;
43 case ERROR_SERVICE_CANNOT_ACCEPT_CTRL: return 5;
44 case ERROR_SERVICE_NOT_ACTIVE: return 6;
45 case ERROR_SERVICE_REQUEST_TIMEOUT: return 7;
46 case ERROR_SERVICE_ALREADY_RUNNING: return 10;
47 default:
48 WARN("unknown error %u\n", error);
49 break;
51 return 8;
54 static HRESULT control_service( const WCHAR *name, DWORD control, VARIANT *retval )
56 SC_HANDLE manager, service = NULL;
57 SERVICE_STATUS status;
58 UINT error = 0;
60 if (!(manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE )))
62 error = map_error( GetLastError() );
63 goto done;
65 if (!(service = OpenServiceW( manager, name, SERVICE_STOP|SERVICE_START|SERVICE_PAUSE_CONTINUE )))
67 error = map_error( GetLastError() );
68 goto done;
70 if (!ControlService( service, control, &status )) error = map_error( GetLastError() );
71 CloseServiceHandle( service );
73 done:
74 set_variant( VT_UI4, error, NULL, retval );
75 if (manager) CloseServiceHandle( manager );
76 return S_OK;
79 HRESULT service_pause_service( IWbemClassObject *obj, IWbemContext *context, IWbemClassObject *in, IWbemClassObject **out )
81 VARIANT name, retval;
82 IWbemClassObject *sig, *out_params = NULL;
83 HRESULT hr;
85 TRACE("%p, %p, %p, %p\n", obj, context, in, out);
87 hr = IWbemClassObject_Get( obj, L"Name", 0, &name, NULL, NULL );
88 if (hr != S_OK) return hr;
90 hr = create_signature( L"Win32_Service", L"PauseService", PARAM_OUT, &sig );
91 if (hr != S_OK)
93 VariantClear( &name );
94 return hr;
96 if (out)
98 hr = IWbemClassObject_SpawnInstance( sig, 0, &out_params );
99 if (hr != S_OK)
101 VariantClear( &name );
102 IWbemClassObject_Release( sig );
103 return hr;
106 hr = control_service( V_BSTR(&name), SERVICE_CONTROL_PAUSE, &retval );
107 if (hr != S_OK) goto done;
109 if (out_params)
110 hr = IWbemClassObject_Put( out_params, L"ReturnValue", 0, &retval, CIM_UINT32 );
112 done:
113 VariantClear( &name );
114 IWbemClassObject_Release( sig );
115 if (hr == S_OK && out)
117 *out = out_params;
118 IWbemClassObject_AddRef( out_params );
120 if (out_params) IWbemClassObject_Release( out_params );
121 return hr;
124 HRESULT service_resume_service( IWbemClassObject *obj, IWbemContext *context, IWbemClassObject *in, IWbemClassObject **out )
126 VARIANT name, retval;
127 IWbemClassObject *sig, *out_params = NULL;
128 HRESULT hr;
130 TRACE("%p, %p, %p, %p\n", obj, context, in, out);
132 hr = IWbemClassObject_Get( obj, L"Name", 0, &name, NULL, NULL );
133 if (hr != S_OK) return hr;
135 hr = create_signature( L"Win32_Service", L"ResumeService", PARAM_OUT, &sig );
136 if (hr != S_OK)
138 VariantClear( &name );
139 return hr;
141 if (out)
143 hr = IWbemClassObject_SpawnInstance( sig, 0, &out_params );
144 if (hr != S_OK)
146 VariantClear( &name );
147 IWbemClassObject_Release( sig );
148 return hr;
151 hr = control_service( V_BSTR(&name), SERVICE_CONTROL_CONTINUE, &retval );
152 if (hr != S_OK) goto done;
154 if (out_params)
155 hr = IWbemClassObject_Put( out_params, L"ReturnValue", 0, &retval, CIM_UINT32 );
157 done:
158 VariantClear( &name );
159 IWbemClassObject_Release( sig );
160 if (hr == S_OK && out)
162 *out = out_params;
163 IWbemClassObject_AddRef( out_params );
165 if (out_params) IWbemClassObject_Release( out_params );
166 return hr;
169 static HRESULT start_service( const WCHAR *name, VARIANT *retval )
171 SC_HANDLE manager, service = NULL;
172 UINT error = 0;
174 if (!(manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE )))
176 error = map_error( GetLastError() );
177 goto done;
179 if (!(service = OpenServiceW( manager, name, SERVICE_START )))
181 error = map_error( GetLastError() );
182 goto done;
184 if (!StartServiceW( service, 0, NULL )) error = map_error( GetLastError() );
185 CloseServiceHandle( service );
187 done:
188 set_variant( VT_UI4, error, NULL, retval );
189 if (manager) CloseServiceHandle( manager );
190 return S_OK;
193 HRESULT service_start_service( IWbemClassObject *obj, IWbemContext *context, IWbemClassObject *in, IWbemClassObject **out )
195 VARIANT name, retval;
196 IWbemClassObject *sig, *out_params = NULL;
197 HRESULT hr;
199 TRACE("%p, %p, %p, %p\n", obj, context, in, out);
201 hr = IWbemClassObject_Get( obj, L"Name", 0, &name, NULL, NULL );
202 if (hr != S_OK) return hr;
204 hr = create_signature( L"Win32_Service", L"StartService", PARAM_OUT, &sig );
205 if (hr != S_OK)
207 VariantClear( &name );
208 return hr;
210 if (out)
212 hr = IWbemClassObject_SpawnInstance( sig, 0, &out_params );
213 if (hr != S_OK)
215 VariantClear( &name );
216 IWbemClassObject_Release( sig );
217 return hr;
220 hr = start_service( V_BSTR(&name), &retval );
221 if (hr != S_OK) goto done;
223 if (out_params)
224 hr = IWbemClassObject_Put( out_params, L"ReturnValue", 0, &retval, CIM_UINT32 );
226 done:
227 VariantClear( &name );
228 IWbemClassObject_Release( sig );
229 if (hr == S_OK && out)
231 *out = out_params;
232 IWbemClassObject_AddRef( out_params );
234 if (out_params) IWbemClassObject_Release( out_params );
235 return hr;
238 HRESULT service_stop_service( IWbemClassObject *obj, IWbemContext *context, IWbemClassObject *in, IWbemClassObject **out )
240 VARIANT name, retval;
241 IWbemClassObject *sig, *out_params = NULL;
242 HRESULT hr;
244 TRACE("%p, %p, %p, %p\n", obj, context, in, out);
246 hr = IWbemClassObject_Get( obj, L"Name", 0, &name, NULL, NULL );
247 if (hr != S_OK) return hr;
249 hr = create_signature( L"Win32_Service", L"StopService", PARAM_OUT, &sig );
250 if (hr != S_OK)
252 VariantClear( &name );
253 return hr;
255 if (out)
257 hr = IWbemClassObject_SpawnInstance( sig, 0, &out_params );
258 if (hr != S_OK)
260 VariantClear( &name );
261 IWbemClassObject_Release( sig );
262 return hr;
265 hr = control_service( V_BSTR(&name), SERVICE_CONTROL_STOP, &retval );
266 if (hr != S_OK) goto done;
268 if (out_params)
269 hr = IWbemClassObject_Put( out_params, L"ReturnValue", 0, &retval, CIM_UINT32 );
271 done:
272 VariantClear( &name );
273 IWbemClassObject_Release( sig );
274 if (hr == S_OK && out)
276 *out = out_params;
277 IWbemClassObject_AddRef( out_params );
279 if (out_params) IWbemClassObject_Release( out_params );
280 return hr;