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
30 #include "wine/debug.h"
31 #include "wbemprox_private.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox
);
35 static UINT
map_error( DWORD 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;
48 WARN( "unknown error %lu\n", error
);
54 static HRESULT
control_service( const WCHAR
*name
, DWORD control
, VARIANT
*retval
)
56 SC_HANDLE manager
, service
= NULL
;
57 SERVICE_STATUS status
;
60 if (!(manager
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
)))
62 error
= map_error( GetLastError() );
65 if (!(service
= OpenServiceW( manager
, name
, SERVICE_STOP
|SERVICE_START
|SERVICE_PAUSE_CONTINUE
)))
67 error
= map_error( GetLastError() );
70 if (!ControlService( service
, control
, &status
)) error
= map_error( GetLastError() );
71 CloseServiceHandle( service
);
74 set_variant( VT_UI4
, error
, NULL
, retval
);
75 if (manager
) CloseServiceHandle( manager
);
79 HRESULT
service_pause_service( IWbemClassObject
*obj
, IWbemContext
*context
, IWbemClassObject
*in
, IWbemClassObject
**out
)
82 IWbemClassObject
*sig
, *out_params
= NULL
;
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( WBEMPROX_NAMESPACE_CIMV2
, L
"Win32_Service", L
"PauseService", PARAM_OUT
, &sig
);
93 VariantClear( &name
);
98 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
101 VariantClear( &name
);
102 IWbemClassObject_Release( sig
);
106 hr
= control_service( V_BSTR(&name
), SERVICE_CONTROL_PAUSE
, &retval
);
107 if (hr
!= S_OK
) goto done
;
110 hr
= IWbemClassObject_Put( out_params
, L
"ReturnValue", 0, &retval
, CIM_UINT32
);
113 VariantClear( &name
);
114 IWbemClassObject_Release( sig
);
115 if (hr
== S_OK
&& out
)
118 IWbemClassObject_AddRef( out_params
);
120 if (out_params
) IWbemClassObject_Release( out_params
);
124 HRESULT
service_resume_service( IWbemClassObject
*obj
, IWbemContext
*context
, IWbemClassObject
*in
, IWbemClassObject
**out
)
126 VARIANT name
, retval
;
127 IWbemClassObject
*sig
, *out_params
= NULL
;
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( WBEMPROX_NAMESPACE_CIMV2
, L
"Win32_Service", L
"ResumeService", PARAM_OUT
, &sig
);
138 VariantClear( &name
);
143 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
146 VariantClear( &name
);
147 IWbemClassObject_Release( sig
);
151 hr
= control_service( V_BSTR(&name
), SERVICE_CONTROL_CONTINUE
, &retval
);
152 if (hr
!= S_OK
) goto done
;
155 hr
= IWbemClassObject_Put( out_params
, L
"ReturnValue", 0, &retval
, CIM_UINT32
);
158 VariantClear( &name
);
159 IWbemClassObject_Release( sig
);
160 if (hr
== S_OK
&& out
)
163 IWbemClassObject_AddRef( out_params
);
165 if (out_params
) IWbemClassObject_Release( out_params
);
169 static HRESULT
start_service( const WCHAR
*name
, VARIANT
*retval
)
171 SC_HANDLE manager
, service
= NULL
;
174 if (!(manager
= OpenSCManagerW( NULL
, NULL
, SC_MANAGER_ENUMERATE_SERVICE
)))
176 error
= map_error( GetLastError() );
179 if (!(service
= OpenServiceW( manager
, name
, SERVICE_START
)))
181 error
= map_error( GetLastError() );
184 if (!StartServiceW( service
, 0, NULL
)) error
= map_error( GetLastError() );
185 CloseServiceHandle( service
);
188 set_variant( VT_UI4
, error
, NULL
, retval
);
189 if (manager
) CloseServiceHandle( manager
);
193 HRESULT
service_start_service( IWbemClassObject
*obj
, IWbemContext
*context
, IWbemClassObject
*in
, IWbemClassObject
**out
)
195 VARIANT name
, retval
;
196 IWbemClassObject
*sig
, *out_params
= NULL
;
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( WBEMPROX_NAMESPACE_CIMV2
, L
"Win32_Service", L
"StartService", PARAM_OUT
, &sig
);
207 VariantClear( &name
);
212 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
215 VariantClear( &name
);
216 IWbemClassObject_Release( sig
);
220 hr
= start_service( V_BSTR(&name
), &retval
);
221 if (hr
!= S_OK
) goto done
;
224 hr
= IWbemClassObject_Put( out_params
, L
"ReturnValue", 0, &retval
, CIM_UINT32
);
227 VariantClear( &name
);
228 IWbemClassObject_Release( sig
);
229 if (hr
== S_OK
&& out
)
232 IWbemClassObject_AddRef( out_params
);
234 if (out_params
) IWbemClassObject_Release( out_params
);
238 HRESULT
service_stop_service( IWbemClassObject
*obj
, IWbemContext
*context
, IWbemClassObject
*in
, IWbemClassObject
**out
)
240 VARIANT name
, retval
;
241 IWbemClassObject
*sig
, *out_params
= NULL
;
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( WBEMPROX_NAMESPACE_CIMV2
, L
"Win32_Service", L
"StopService", PARAM_OUT
, &sig
);
252 VariantClear( &name
);
257 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
260 VariantClear( &name
);
261 IWbemClassObject_Release( sig
);
265 hr
= control_service( V_BSTR(&name
), SERVICE_CONTROL_STOP
, &retval
);
266 if (hr
!= S_OK
) goto done
;
269 hr
= IWbemClassObject_Put( out_params
, L
"ReturnValue", 0, &retval
, CIM_UINT32
);
272 VariantClear( &name
);
273 IWbemClassObject_Release( sig
);
274 if (hr
== S_OK
&& out
)
277 IWbemClassObject_AddRef( out_params
);
279 if (out_params
) IWbemClassObject_Release( out_params
);