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 HRESULT
to_i4_array( DWORD
*values
, DWORD count
, VARIANT
*var
)
60 if (!(sa
= SafeArrayCreateVector( VT_I4
, 0, count
))) return E_OUTOFMEMORY
;
61 for (i
= 0; i
< count
; i
++)
63 if ((hr
= SafeArrayPutElement( sa
, &i
, &values
[i
] )) != S_OK
)
65 SafeArrayDestroy( sa
);
69 set_variant( VT_I4
|VT_ARRAY
, 0, sa
, var
);
73 static HRESULT
enum_key( HKEY root
, const WCHAR
*subkey
, VARIANT
*names
, VARIANT
*retval
)
79 DWORD count
= 2, len
= sizeof(buf
)/sizeof(buf
[0]);
82 TRACE("%p, %s\n", root
, debugstr_w(subkey
));
84 if (!(strings
= heap_alloc( count
* sizeof(BSTR
) ))) return E_OUTOFMEMORY
;
85 if ((res
= RegOpenKeyExW( root
, subkey
, 0, KEY_ENUMERATE_SUB_KEYS
, &hkey
)))
87 set_variant( VT_UI4
, res
, NULL
, retval
);
96 if (!(tmp
= heap_realloc( strings
, count
* sizeof(BSTR
) )))
103 if ((res
= RegEnumKeyW( hkey
, i
, buf
, len
)) == ERROR_NO_MORE_ITEMS
)
105 if (i
) res
= ERROR_SUCCESS
;
109 if (!(strings
[i
] = SysAllocString( buf
)))
111 for (i
--; i
>= 0; i
--) SysFreeString( strings
[i
] );
112 hr
= ERROR_OUTOFMEMORY
;
117 if (hr
== S_OK
&& !res
) hr
= to_bstr_array( strings
, i
, names
);
118 set_variant( VT_UI4
, res
, NULL
, retval
);
120 heap_free( strings
);
124 HRESULT
reg_enum_key( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
126 VARIANT defkey
, subkey
, names
, retval
;
127 IWbemClassObject
*sig
, *out_params
= NULL
;
130 TRACE("%p, %p\n", in
, out
);
132 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
133 if (hr
!= S_OK
) return hr
;
134 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
135 if (hr
!= S_OK
) return hr
;
137 hr
= create_signature( class_stdregprovW
, method_enumkeyW
, PARAM_OUT
, &sig
);
140 VariantClear( &subkey
);
145 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
148 VariantClear( &subkey
);
149 IWbemClassObject_Release( sig
);
153 VariantInit( &names
);
154 hr
= enum_key( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), &names
, &retval
);
155 if (hr
!= S_OK
) goto done
;
158 if (!V_UI4( &retval
))
160 hr
= IWbemClassObject_Put( out_params
, param_namesW
, 0, &names
, CIM_STRING
|CIM_FLAG_ARRAY
);
161 if (hr
!= S_OK
) goto done
;
163 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
167 VariantClear( &names
);
168 VariantClear( &subkey
);
169 IWbemClassObject_Release( sig
);
170 if (hr
== S_OK
&& out
)
173 IWbemClassObject_AddRef( out_params
);
175 if (out_params
) IWbemClassObject_Release( out_params
);
179 static HRESULT
enum_values( HKEY root
, const WCHAR
*subkey
, VARIANT
*names
, VARIANT
*types
, VARIANT
*retval
)
183 BSTR
*value_names
= NULL
;
184 DWORD count
, buflen
, len
, *value_types
= NULL
;
188 TRACE("%p, %s\n", root
, debugstr_w(subkey
));
190 if ((res
= RegOpenKeyExW( root
, subkey
, 0, KEY_QUERY_VALUE
, &hkey
))) goto done
;
191 if ((res
= RegQueryInfoKeyW( hkey
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &count
, &buflen
, NULL
, NULL
, NULL
)))
195 if (!(buf
= heap_alloc( (buflen
+ 1) * sizeof(WCHAR
) ))) goto done
;
196 if (!(value_names
= heap_alloc( count
* sizeof(BSTR
) ))) goto done
;
197 if (!(value_types
= heap_alloc( count
* sizeof(DWORD
) ))) goto done
;
203 res
= RegEnumValueW( hkey
, i
, buf
, &len
, NULL
, &value_types
[i
], NULL
, NULL
);
204 if (res
== ERROR_NO_MORE_ITEMS
)
206 if (i
) res
= ERROR_SUCCESS
;
210 if (!(value_names
[i
] = SysAllocString( buf
)))
212 for (i
--; i
>= 0; i
--) SysFreeString( value_names
[i
] );
213 hr
= ERROR_OUTOFMEMORY
;
218 if (hr
== S_OK
&& !res
)
220 hr
= to_bstr_array( value_names
, i
, names
);
221 if (hr
== S_OK
) hr
= to_i4_array( value_types
, i
, types
);
225 set_variant( VT_UI4
, res
, NULL
, retval
);
227 heap_free( value_names
);
228 heap_free( value_types
);
233 HRESULT
reg_enum_values( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
235 VARIANT defkey
, subkey
, names
, types
, retval
;
236 IWbemClassObject
*sig
, *out_params
= NULL
;
239 TRACE("%p, %p\n", in
, out
);
241 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
242 if (hr
!= S_OK
) return hr
;
243 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
244 if (hr
!= S_OK
) return hr
;
246 hr
= create_signature( class_stdregprovW
, method_enumvaluesW
, PARAM_OUT
, &sig
);
249 VariantClear( &subkey
);
254 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
257 VariantClear( &subkey
);
258 IWbemClassObject_Release( sig
);
262 VariantInit( &names
);
263 VariantInit( &types
);
264 hr
= enum_values( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), &names
, &types
, &retval
);
265 if (hr
!= S_OK
) goto done
;
268 if (!V_UI4( &retval
))
270 hr
= IWbemClassObject_Put( out_params
, param_namesW
, 0, &names
, CIM_STRING
|CIM_FLAG_ARRAY
);
271 if (hr
!= S_OK
) goto done
;
272 hr
= IWbemClassObject_Put( out_params
, param_typesW
, 0, &types
, CIM_SINT32
|CIM_FLAG_ARRAY
);
273 if (hr
!= S_OK
) goto done
;
275 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
279 VariantClear( &types
);
280 VariantClear( &names
);
281 VariantClear( &subkey
);
282 IWbemClassObject_Release( sig
);
283 if (hr
== S_OK
&& out
)
286 IWbemClassObject_AddRef( out_params
);
288 if (out_params
) IWbemClassObject_Release( out_params
);
292 static HRESULT
get_stringvalue( HKEY root
, const WCHAR
*subkey
, const WCHAR
*name
, VARIANT
*value
, VARIANT
*retval
)
299 TRACE("%p, %s, %s\n", root
, debugstr_w(subkey
), debugstr_w(name
));
301 if ((res
= RegGetValueW( root
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, NULL
, &size
))) goto done
;
302 if (!(str
= SysAllocStringLen( NULL
, size
/ sizeof(WCHAR
) - 1 )))
307 if (!(res
= RegGetValueW( root
, subkey
, name
, RRF_RT_REG_SZ
, NULL
, str
, &size
)))
308 set_variant( VT_BSTR
, 0, str
, value
);
311 set_variant( VT_UI4
, res
, NULL
, retval
);
312 if (res
) SysFreeString( str
);
316 HRESULT
reg_get_stringvalue( IWbemClassObject
*obj
, IWbemClassObject
*in
, IWbemClassObject
**out
)
318 VARIANT defkey
, subkey
, name
, value
, retval
;
319 IWbemClassObject
*sig
, *out_params
= NULL
;
322 TRACE("%p, %p\n", in
, out
);
324 hr
= IWbemClassObject_Get( in
, param_defkeyW
, 0, &defkey
, NULL
, NULL
);
325 if (hr
!= S_OK
) return hr
;
326 hr
= IWbemClassObject_Get( in
, param_subkeynameW
, 0, &subkey
, NULL
, NULL
);
327 if (hr
!= S_OK
) return hr
;
328 hr
= IWbemClassObject_Get( in
, param_valuenameW
, 0, &name
, NULL
, NULL
);
329 if (hr
!= S_OK
) return hr
;
331 hr
= create_signature( class_stdregprovW
, method_getstringvalueW
, PARAM_OUT
, &sig
);
334 VariantClear( &name
);
335 VariantClear( &subkey
);
340 hr
= IWbemClassObject_SpawnInstance( sig
, 0, &out_params
);
343 VariantClear( &name
);
344 VariantClear( &subkey
);
345 IWbemClassObject_Release( sig
);
349 VariantInit( &value
);
350 hr
= get_stringvalue( (HKEY
)(INT_PTR
)V_I4(&defkey
), V_BSTR(&subkey
), V_BSTR(&name
), &value
, &retval
);
351 if (hr
!= S_OK
) goto done
;
354 if (!V_UI4( &retval
))
356 hr
= IWbemClassObject_Put( out_params
, param_valueW
, 0, &value
, CIM_STRING
);
357 if (hr
!= S_OK
) goto done
;
359 hr
= IWbemClassObject_Put( out_params
, param_returnvalueW
, 0, &retval
, CIM_UINT32
);
363 VariantClear( &name
);
364 VariantClear( &subkey
);
365 IWbemClassObject_Release( sig
);
366 if (hr
== S_OK
&& out
)
369 IWbemClassObject_AddRef( out_params
);
371 if (out_params
) IWbemClassObject_Release( out_params
);