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
enum_key( HKEY root
, const WCHAR
*subkey
, VARIANT
*names
, VARIANT
*retval
)
85 DWORD count
= 2, len
= sizeof(buf
)/sizeof(buf
[0]);
88 TRACE("%p, %s\n", root
, debugstr_w(subkey
));
90 if (!(strings
= heap_alloc( count
* sizeof(BSTR
) ))) return E_OUTOFMEMORY
;
91 if ((res
= RegOpenKeyExW( root
, subkey
, 0, KEY_ENUMERATE_SUB_KEYS
, &hkey
)))
93 set_variant( VT_UI4
, res
, NULL
, retval
);
102 if (!(tmp
= heap_realloc( strings
, count
* sizeof(BSTR
) )))
105 return E_OUTOFMEMORY
;
109 if ((res
= RegEnumKeyW( hkey
, i
, buf
, len
)) == ERROR_NO_MORE_ITEMS
)
111 if (i
) res
= ERROR_SUCCESS
;
115 if (!(strings
[i
] = SysAllocString( buf
)))
117 for (i
--; i
>= 0; i
--) SysFreeString( strings
[i
] );
123 if (hr
== S_OK
&& !res
)
125 hr
= to_bstr_array( strings
, i
, names
);
126 free_bstr_array( strings
, i
);
128 set_variant( VT_UI4
, res
, NULL
, retval
);
130 heap_free( strings
);
134 HRESULT
reg_enum_key( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
136 VARIANT defkey
, subkey
, names
, retval
;
137 IWbemClassObject
*sig
, *out_params
= NULL
;
140 TRACE("%p, %p\n", in
, out
);
142 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
143 if (hr
!= S_OK
) return hr
;
144 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
145 if (hr
!= S_OK
) return hr
;
147 hr
= create_signature( class_stdregprovW
, method_enumkeyW
, PARAM_OUT
, &sig
);
150 VariantClear( &subkey
);
155 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
158 VariantClear( &subkey
);
159 IWbemClassObject_Release( sig
);
163 VariantInit( &names
);
164 hr
= enum_key( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), &names
, &retval
);
165 if (hr
!= S_OK
) goto done
;
168 if (!V_UI4( &retval
))
170 hr
= IWbemClassObject_Put( out_params
, param_namesW
, 0, &names
, CIM_STRING
|CIM_FLAG_ARRAY
);
171 if (hr
!= S_OK
) goto done
;
173 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
177 VariantClear( &names
);
178 VariantClear( &subkey
);
179 IWbemClassObject_Release( sig
);
180 if (hr
== S_OK
&& out
)
183 IWbemClassObject_AddRef( out_params
);
185 if (out_params
) IWbemClassObject_Release( out_params
);
189 static HRESULT
enum_values( HKEY root
, const WCHAR
*subkey
, VARIANT
*names
, VARIANT
*types
, VARIANT
*retval
)
193 BSTR
*value_names
= NULL
;
194 DWORD count
, buflen
, len
, *value_types
= NULL
;
198 TRACE("%p, %s\n", root
, debugstr_w(subkey
));
200 if ((res
= RegOpenKeyExW( root
, subkey
, 0, KEY_QUERY_VALUE
, &hkey
))) goto done
;
201 if ((res
= RegQueryInfoKeyW( hkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &count
, &buflen
, NULL
, NULL
, NULL
)))
205 if (!(buf
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
) ))) goto done
;
206 if (!(value_names
= heap_alloc( count
* sizeof(BSTR
) ))) goto done
;
207 if (!(value_types
= heap_alloc( count
* sizeof(DWORD
) ))) goto done
;
213 res
= RegEnumValueW( hkey
, i
, buf
, &len
, NULL
, &value_types
[i
], NULL
, NULL
);
214 if (res
== ERROR_NO_MORE_ITEMS
)
216 if (i
) res
= ERROR_SUCCESS
;
220 if (!(value_names
[i
] = SysAllocString( buf
)))
222 for (i
--; i
>= 0; i
--) SysFreeString( value_names
[i
] );
228 if (hr
== S_OK
&& !res
)
230 hr
= to_bstr_array( value_names
, i
, names
);
231 free_bstr_array( value_names
, i
);
232 if (hr
== S_OK
) hr
= to_i4_array( value_types
, i
, types
);
236 set_variant( VT_UI4
, res
, NULL
, retval
);
238 heap_free( value_names
);
239 heap_free( value_types
);
244 HRESULT
reg_enum_values( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
246 VARIANT defkey
, subkey
, names
, types
, retval
;
247 IWbemClassObject
*sig
, *out_params
= NULL
;
250 TRACE("%p, %p\n", in
, out
);
252 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
253 if (hr
!= S_OK
) return hr
;
254 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
255 if (hr
!= S_OK
) return hr
;
257 hr
= create_signature( class_stdregprovW
, method_enumvaluesW
, PARAM_OUT
, &sig
);
260 VariantClear( &subkey
);
265 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
268 VariantClear( &subkey
);
269 IWbemClassObject_Release( sig
);
273 VariantInit( &names
);
274 VariantInit( &types
);
275 hr
= enum_values( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), &names
, &types
, &retval
);
276 if (hr
!= S_OK
) goto done
;
279 if (!V_UI4( &retval
))
281 hr
= IWbemClassObject_Put( out_params
, param_namesW
, 0, &names
, CIM_STRING
|CIM_FLAG_ARRAY
);
282 if (hr
!= S_OK
) goto done
;
283 hr
= IWbemClassObject_Put( out_params
, param_typesW
, 0, &types
, CIM_SINT32
|CIM_FLAG_ARRAY
);
284 if (hr
!= S_OK
) goto done
;
286 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
290 VariantClear( &types
);
291 VariantClear( &names
);
292 VariantClear( &subkey
);
293 IWbemClassObject_Release( sig
);
294 if (hr
== S_OK
&& out
)
297 IWbemClassObject_AddRef( out_params
);
299 if (out_params
) IWbemClassObject_Release( out_params
);
303 static HRESULT
get_stringvalue( HKEY root
, const WCHAR
*subkey
, const WCHAR
*name
, VARIANT
*value
, VARIANT
*retval
)
310 TRACE("%p, %s, %s\n", root
, debugstr_w(subkey
), debugstr_w(name
));
312 if ((res
= RegGetValueW( root
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
))) goto done
;
313 if (!(str
= SysAllocStringLen( NULL
, size
/ sizeof(WCHAR
) - 1 )))
318 if (!(res
= RegGetValueW( root
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, str
, &size
)))
319 set_variant( VT_BSTR
, 0, str
, value
);
322 set_variant( VT_UI4
, res
, NULL
, retval
);
323 if (res
) SysFreeString( str
);
327 HRESULT
reg_get_stringvalue( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
329 VARIANT defkey
, subkey
, name
, value
, retval
;
330 IWbemClassObject
*sig
, *out_params
= NULL
;
333 TRACE("%p, %p\n", in
, out
);
335 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
336 if (hr
!= S_OK
) return hr
;
337 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
338 if (hr
!= S_OK
) return hr
;
339 hr
= IWbemClassObject_Get( in
, param_valuenameW
, 0, &name
, NULL
, NULL
);
340 if (hr
!= S_OK
) return hr
;
342 hr
= create_signature( class_stdregprovW
, method_getstringvalueW
, PARAM_OUT
, &sig
);
345 VariantClear( &name
);
346 VariantClear( &subkey
);
351 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
354 VariantClear( &name
);
355 VariantClear( &subkey
);
356 IWbemClassObject_Release( sig
);
360 VariantInit( &value
);
361 hr
= get_stringvalue( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), V_BSTR(&name
), &value
, &retval
);
362 if (hr
!= S_OK
) goto done
;
365 if (!V_UI4( &retval
))
367 hr
= IWbemClassObject_Put( out_params
, param_valueW
, 0, &value
, CIM_STRING
);
368 if (hr
!= S_OK
) goto done
;
370 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
374 VariantClear( &name
);
375 VariantClear( &subkey
);
376 IWbemClassObject_Release( sig
);
377 if (hr
== S_OK
&& out
)
380 IWbemClassObject_AddRef( out_params
);
382 if (out_params
) IWbemClassObject_Release( out_params
);