secur32/tests: Use importlib for functions available since Windows XP.
[wine.git] / dlls / wbemprox / class.c
blob27229bd69a2396f0cc7f8699a974413eb0e9017e
1 /*
2 * Copyright 2012 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #define COBJMACROS
21 #include "config.h"
22 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "objbase.h"
27 #include "wbemcli.h"
29 #include "wine/debug.h"
30 #include "wbemprox_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wbemprox);
34 struct enum_class_object
36 IEnumWbemClassObject IEnumWbemClassObject_iface;
37 LONG refs;
38 struct query *query;
39 UINT index;
42 static inline struct enum_class_object *impl_from_IEnumWbemClassObject(
43 IEnumWbemClassObject *iface )
45 return CONTAINING_RECORD(iface, struct enum_class_object, IEnumWbemClassObject_iface);
48 static ULONG WINAPI enum_class_object_AddRef(
49 IEnumWbemClassObject *iface )
51 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( iface );
52 return InterlockedIncrement( &ec->refs );
55 static ULONG WINAPI enum_class_object_Release(
56 IEnumWbemClassObject *iface )
58 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( iface );
59 LONG refs = InterlockedDecrement( &ec->refs );
60 if (!refs)
62 TRACE("destroying %p\n", ec);
63 release_query( ec->query );
64 heap_free( ec );
66 return refs;
69 static HRESULT WINAPI enum_class_object_QueryInterface(
70 IEnumWbemClassObject *iface,
71 REFIID riid,
72 void **ppvObject )
74 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( iface );
76 TRACE("%p, %s, %p\n", ec, debugstr_guid( riid ), ppvObject );
78 if ( IsEqualGUID( riid, &IID_IEnumWbemClassObject ) ||
79 IsEqualGUID( riid, &IID_IUnknown ) )
81 *ppvObject = ec;
83 else if ( IsEqualGUID( riid, &IID_IClientSecurity ) )
85 *ppvObject = &client_security;
86 return S_OK;
88 else
90 FIXME("interface %s not implemented\n", debugstr_guid(riid));
91 return E_NOINTERFACE;
93 IEnumWbemClassObject_AddRef( iface );
94 return S_OK;
97 static HRESULT WINAPI enum_class_object_Reset(
98 IEnumWbemClassObject *iface )
100 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( iface );
102 TRACE("%p\n", iface);
104 ec->index = 0;
105 return WBEM_S_NO_ERROR;
108 static HRESULT WINAPI enum_class_object_Next(
109 IEnumWbemClassObject *iface,
110 LONG lTimeout,
111 ULONG uCount,
112 IWbemClassObject **apObjects,
113 ULONG *puReturned )
115 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( iface );
116 struct view *view = ec->query->view;
117 static int once = 0;
118 HRESULT hr;
120 TRACE("%p, %d, %u, %p, %p\n", iface, lTimeout, uCount, apObjects, puReturned);
122 if (!uCount) return WBEM_S_FALSE;
123 if (!apObjects || !puReturned) return WBEM_E_INVALID_PARAMETER;
124 if (lTimeout != WBEM_INFINITE && !once++) FIXME("timeout not supported\n");
126 *puReturned = 0;
127 if (ec->index >= view->count) return WBEM_S_FALSE;
129 hr = create_class_object( view->table->name, iface, ec->index, NULL, apObjects );
130 if (hr != S_OK) return hr;
132 ec->index++;
133 *puReturned = 1;
134 if (ec->index == view->count && uCount > 1) return WBEM_S_FALSE;
135 if (uCount > 1) return WBEM_S_TIMEDOUT;
136 return WBEM_S_NO_ERROR;
139 static HRESULT WINAPI enum_class_object_NextAsync(
140 IEnumWbemClassObject *iface,
141 ULONG uCount,
142 IWbemObjectSink *pSink )
144 FIXME("%p, %u, %p\n", iface, uCount, pSink);
145 return E_NOTIMPL;
148 static HRESULT WINAPI enum_class_object_Clone(
149 IEnumWbemClassObject *iface,
150 IEnumWbemClassObject **ppEnum )
152 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( iface );
154 TRACE("%p, %p\n", iface, ppEnum);
156 return EnumWbemClassObject_create( ec->query, (void **)ppEnum );
159 static HRESULT WINAPI enum_class_object_Skip(
160 IEnumWbemClassObject *iface,
161 LONG lTimeout,
162 ULONG nCount )
164 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( iface );
165 struct view *view = ec->query->view;
166 static int once = 0;
168 TRACE("%p, %d, %u\n", iface, lTimeout, nCount);
170 if (lTimeout != WBEM_INFINITE && !once++) FIXME("timeout not supported\n");
172 if (!view->count) return WBEM_S_FALSE;
174 if (nCount > view->count - ec->index)
176 ec->index = view->count - 1;
177 return WBEM_S_FALSE;
179 ec->index += nCount;
180 return WBEM_S_NO_ERROR;
183 static const IEnumWbemClassObjectVtbl enum_class_object_vtbl =
185 enum_class_object_QueryInterface,
186 enum_class_object_AddRef,
187 enum_class_object_Release,
188 enum_class_object_Reset,
189 enum_class_object_Next,
190 enum_class_object_NextAsync,
191 enum_class_object_Clone,
192 enum_class_object_Skip
195 HRESULT EnumWbemClassObject_create( struct query *query, LPVOID *ppObj )
197 struct enum_class_object *ec;
199 TRACE("%p\n", ppObj);
201 ec = heap_alloc( sizeof(*ec) );
202 if (!ec) return E_OUTOFMEMORY;
204 ec->IEnumWbemClassObject_iface.lpVtbl = &enum_class_object_vtbl;
205 ec->refs = 1;
206 ec->query = addref_query( query );
207 ec->index = 0;
209 *ppObj = &ec->IEnumWbemClassObject_iface;
211 TRACE("returning iface %p\n", *ppObj);
212 return S_OK;
215 static struct record *create_record( struct table *table )
217 UINT i;
218 struct record *record;
220 if (!(record = heap_alloc( sizeof(struct record) ))) return NULL;
221 if (!(record->fields = heap_alloc( table->num_cols * sizeof(struct field) )))
223 heap_free( record );
224 return NULL;
226 for (i = 0; i < table->num_cols; i++)
228 record->fields[i].type = table->columns[i].type;
229 record->fields[i].vartype = table->columns[i].vartype;
230 record->fields[i].u.ival = 0;
232 record->count = table->num_cols;
233 record->table = addref_table( table );
234 return record;
237 void destroy_array( struct array *array, CIMTYPE type )
239 UINT i, size;
241 if (!array) return;
242 if (type == CIM_STRING || type == CIM_DATETIME)
244 size = get_type_size( type );
245 for (i = 0; i < array->count; i++) heap_free( *(WCHAR **)((char *)array->ptr + i * size) );
247 heap_free( array->ptr );
248 heap_free( array );
251 static void destroy_record( struct record *record )
253 UINT i;
255 if (!record) return;
256 release_table( record->table );
257 for (i = 0; i < record->count; i++)
259 if (record->fields[i].type == CIM_STRING || record->fields[i].type == CIM_DATETIME)
260 heap_free( record->fields[i].u.sval );
261 else if (record->fields[i].type & CIM_FLAG_ARRAY)
262 destroy_array( record->fields[i].u.aval, record->fields[i].type & CIM_TYPE_MASK );
264 heap_free( record->fields );
265 heap_free( record );
268 struct class_object
270 IWbemClassObject IWbemClassObject_iface;
271 LONG refs;
272 WCHAR *name;
273 IEnumWbemClassObject *iter;
274 UINT index;
275 UINT index_method;
276 UINT index_property;
277 struct record *record; /* uncommitted instance */
280 static inline struct class_object *impl_from_IWbemClassObject(
281 IWbemClassObject *iface )
283 return CONTAINING_RECORD(iface, struct class_object, IWbemClassObject_iface);
286 static ULONG WINAPI class_object_AddRef(
287 IWbemClassObject *iface )
289 struct class_object *co = impl_from_IWbemClassObject( iface );
290 return InterlockedIncrement( &co->refs );
293 static ULONG WINAPI class_object_Release(
294 IWbemClassObject *iface )
296 struct class_object *co = impl_from_IWbemClassObject( iface );
297 LONG refs = InterlockedDecrement( &co->refs );
298 if (!refs)
300 TRACE("destroying %p\n", co);
301 if (co->iter) IEnumWbemClassObject_Release( co->iter );
302 destroy_record( co->record );
303 heap_free( co->name );
304 heap_free( co );
306 return refs;
309 static HRESULT WINAPI class_object_QueryInterface(
310 IWbemClassObject *iface,
311 REFIID riid,
312 void **ppvObject )
314 struct class_object *co = impl_from_IWbemClassObject( iface );
316 TRACE("%p, %s, %p\n", co, debugstr_guid( riid ), ppvObject );
318 if ( IsEqualGUID( riid, &IID_IWbemClassObject ) ||
319 IsEqualGUID( riid, &IID_IUnknown ) )
321 *ppvObject = co;
323 else if (IsEqualGUID( riid, &IID_IClientSecurity ))
325 *ppvObject = &client_security;
326 return S_OK;
328 else
330 FIXME("interface %s not implemented\n", debugstr_guid(riid));
331 return E_NOINTERFACE;
333 IWbemClassObject_AddRef( iface );
334 return S_OK;
337 static HRESULT WINAPI class_object_GetQualifierSet(
338 IWbemClassObject *iface,
339 IWbemQualifierSet **ppQualSet )
341 FIXME("%p, %p\n", iface, ppQualSet);
342 return E_NOTIMPL;
345 static HRESULT record_get_value( const struct record *record, UINT index, VARIANT *var, CIMTYPE *type )
347 VARTYPE vartype = record->fields[index].vartype;
349 if (type) *type = record->fields[index].type;
351 if (record->fields[index].type & CIM_FLAG_ARRAY)
353 V_VT( var ) = vartype ? vartype : to_vartype( record->fields[index].type & CIM_TYPE_MASK ) | VT_ARRAY;
354 V_ARRAY( var ) = to_safearray( record->fields[index].u.aval, record->fields[index].type & CIM_TYPE_MASK );
355 return S_OK;
357 switch (record->fields[index].type)
359 case CIM_STRING:
360 case CIM_DATETIME:
361 if (!vartype) vartype = VT_BSTR;
362 V_BSTR( var ) = SysAllocString( record->fields[index].u.sval );
363 break;
364 case CIM_SINT32:
365 if (!vartype) vartype = VT_I4;
366 V_I4( var ) = record->fields[index].u.ival;
367 break;
368 case CIM_UINT32:
369 if (!vartype) vartype = VT_UI4;
370 V_UI4( var ) = record->fields[index].u.ival;
371 break;
372 default:
373 FIXME("unhandled type %u\n", record->fields[index].type);
374 return WBEM_E_INVALID_PARAMETER;
376 V_VT( var ) = vartype;
377 return S_OK;
380 static HRESULT WINAPI class_object_Get(
381 IWbemClassObject *iface,
382 LPCWSTR wszName,
383 LONG lFlags,
384 VARIANT *pVal,
385 CIMTYPE *pType,
386 LONG *plFlavor )
388 struct class_object *co = impl_from_IWbemClassObject( iface );
389 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( co->iter );
391 TRACE("%p, %s, %08x, %p, %p, %p\n", iface, debugstr_w(wszName), lFlags, pVal, pType, plFlavor);
393 if (co->record)
395 UINT index;
396 HRESULT hr;
398 if ((hr = get_column_index( co->record->table, wszName, &index )) != S_OK) return hr;
399 return record_get_value( co->record, index, pVal, pType );
401 return get_propval( ec->query->view, co->index, wszName, pVal, pType, plFlavor );
404 static HRESULT record_set_value( struct record *record, UINT index, VARIANT *var )
406 LONGLONG val;
407 CIMTYPE type;
408 HRESULT hr;
410 if ((hr = to_longlong( var, &val, &type )) != S_OK) return hr;
411 if (type != record->fields[index].type) return WBEM_E_TYPE_MISMATCH;
413 if (type & CIM_FLAG_ARRAY)
415 record->fields[index].u.aval = (struct array *)(INT_PTR)val;
416 return S_OK;
418 switch (type)
420 case CIM_STRING:
421 case CIM_DATETIME:
422 record->fields[index].u.sval = (WCHAR *)(INT_PTR)val;
423 return S_OK;
424 case CIM_SINT16:
425 case CIM_UINT16:
426 case CIM_SINT32:
427 case CIM_UINT32:
428 record->fields[index].u.ival = val;
429 return S_OK;
430 default:
431 FIXME("unhandled type %u\n", type);
432 break;
434 return WBEM_E_INVALID_PARAMETER;
437 static HRESULT WINAPI class_object_Put(
438 IWbemClassObject *iface,
439 LPCWSTR wszName,
440 LONG lFlags,
441 VARIANT *pVal,
442 CIMTYPE Type )
444 struct class_object *co = impl_from_IWbemClassObject( iface );
445 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( co->iter );
447 TRACE("%p, %s, %08x, %p, %u\n", iface, debugstr_w(wszName), lFlags, pVal, Type);
449 if (co->record)
451 UINT index;
452 HRESULT hr;
454 if ((hr = get_column_index( co->record->table, wszName, &index )) != S_OK) return hr;
455 return record_set_value( co->record, index, pVal );
457 return put_propval( ec->query->view, co->index, wszName, pVal, Type );
460 static HRESULT WINAPI class_object_Delete(
461 IWbemClassObject *iface,
462 LPCWSTR wszName )
464 FIXME("%p, %s\n", iface, debugstr_w(wszName));
465 return E_NOTIMPL;
468 static HRESULT WINAPI class_object_GetNames(
469 IWbemClassObject *iface,
470 LPCWSTR wszQualifierName,
471 LONG lFlags,
472 VARIANT *pQualifierVal,
473 SAFEARRAY **pNames )
475 struct class_object *co = impl_from_IWbemClassObject( iface );
476 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( co->iter );
478 TRACE("%p, %s, %08x, %s, %p\n", iface, debugstr_w(wszQualifierName), lFlags,
479 debugstr_variant(pQualifierVal), pNames);
481 if (lFlags != WBEM_FLAG_ALWAYS &&
482 lFlags != WBEM_FLAG_NONSYSTEM_ONLY &&
483 lFlags != WBEM_FLAG_SYSTEM_ONLY)
485 FIXME("flags %08x not supported\n", lFlags);
486 return E_NOTIMPL;
488 if (wszQualifierName || pQualifierVal)
489 FIXME("qualifier not supported\n");
491 return get_properties( ec->query->view, lFlags, pNames );
494 static HRESULT WINAPI class_object_BeginEnumeration(
495 IWbemClassObject *iface,
496 LONG lEnumFlags )
498 struct class_object *co = impl_from_IWbemClassObject( iface );
500 TRACE("%p, %08x\n", iface, lEnumFlags);
502 if (lEnumFlags) FIXME("flags 0x%08x not supported\n", lEnumFlags);
504 co->index_property = 0;
505 return S_OK;
508 static HRESULT WINAPI class_object_Next(
509 IWbemClassObject *iface,
510 LONG lFlags,
511 BSTR *strName,
512 VARIANT *pVal,
513 CIMTYPE *pType,
514 LONG *plFlavor )
516 struct class_object *obj = impl_from_IWbemClassObject( iface );
517 struct enum_class_object *iter = impl_from_IEnumWbemClassObject( obj->iter );
518 struct view *view = iter->query->view;
519 BSTR prop;
520 HRESULT hr;
521 UINT i;
523 TRACE("%p, %08x, %p, %p, %p, %p\n", iface, lFlags, strName, pVal, pType, plFlavor);
525 for (i = obj->index_property; i < view->table->num_cols; i++)
527 if (is_method( view->table, i )) continue;
528 if (!is_selected_prop( view, view->table->columns[i].name )) continue;
529 if (!(prop = SysAllocString( view->table->columns[i].name ))) return E_OUTOFMEMORY;
530 if ((hr = get_propval( view, obj->index, prop, pVal, pType, plFlavor )) != S_OK)
532 SysFreeString( prop );
533 return hr;
535 obj->index_property = i + 1;
536 *strName = prop;
537 return S_OK;
539 return WBEM_S_NO_MORE_DATA;
542 static HRESULT WINAPI class_object_EndEnumeration(
543 IWbemClassObject *iface )
545 struct class_object *co = impl_from_IWbemClassObject( iface );
547 TRACE("%p\n", iface);
549 co->index_property = 0;
550 return S_OK;
553 static HRESULT WINAPI class_object_GetPropertyQualifierSet(
554 IWbemClassObject *iface,
555 LPCWSTR wszProperty,
556 IWbemQualifierSet **ppQualSet )
558 struct class_object *co = impl_from_IWbemClassObject( iface );
560 TRACE("%p, %s, %p\n", iface, debugstr_w(wszProperty), ppQualSet);
562 return WbemQualifierSet_create( co->name, wszProperty, (void **)ppQualSet );
565 static HRESULT WINAPI class_object_Clone(
566 IWbemClassObject *iface,
567 IWbemClassObject **ppCopy )
569 FIXME("%p, %p\n", iface, ppCopy);
570 return E_NOTIMPL;
573 static BSTR get_body_text( const struct table *table, UINT row, UINT *len )
575 static const WCHAR fmtW[] = {'\n','\t','%','s',' ','=',' ','%','s',';',0};
576 BSTR value, ret;
577 WCHAR *p;
578 UINT i;
580 *len = 0;
581 for (i = 0; i < table->num_cols; i++)
583 if ((value = get_value_bstr( table, row, i )))
585 *len += sizeof(fmtW) / sizeof(fmtW[0]);
586 *len += strlenW( table->columns[i].name );
587 *len += SysStringLen( value );
588 SysFreeString( value );
591 if (!(ret = SysAllocStringLen( NULL, *len ))) return NULL;
592 p = ret;
593 for (i = 0; i < table->num_cols; i++)
595 if ((value = get_value_bstr( table, row, i )))
597 p += sprintfW( p, fmtW, table->columns[i].name, value );
598 SysFreeString( value );
601 return ret;
604 static BSTR get_object_text( const struct view *view, UINT index )
606 static const WCHAR fmtW[] =
607 {'\n','i','n','s','t','a','n','c','e',' ','o','f',' ','%','s','\n','{','%','s','\n','}',';',0};
608 UINT len, len_body, row = view->result[index];
609 BSTR ret, body;
611 len = sizeof(fmtW) / sizeof(fmtW[0]);
612 len += strlenW( view->table->name );
613 if (!(body = get_body_text( view->table, row, &len_body ))) return NULL;
614 len += len_body;
616 if (!(ret = SysAllocStringLen( NULL, len ))) return NULL;
617 sprintfW( ret, fmtW, view->table->name, body );
618 SysFreeString( body );
619 return ret;
622 static HRESULT WINAPI class_object_GetObjectText(
623 IWbemClassObject *iface,
624 LONG lFlags,
625 BSTR *pstrObjectText )
627 struct class_object *co = impl_from_IWbemClassObject( iface );
628 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( co->iter );
629 struct view *view = ec->query->view;
630 BSTR text;
632 TRACE("%p, %08x, %p\n", iface, lFlags, pstrObjectText);
634 if (lFlags) FIXME("flags %08x not implemented\n", lFlags);
636 if (!(text = get_object_text( view, co->index ))) return E_OUTOFMEMORY;
637 *pstrObjectText = text;
638 return S_OK;
641 static HRESULT WINAPI class_object_SpawnDerivedClass(
642 IWbemClassObject *iface,
643 LONG lFlags,
644 IWbemClassObject **ppNewClass )
646 FIXME("%p, %08x, %p\n", iface, lFlags, ppNewClass);
647 return E_NOTIMPL;
650 static HRESULT WINAPI class_object_SpawnInstance(
651 IWbemClassObject *iface,
652 LONG lFlags,
653 IWbemClassObject **ppNewInstance )
655 struct class_object *co = impl_from_IWbemClassObject( iface );
656 struct enum_class_object *ec = impl_from_IEnumWbemClassObject( co->iter );
657 struct view *view = ec->query->view;
658 struct record *record;
660 TRACE("%p, %08x, %p\n", iface, lFlags, ppNewInstance);
662 if (!(record = create_record( view->table ))) return E_OUTOFMEMORY;
664 return create_class_object( co->name, NULL, 0, record, ppNewInstance );
667 static HRESULT WINAPI class_object_CompareTo(
668 IWbemClassObject *iface,
669 LONG lFlags,
670 IWbemClassObject *pCompareTo )
672 FIXME("%p, %08x, %p\n", iface, lFlags, pCompareTo);
673 return E_NOTIMPL;
676 static HRESULT WINAPI class_object_GetPropertyOrigin(
677 IWbemClassObject *iface,
678 LPCWSTR wszName,
679 BSTR *pstrClassName )
681 FIXME("%p, %s, %p\n", iface, debugstr_w(wszName), pstrClassName);
682 return E_NOTIMPL;
685 static HRESULT WINAPI class_object_InheritsFrom(
686 IWbemClassObject *iface,
687 LPCWSTR strAncestor )
689 FIXME("%p, %s\n", iface, debugstr_w(strAncestor));
690 return E_NOTIMPL;
693 static UINT count_instances( IEnumWbemClassObject *iter )
695 UINT count = 0;
696 while (!IEnumWbemClassObject_Skip( iter, WBEM_INFINITE, 1 )) count++;
697 IEnumWbemClassObject_Reset( iter );
698 return count;
701 static void set_default_value( CIMTYPE type, UINT val, BYTE *ptr )
703 switch (type)
705 case CIM_SINT16:
706 *(INT16 *)ptr = val;
707 break;
708 case CIM_UINT16:
709 *(UINT16 *)ptr = val;
710 break;
711 case CIM_SINT32:
712 *(INT32 *)ptr = val;
713 break;
714 case CIM_UINT32:
715 *(UINT32 *)ptr = val;
716 break;
717 default:
718 FIXME("unhandled type %u\n", type);
719 break;
723 static HRESULT create_signature_columns_and_data( IEnumWbemClassObject *iter, UINT *num_cols,
724 struct column **cols, BYTE **data )
726 static const WCHAR parameterW[] = {'P','a','r','a','m','e','t','e','r',0};
727 static const WCHAR typeW[] = {'T','y','p','e',0};
728 static const WCHAR varianttypeW[] = {'V','a','r','i','a','n','t','T','y','p','e',0};
729 static const WCHAR defaultvalueW[] = {'D','e','f','a','u','l','t','V','a','l','u','e',0};
730 struct column *columns;
731 BYTE *row;
732 IWbemClassObject *param;
733 VARIANT val;
734 HRESULT hr = E_OUTOFMEMORY;
735 UINT offset = 0;
736 ULONG count;
737 int i = 0;
739 count = count_instances( iter );
740 if (!(columns = heap_alloc( count * sizeof(struct column) ))) return E_OUTOFMEMORY;
741 if (!(row = heap_alloc_zero( count * sizeof(LONGLONG) ))) goto error;
743 for (;;)
745 IEnumWbemClassObject_Next( iter, WBEM_INFINITE, 1, &param, &count );
746 if (!count) break;
748 hr = IWbemClassObject_Get( param, parameterW, 0, &val, NULL, NULL );
749 if (hr != S_OK) goto error;
750 columns[i].name = heap_strdupW( V_BSTR( &val ) );
751 VariantClear( &val );
753 hr = IWbemClassObject_Get( param, typeW, 0, &val, NULL, NULL );
754 if (hr != S_OK) goto error;
755 columns[i].type = V_UI4( &val );
757 hr = IWbemClassObject_Get( param, varianttypeW, 0, &val, NULL, NULL );
758 if (hr != S_OK) goto error;
759 columns[i].vartype = V_UI4( &val );
761 hr = IWbemClassObject_Get( param, defaultvalueW, 0, &val, NULL, NULL );
762 if (hr != S_OK) goto error;
763 if (V_UI4( &val )) set_default_value( columns[i].type, V_UI4( &val ), row + offset );
764 offset += get_type_size( columns[i].type );
766 IWbemClassObject_Release( param );
767 i++;
769 *num_cols = i;
770 *cols = columns;
771 *data = row;
772 return S_OK;
774 error:
775 for (; i >= 0; i--) heap_free( (WCHAR *)columns[i].name );
776 heap_free( columns );
777 heap_free( row );
778 return hr;
781 static HRESULT create_signature_table( IEnumWbemClassObject *iter, WCHAR *name )
783 HRESULT hr;
784 struct table *table;
785 struct column *columns;
786 UINT num_cols;
787 BYTE *row;
789 hr = create_signature_columns_and_data( iter, &num_cols, &columns, &row );
790 if (hr != S_OK) return hr;
792 if (!(table = create_table( name, num_cols, columns, 1, 1, row, NULL )))
794 free_columns( columns, num_cols );
795 heap_free( row );
796 return E_OUTOFMEMORY;
798 if (!add_table( table )) free_table( table ); /* already exists */
799 return S_OK;
802 static WCHAR *build_signature_table_name( const WCHAR *class, const WCHAR *method, enum param_direction dir )
804 static const WCHAR fmtW[] = {'_','_','%','s','_','%','s','_','%','s',0};
805 static const WCHAR outW[] = {'O','U','T',0};
806 static const WCHAR inW[] = {'I','N',0};
807 UINT len = SIZEOF(fmtW) + SIZEOF(outW) + strlenW( class ) + strlenW( method );
808 WCHAR *ret;
810 if (!(ret = heap_alloc( len * sizeof(WCHAR) ))) return NULL;
811 sprintfW( ret, fmtW, class, method, dir == PARAM_IN ? inW : outW );
812 return struprW( ret );
815 HRESULT create_signature( const WCHAR *class, const WCHAR *method, enum param_direction dir,
816 IWbemClassObject **sig )
818 static const WCHAR selectW[] =
819 {'S','E','L','E','C','T',' ','*',' ','F','R','O','M',' ',
820 '_','_','P','A','R','A','M','E','T','E','R','S',' ','W','H','E','R','E',' ',
821 'C','l','a','s','s','=','\'','%','s','\'',' ','A','N','D',' ',
822 'M','e','t','h','o','d','=','\'','%','s','\'',' ','A','N','D',' ',
823 'D','i','r','e','c','t','i','o','n','%','s',0};
824 static const WCHAR geW[] = {'>','=','0',0};
825 static const WCHAR leW[] = {'<','=','0',0};
826 UINT len = SIZEOF(selectW) + SIZEOF(geW);
827 IEnumWbemClassObject *iter;
828 WCHAR *query, *name;
829 HRESULT hr;
831 len += strlenW( class ) + strlenW( method );
832 if (!(query = heap_alloc( len * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
833 sprintfW( query, selectW, class, method, dir >= 0 ? geW : leW );
835 hr = exec_query( query, &iter );
836 heap_free( query );
837 if (hr != S_OK) return hr;
839 if (!(name = build_signature_table_name( class, method, dir )))
841 IEnumWbemClassObject_Release( iter );
842 return E_OUTOFMEMORY;
844 hr = create_signature_table( iter, name );
845 IEnumWbemClassObject_Release( iter );
846 if (hr == S_OK)
847 hr = get_object( name, sig );
849 heap_free( name );
850 return hr;
853 static HRESULT WINAPI class_object_GetMethod(
854 IWbemClassObject *iface,
855 LPCWSTR wszName,
856 LONG lFlags,
857 IWbemClassObject **ppInSignature,
858 IWbemClassObject **ppOutSignature )
860 struct class_object *co = impl_from_IWbemClassObject( iface );
861 IWbemClassObject *in, *out;
862 HRESULT hr;
864 TRACE("%p, %s, %08x, %p, %p\n", iface, debugstr_w(wszName), lFlags, ppInSignature, ppOutSignature);
866 hr = create_signature( co->name, wszName, PARAM_IN, &in );
867 if (hr != S_OK) return hr;
869 hr = create_signature( co->name, wszName, PARAM_OUT, &out );
870 if (hr == S_OK)
872 if (ppInSignature) *ppInSignature = in;
873 else IWbemClassObject_Release( in );
874 if (ppOutSignature) *ppOutSignature = out;
875 else IWbemClassObject_Release( out );
877 else IWbemClassObject_Release( in );
878 return hr;
881 static HRESULT WINAPI class_object_PutMethod(
882 IWbemClassObject *iface,
883 LPCWSTR wszName,
884 LONG lFlags,
885 IWbemClassObject *pInSignature,
886 IWbemClassObject *pOutSignature )
888 FIXME("%p, %s, %08x, %p, %p\n", iface, debugstr_w(wszName), lFlags, pInSignature, pOutSignature);
889 return E_NOTIMPL;
892 static HRESULT WINAPI class_object_DeleteMethod(
893 IWbemClassObject *iface,
894 LPCWSTR wszName )
896 FIXME("%p, %s\n", iface, debugstr_w(wszName));
897 return E_NOTIMPL;
900 static HRESULT WINAPI class_object_BeginMethodEnumeration(
901 IWbemClassObject *iface,
902 LONG lEnumFlags)
904 struct class_object *co = impl_from_IWbemClassObject( iface );
906 TRACE("%p, %08x\n", iface, lEnumFlags);
908 if (lEnumFlags) FIXME("flags 0x%08x not supported\n", lEnumFlags);
910 if (co->iter)
912 WARN("not allowed on instance\n");
913 return WBEM_E_ILLEGAL_OPERATION;
915 co->index_method = 0;
916 return S_OK;
919 static HRESULT WINAPI class_object_NextMethod(
920 IWbemClassObject *iface,
921 LONG lFlags,
922 BSTR *pstrName,
923 IWbemClassObject **ppInSignature,
924 IWbemClassObject **ppOutSignature)
926 struct class_object *co = impl_from_IWbemClassObject( iface );
927 BSTR method;
928 HRESULT hr;
930 TRACE("%p, %08x, %p, %p, %p\n", iface, lFlags, pstrName, ppInSignature, ppOutSignature);
932 if (!(method = get_method_name( co->name, co->index_method ))) return WBEM_S_NO_MORE_DATA;
934 hr = create_signature( co->name, method, PARAM_IN, ppInSignature );
935 if (hr != S_OK)
937 SysFreeString( method );
938 return hr;
940 hr = create_signature( co->name, method, PARAM_OUT, ppOutSignature );
941 if (hr != S_OK)
943 SysFreeString( method );
944 IWbemClassObject_Release( *ppInSignature );
946 else
948 *pstrName = method;
949 co->index_method++;
951 return hr;
954 static HRESULT WINAPI class_object_EndMethodEnumeration(
955 IWbemClassObject *iface )
957 struct class_object *co = impl_from_IWbemClassObject( iface );
959 TRACE("%p\n", iface);
961 co->index_method = 0;
962 return S_OK;
965 static HRESULT WINAPI class_object_GetMethodQualifierSet(
966 IWbemClassObject *iface,
967 LPCWSTR wszMethod,
968 IWbemQualifierSet **ppQualSet)
970 FIXME("%p, %s, %p\n", iface, debugstr_w(wszMethod), ppQualSet);
971 return E_NOTIMPL;
974 static HRESULT WINAPI class_object_GetMethodOrigin(
975 IWbemClassObject *iface,
976 LPCWSTR wszMethodName,
977 BSTR *pstrClassName)
979 FIXME("%p, %s, %p\n", iface, debugstr_w(wszMethodName), pstrClassName);
980 return E_NOTIMPL;
983 static const IWbemClassObjectVtbl class_object_vtbl =
985 class_object_QueryInterface,
986 class_object_AddRef,
987 class_object_Release,
988 class_object_GetQualifierSet,
989 class_object_Get,
990 class_object_Put,
991 class_object_Delete,
992 class_object_GetNames,
993 class_object_BeginEnumeration,
994 class_object_Next,
995 class_object_EndEnumeration,
996 class_object_GetPropertyQualifierSet,
997 class_object_Clone,
998 class_object_GetObjectText,
999 class_object_SpawnDerivedClass,
1000 class_object_SpawnInstance,
1001 class_object_CompareTo,
1002 class_object_GetPropertyOrigin,
1003 class_object_InheritsFrom,
1004 class_object_GetMethod,
1005 class_object_PutMethod,
1006 class_object_DeleteMethod,
1007 class_object_BeginMethodEnumeration,
1008 class_object_NextMethod,
1009 class_object_EndMethodEnumeration,
1010 class_object_GetMethodQualifierSet,
1011 class_object_GetMethodOrigin
1014 HRESULT create_class_object( const WCHAR *name, IEnumWbemClassObject *iter, UINT index,
1015 struct record *record, IWbemClassObject **obj )
1017 struct class_object *co;
1019 TRACE("%s, %p\n", debugstr_w(name), obj);
1021 co = heap_alloc( sizeof(*co) );
1022 if (!co) return E_OUTOFMEMORY;
1024 co->IWbemClassObject_iface.lpVtbl = &class_object_vtbl;
1025 co->refs = 1;
1026 if (!name) co->name = NULL;
1027 else if (!(co->name = heap_strdupW( name )))
1029 heap_free( co );
1030 return E_OUTOFMEMORY;
1032 co->iter = iter;
1033 co->index = index;
1034 co->index_method = 0;
1035 co->index_property = 0;
1036 co->record = record;
1037 if (iter) IEnumWbemClassObject_AddRef( iter );
1039 *obj = &co->IWbemClassObject_iface;
1041 TRACE("returning iface %p\n", *obj);
1042 return S_OK;