2 * StdRegProv 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 HRESULT
to_bstr_array( BSTR
*strings
, DWORD count
, VARIANT
*var
)
41 if (!(sa
= SafeArrayCreateVector( VT_BSTR
, 0, count
))) return E_OUTOFMEMORY
;
42 for (i
= 0; i
< count
; i
++)
44 if ((hr
= SafeArrayPutElement( sa
, &i
, strings
[i
] )) != S_OK
)
46 SafeArrayDestroy( sa
);
50 set_variant( VT_BSTR
|VT_ARRAY
, 0, sa
, var
);
54 static void free_bstr_array( BSTR
*strings
, DWORD count
)
57 SysFreeString( *(strings
++) );
60 static HRESULT
to_i4_array( DWORD
*values
, DWORD count
, VARIANT
*var
)
66 if (!(sa
= SafeArrayCreateVector( VT_I4
, 0, count
))) return E_OUTOFMEMORY
;
67 for (i
= 0; i
< count
; i
++)
69 if ((hr
= SafeArrayPutElement( sa
, &i
, &values
[i
] )) != S_OK
)
71 SafeArrayDestroy( sa
);
75 set_variant( VT_I4
|VT_ARRAY
, 0, sa
, var
);
79 static HRESULT
create_key( HKEY root
, const WCHAR
*subkey
, VARIANT
*retval
)
84 TRACE("%p, %s\n", root
, debugstr_w(subkey
));
86 res
= RegCreateKeyExW( root
, subkey
, 0, NULL
, 0, 0, NULL
, &hkey
, NULL
);
87 set_variant( VT_UI4
, res
, NULL
, retval
);
93 return HRESULT_FROM_WIN32( res
);
96 HRESULT
reg_create_key( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
98 VARIANT defkey
, subkey
, retval
;
99 IWbemClassObject
*sig
, *out_params
= NULL
;
102 TRACE("%p, %p\n", in
, out
);
104 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
105 if (hr
!= S_OK
) return hr
;
106 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
107 if (hr
!= S_OK
) return hr
;
109 hr
= create_signature( class_stdregprovW
, method_createkeyW
, PARAM_OUT
, &sig
);
112 VariantClear( &subkey
);
117 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
120 VariantClear( &subkey
);
121 IWbemClassObject_Release( sig
);
125 hr
= create_key( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), &retval
);
126 if (hr
== S_OK
&& out_params
)
127 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
129 VariantClear( &subkey
);
130 IWbemClassObject_Release( sig
);
131 if (hr
== S_OK
&& out
)
134 IWbemClassObject_AddRef( out_params
);
136 if (out_params
) IWbemClassObject_Release( out_params
);
140 static HRESULT
enum_key( HKEY root
, const WCHAR
*subkey
, VARIANT
*names
, VARIANT
*retval
)
146 DWORD count
= 2, len
= ARRAY_SIZE( buf
);
149 TRACE("%p, %s\n", root
, debugstr_w(subkey
));
151 if (!(strings
= heap_alloc( count
* sizeof(BSTR
) ))) return E_OUTOFMEMORY
;
152 if ((res
= RegOpenKeyExW( root
, subkey
, 0, KEY_ENUMERATE_SUB_KEYS
, &hkey
)))
154 set_variant( VT_UI4
, res
, NULL
, retval
);
155 heap_free( strings
);
163 if (!(tmp
= heap_realloc( strings
, count
* sizeof(BSTR
) )))
166 return E_OUTOFMEMORY
;
170 if ((res
= RegEnumKeyW( hkey
, i
, buf
, len
)) == ERROR_NO_MORE_ITEMS
)
172 if (i
) res
= ERROR_SUCCESS
;
176 if (!(strings
[i
] = SysAllocString( buf
)))
178 for (i
--; i
>= 0; i
--) SysFreeString( strings
[i
] );
184 if (hr
== S_OK
&& !res
)
186 hr
= to_bstr_array( strings
, i
, names
);
187 free_bstr_array( strings
, i
);
189 set_variant( VT_UI4
, res
, NULL
, retval
);
191 heap_free( strings
);
195 HRESULT
reg_enum_key( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
197 VARIANT defkey
, subkey
, names
, retval
;
198 IWbemClassObject
*sig
, *out_params
= NULL
;
201 TRACE("%p, %p\n", in
, out
);
203 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
204 if (hr
!= S_OK
) return hr
;
205 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
206 if (hr
!= S_OK
) return hr
;
208 hr
= create_signature( class_stdregprovW
, method_enumkeyW
, PARAM_OUT
, &sig
);
211 VariantClear( &subkey
);
216 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
219 VariantClear( &subkey
);
220 IWbemClassObject_Release( sig
);
224 VariantInit( &names
);
225 hr
= enum_key( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), &names
, &retval
);
226 if (hr
!= S_OK
) goto done
;
229 if (!V_UI4( &retval
))
231 hr
= IWbemClassObject_Put( out_params
, param_namesW
, 0, &names
, CIM_STRING
|CIM_FLAG_ARRAY
);
232 if (hr
!= S_OK
) goto done
;
234 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
238 VariantClear( &names
);
239 VariantClear( &subkey
);
240 IWbemClassObject_Release( sig
);
241 if (hr
== S_OK
&& out
)
244 IWbemClassObject_AddRef( out_params
);
246 if (out_params
) IWbemClassObject_Release( out_params
);
250 static HRESULT
enum_values( HKEY root
, const WCHAR
*subkey
, VARIANT
*names
, VARIANT
*types
, VARIANT
*retval
)
254 BSTR
*value_names
= NULL
;
255 DWORD count
, buflen
, len
, *value_types
= NULL
;
259 TRACE("%p, %s\n", root
, debugstr_w(subkey
));
261 if ((res
= RegOpenKeyExW( root
, subkey
, 0, KEY_QUERY_VALUE
, &hkey
))) goto done
;
262 if ((res
= RegQueryInfoKeyW( hkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &count
, &buflen
, NULL
, NULL
, NULL
)))
266 if (!(buf
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
) ))) goto done
;
267 if (!(value_names
= heap_alloc( count
* sizeof(BSTR
) ))) goto done
;
268 if (!(value_types
= heap_alloc( count
* sizeof(DWORD
) ))) goto done
;
274 res
= RegEnumValueW( hkey
, i
, buf
, &len
, NULL
, &value_types
[i
], NULL
, NULL
);
275 if (res
== ERROR_NO_MORE_ITEMS
)
277 if (i
) res
= ERROR_SUCCESS
;
281 if (!(value_names
[i
] = SysAllocString( buf
)))
283 for (i
--; i
>= 0; i
--) SysFreeString( value_names
[i
] );
289 if (hr
== S_OK
&& !res
)
291 hr
= to_bstr_array( value_names
, i
, names
);
292 free_bstr_array( value_names
, i
);
293 if (hr
== S_OK
) hr
= to_i4_array( value_types
, i
, types
);
297 set_variant( VT_UI4
, res
, NULL
, retval
);
299 heap_free( value_names
);
300 heap_free( value_types
);
305 HRESULT
reg_enum_values( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
307 VARIANT defkey
, subkey
, names
, types
, retval
;
308 IWbemClassObject
*sig
, *out_params
= NULL
;
311 TRACE("%p, %p\n", in
, out
);
313 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
314 if (hr
!= S_OK
) return hr
;
315 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
316 if (hr
!= S_OK
) return hr
;
318 hr
= create_signature( class_stdregprovW
, method_enumvaluesW
, PARAM_OUT
, &sig
);
321 VariantClear( &subkey
);
326 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
329 VariantClear( &subkey
);
330 IWbemClassObject_Release( sig
);
334 VariantInit( &names
);
335 VariantInit( &types
);
336 hr
= enum_values( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), &names
, &types
, &retval
);
337 if (hr
!= S_OK
) goto done
;
340 if (!V_UI4( &retval
))
342 hr
= IWbemClassObject_Put( out_params
, param_namesW
, 0, &names
, CIM_STRING
|CIM_FLAG_ARRAY
);
343 if (hr
!= S_OK
) goto done
;
344 hr
= IWbemClassObject_Put( out_params
, param_typesW
, 0, &types
, CIM_SINT32
|CIM_FLAG_ARRAY
);
345 if (hr
!= S_OK
) goto done
;
347 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
351 VariantClear( &types
);
352 VariantClear( &names
);
353 VariantClear( &subkey
);
354 IWbemClassObject_Release( sig
);
355 if (hr
== S_OK
&& out
)
358 IWbemClassObject_AddRef( out_params
);
360 if (out_params
) IWbemClassObject_Release( out_params
);
364 static HRESULT
get_stringvalue( HKEY root
, const WCHAR
*subkey
, const WCHAR
*name
, VARIANT
*value
, VARIANT
*retval
)
371 TRACE("%p, %s, %s\n", root
, debugstr_w(subkey
), debugstr_w(name
));
373 if ((res
= RegGetValueW( root
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
))) goto done
;
374 if (!(str
= SysAllocStringLen( NULL
, size
/ sizeof(WCHAR
) - 1 )))
379 if (!(res
= RegGetValueW( root
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, str
, &size
)))
380 set_variant( VT_BSTR
, 0, str
, value
);
383 set_variant( VT_UI4
, res
, NULL
, retval
);
384 if (res
) SysFreeString( str
);
388 HRESULT
reg_get_stringvalue( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
390 VARIANT defkey
, subkey
, name
, value
, retval
;
391 IWbemClassObject
*sig
, *out_params
= NULL
;
394 TRACE("%p, %p\n", in
, out
);
396 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
397 if (hr
!= S_OK
) return hr
;
398 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
399 if (hr
!= S_OK
) return hr
;
400 hr
= IWbemClassObject_Get( in
, param_valuenameW
, 0, &name
, NULL
, NULL
);
401 if (hr
!= S_OK
) return hr
;
403 hr
= create_signature( class_stdregprovW
, method_getstringvalueW
, PARAM_OUT
, &sig
);
406 VariantClear( &name
);
407 VariantClear( &subkey
);
412 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
415 VariantClear( &name
);
416 VariantClear( &subkey
);
417 IWbemClassObject_Release( sig
);
421 VariantInit( &value
);
422 hr
= get_stringvalue( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), V_BSTR(&name
), &value
, &retval
);
423 if (hr
!= S_OK
) goto done
;
426 if (!V_UI4( &retval
))
428 hr
= IWbemClassObject_Put( out_params
, param_valueW
, 0, &value
, CIM_STRING
);
429 if (hr
!= S_OK
) goto done
;
431 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
435 VariantClear( &name
);
436 VariantClear( &subkey
);
437 IWbemClassObject_Release( sig
);
438 if (hr
== S_OK
&& out
)
441 IWbemClassObject_AddRef( out_params
);
443 if (out_params
) IWbemClassObject_Release( out_params
);