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
31 #include "wine/debug.h"
32 #include "wbemprox_private.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
36 static UINT map_error( DWORD error )
40 case ERROR_SUCCESS: return 0;
41 case ERROR_ACCESS_DENIED: return 2;
42 case ERROR_DEPENDENT_SERVICES_RUNNING: return 3;
43 case ERROR_INVALID_SERVICE_CONTROL: return 4;
44 case ERROR_SERVICE_CANNOT_ACCEPT_CTRL: return 5;
45 case ERROR_SERVICE_NOT_ACTIVE: return 6;
46 case ERROR_SERVICE_REQUEST_TIMEOUT: return 7;
47 case ERROR_SERVICE_ALREADY_RUNNING: return 10;
49 WARN("unknown error %u\n", error);
55 static HRESULT control_service( const WCHAR *name, DWORD control, VARIANT *retval )
57 SC_HANDLE manager, service = NULL;
58 SERVICE_STATUS status;
61 if (!(manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE )))
63 error = map_error( GetLastError() );
66 if (!(service = OpenServiceW( manager, name, SERVICE_STOP|SERVICE_START|SERVICE_PAUSE_CONTINUE )))
68 error = map_error( GetLastError() );
71 if (!ControlService( service, control, &status )) error = map_error( GetLastError() );
74 set_variant( VT_UI4, error, NULL, retval );
75 CloseServiceHandle( service );
76 CloseServiceHandle( manager );
80 HRESULT service_pause_service( IWbemClassObject *obj, IWbemClassObject *in, IWbemClassObject **out )
83 IWbemClassObject *sig;
86 TRACE("%p, %p, %p\n", obj, in, out);
88 hr = IWbemClassObject_Get( obj, prop_nameW, 0, &name, NULL, NULL );
89 if (hr != S_OK) return hr;
91 hr = create_signature( class_serviceW, method_pauseserviceW, PARAM_OUT, &sig );
94 VariantClear( &name );
97 hr = IWbemClassObject_SpawnInstance( sig, 0, out );
100 VariantClear( &name );
101 IWbemClassObject_Release( sig );
104 hr = control_service( V_BSTR(&name), SERVICE_CONTROL_PAUSE, &retval );
105 if (hr != S_OK) goto done;
106 hr = IWbemClassObject_Put( *out, param_returnvalueW, 0, &retval, CIM_UINT32 );
109 VariantClear( &name );
110 IWbemClassObject_Release( sig );
111 if (hr != S_OK) IWbemClassObject_Release( *out );
115 HRESULT service_resume_service( IWbemClassObject *obj, IWbemClassObject *in, IWbemClassObject **out )
117 VARIANT name, retval;
118 IWbemClassObject *sig;
121 TRACE("%p, %p, %p\n", obj, in, out);
123 hr = IWbemClassObject_Get( obj, prop_nameW, 0, &name, NULL, NULL );
124 if (hr != S_OK) return hr;
126 hr = create_signature( class_serviceW, method_resumeserviceW, PARAM_OUT, &sig );
129 VariantClear( &name );
132 hr = IWbemClassObject_SpawnInstance( sig, 0, out );
135 VariantClear( &name );
136 IWbemClassObject_Release( sig );
139 hr = control_service( V_BSTR(&name), SERVICE_CONTROL_CONTINUE, &retval );
140 if (hr != S_OK) goto done;
141 hr = IWbemClassObject_Put( *out, param_returnvalueW, 0, &retval, CIM_UINT32 );
144 VariantClear( &name );
145 IWbemClassObject_Release( sig );
146 if (hr != S_OK) IWbemClassObject_Release( *out );
150 static HRESULT start_service( const WCHAR *name, VARIANT *retval )
152 SC_HANDLE manager, service = NULL;
155 if (!(manager = OpenSCManagerW( NULL, NULL, SC_MANAGER_ENUMERATE_SERVICE )))
157 error = map_error( GetLastError() );
160 if (!(service = OpenServiceW( manager, name, SERVICE_START )))
162 error = map_error( GetLastError() );
165 if (!StartServiceW( service, 0, NULL )) error = map_error( GetLastError() );
168 set_variant( VT_UI4, error, NULL, retval );
169 CloseServiceHandle( service );
170 CloseServiceHandle( manager );
174 HRESULT service_start_service( IWbemClassObject *obj, IWbemClassObject *in, IWbemClassObject **out )
176 VARIANT name, retval;
177 IWbemClassObject *sig;
180 TRACE("%p, %p, %p\n", obj, in, out);
182 hr = IWbemClassObject_Get( obj, prop_nameW, 0, &name, NULL, NULL );
183 if (hr != S_OK) return hr;
185 hr = create_signature( class_serviceW, method_startserviceW, PARAM_OUT, &sig );
188 VariantClear( &name );
191 hr = IWbemClassObject_SpawnInstance( sig, 0, out );
194 VariantClear( &name );
195 IWbemClassObject_Release( sig );
198 hr = start_service( V_BSTR(&name), &retval );
199 if (hr != S_OK) goto done;
200 hr = IWbemClassObject_Put( *out, param_returnvalueW, 0, &retval, CIM_UINT32 );
203 VariantClear( &name );
204 IWbemClassObject_Release( sig );
205 if (hr != S_OK) IWbemClassObject_Release( *out );
209 HRESULT service_stop_service( IWbemClassObject *obj, IWbemClassObject *in, IWbemClassObject **out )
211 VARIANT name, retval;
212 IWbemClassObject *sig;
215 TRACE("%p, %p, %p\n", obj, in, out);
217 hr = IWbemClassObject_Get( obj, prop_nameW, 0, &name, NULL, NULL );
218 if (hr != S_OK) return hr;
220 hr = create_signature( class_serviceW, method_stopserviceW, PARAM_OUT, &sig );
223 VariantClear( &name );
226 hr = IWbemClassObject_SpawnInstance( sig, 0, out );
229 VariantClear( &name );
230 IWbemClassObject_Release( sig );
233 hr = control_service( V_BSTR(&name), SERVICE_CONTROL_STOP, &retval );
234 if (hr != S_OK) goto done;
235 hr = IWbemClassObject_Put( *out, param_returnvalueW, 0, &retval, CIM_UINT32 );
238 VariantClear( &name );
239 IWbemClassObject_Release( sig );
240 if (hr != S_OK) IWbemClassObject_Release( *out );