2 * Copyright 2005 Jacek Caban
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
22 #define NONAMELESSUNION
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
44 IRecordInfo IRecordInfo_iface
;
56 static inline IRecordInfoImpl
*impl_from_IRecordInfo(IRecordInfo
*iface
)
58 return CONTAINING_RECORD(iface
, IRecordInfoImpl
, IRecordInfo_iface
);
61 static HRESULT
copy_to_variant(void *src
, VARIANT
*pvar
, enum VARENUM vt
)
63 TRACE("%p %p %d\n", src
, pvar
, vt
);
65 #define CASE_COPY(x) \
67 memcpy(&V_ ## x(pvar), src, sizeof(V_ ## x(pvar))); \
92 FIXME("Not supported type: %d\n", vt
);
101 static HRESULT
copy_from_variant(VARIANT
*src
, void *dest
, enum VARENUM vt
)
106 TRACE("(%p(%d) %p %d)\n", src
, V_VT(src
), dest
, vt
);
108 hres
= VariantChangeType(&var
, src
, 0, vt
);
112 #define CASE_COPY(x) \
114 memcpy(dest, &V_ ## x(&var), sizeof(V_ ## x(&var))); \
139 FIXME("Not supported type: %d\n", V_VT(&var
));
146 static HRESULT WINAPI
IRecordInfoImpl_QueryInterface(IRecordInfo
*iface
, REFIID riid
,
149 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
153 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IRecordInfo
, riid
)) {
155 IRecordInfo_AddRef(iface
);
159 FIXME("Not supported interface: %s\n", debugstr_guid(riid
));
160 return E_NOINTERFACE
;
163 static ULONG WINAPI
IRecordInfoImpl_AddRef(IRecordInfo
*iface
)
165 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
166 ULONG ref
= InterlockedIncrement(&This
->ref
);
167 TRACE("(%p) -> %d\n", This
, ref
);
171 static ULONG WINAPI
IRecordInfoImpl_Release(IRecordInfo
*iface
)
173 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
174 ULONG ref
= InterlockedDecrement(&This
->ref
);
176 TRACE("(%p) -> %d\n", This
, ref
);
180 for(i
=0; i
<This
->n_vars
; i
++)
181 SysFreeString(This
->fields
[i
].name
);
182 SysFreeString(This
->name
);
183 HeapFree(GetProcessHeap(), 0, This
->fields
);
184 ITypeInfo_Release(This
->pTypeInfo
);
185 HeapFree(GetProcessHeap(), 0, This
);
190 static HRESULT WINAPI
IRecordInfoImpl_RecordInit(IRecordInfo
*iface
, PVOID pvNew
)
192 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
193 TRACE("(%p)->(%p)\n", This
, pvNew
);
198 memset(pvNew
, 0, This
->size
);
202 static HRESULT WINAPI
IRecordInfoImpl_RecordClear(IRecordInfo
*iface
, PVOID pvExisting
)
204 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
208 TRACE("(%p)->(%p)\n", This
, pvExisting
);
213 for(i
=0; i
<This
->n_vars
; i
++) {
214 if(This
->fields
[i
].varkind
!= VAR_PERINSTANCE
) {
215 ERR("varkind != VAR_PERINSTANCE\n");
218 var
= ((PBYTE
)pvExisting
)+This
->fields
[i
].offset
;
219 switch(This
->fields
[i
].vt
) {
221 SysFreeString(*(BSTR
*)var
);
248 SafeArrayDestroy(var
);
253 IUnknown
*unk
= *(IUnknown
**)var
;
255 IUnknown_Release(unk
);
260 FIXME("Not supported vt = %d\n", This
->fields
[i
].vt
);
268 static HRESULT WINAPI
IRecordInfoImpl_RecordCopy(IRecordInfo
*iface
, void *src_rec
, void *dest_rec
)
270 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
274 TRACE("(%p)->(%p %p)\n", This
, src_rec
, dest_rec
);
276 if(!src_rec
|| !dest_rec
)
279 /* release already stored data */
280 IRecordInfo_RecordClear(iface
, dest_rec
);
282 for (i
= 0; i
< This
->n_vars
; i
++)
286 if (This
->fields
[i
].varkind
!= VAR_PERINSTANCE
) {
287 ERR("varkind != VAR_PERINSTANCE\n");
291 src
= ((BYTE
*)src_rec
) + This
->fields
[i
].offset
;
292 dest
= ((BYTE
*)dest_rec
) + This
->fields
[i
].offset
;
293 switch (This
->fields
[i
].vt
)
297 BSTR src_str
= *(BSTR
*)src
;
301 BSTR str
= SysAllocString(*(BSTR
*)src
);
302 if (!str
) hr
= E_OUTOFMEMORY
;
313 IUnknown
*unk
= *(IUnknown
**)src
;
314 *(IUnknown
**)dest
= unk
;
315 if (unk
) IUnknown_AddRef(unk
);
319 hr
= SafeArrayCopy(src
, dest
);
323 /* copy directly for types that don't need deep copy */
324 int len
= get_type_size(NULL
, This
->fields
[i
].vt
);
325 memcpy(dest
, src
, len
);
330 if (FAILED(hr
)) break;
334 IRecordInfo_RecordClear(iface
, dest_rec
);
339 static HRESULT WINAPI
IRecordInfoImpl_GetGuid(IRecordInfo
*iface
, GUID
*pguid
)
341 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
343 TRACE("(%p)->(%p)\n", This
, pguid
);
352 static HRESULT WINAPI
IRecordInfoImpl_GetName(IRecordInfo
*iface
, BSTR
*pbstrName
)
354 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
356 TRACE("(%p)->(%p)\n", This
, pbstrName
);
361 *pbstrName
= SysAllocString(This
->name
);
365 static HRESULT WINAPI
IRecordInfoImpl_GetSize(IRecordInfo
*iface
, ULONG
*pcbSize
)
367 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
369 TRACE("(%p)->(%p)\n", This
, pcbSize
);
374 *pcbSize
= This
->size
;
378 static HRESULT WINAPI
IRecordInfoImpl_GetTypeInfo(IRecordInfo
*iface
, ITypeInfo
**ppTypeInfo
)
380 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
382 TRACE("(%p)->(%p)\n", This
, ppTypeInfo
);
387 ITypeInfo_AddRef(This
->pTypeInfo
);
388 *ppTypeInfo
= This
->pTypeInfo
;
393 static HRESULT WINAPI
IRecordInfoImpl_GetField(IRecordInfo
*iface
, PVOID pvData
,
394 LPCOLESTR szFieldName
, VARIANT
*pvarField
)
396 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
399 TRACE("(%p)->(%p %s %p)\n", This
, pvData
, debugstr_w(szFieldName
), pvarField
);
401 if(!pvData
|| !szFieldName
|| !pvarField
)
404 for(i
=0; i
<This
->n_vars
; i
++)
405 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
407 if(i
== This
->n_vars
)
408 return TYPE_E_FIELDNOTFOUND
;
410 VariantClear(pvarField
);
411 return copy_to_variant(((PBYTE
)pvData
)+This
->fields
[i
].offset
, pvarField
,
415 static HRESULT WINAPI
IRecordInfoImpl_GetFieldNoCopy(IRecordInfo
*iface
, PVOID pvData
,
416 LPCOLESTR szFieldName
, VARIANT
*pvarField
, PVOID
*ppvDataCArray
)
418 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
421 TRACE("(%p)->(%p %s %p %p)\n", This
, pvData
, debugstr_w(szFieldName
), pvarField
, ppvDataCArray
);
423 if(!pvData
|| !szFieldName
|| !pvarField
)
426 for(i
=0; i
<This
->n_vars
; i
++)
427 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
429 if(i
== This
->n_vars
)
430 return TYPE_E_FIELDNOTFOUND
;
432 VariantClear(pvarField
);
433 V_VT(pvarField
) = VT_BYREF
|This
->fields
[i
].vt
;
434 V_BYREF(pvarField
) = ((PBYTE
)pvData
)+This
->fields
[i
].offset
;
435 *ppvDataCArray
= NULL
;
439 static HRESULT WINAPI
IRecordInfoImpl_PutField(IRecordInfo
*iface
, ULONG wFlags
, PVOID pvData
,
440 LPCOLESTR szFieldName
, VARIANT
*pvarField
)
442 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
445 TRACE("(%p)->(%08x %p %s %p)\n", This
, wFlags
, pvData
, debugstr_w(szFieldName
),
448 if(!pvData
|| !szFieldName
|| !pvarField
449 || (wFlags
!= INVOKE_PROPERTYPUTREF
&& wFlags
!= INVOKE_PROPERTYPUT
))
452 if(wFlags
== INVOKE_PROPERTYPUTREF
) {
453 FIXME("wFlag == INVOKE_PROPERTYPUTREF not supported\n");
457 for(i
=0; i
<This
->n_vars
; i
++)
458 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
460 if(i
== This
->n_vars
)
461 return TYPE_E_FIELDNOTFOUND
;
463 return copy_from_variant(pvarField
, ((PBYTE
)pvData
)+This
->fields
[i
].offset
,
467 static HRESULT WINAPI
IRecordInfoImpl_PutFieldNoCopy(IRecordInfo
*iface
, ULONG wFlags
,
468 PVOID pvData
, LPCOLESTR szFieldName
, VARIANT
*pvarField
)
470 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
473 FIXME("(%p)->(%08x %p %s %p) stub\n", This
, wFlags
, pvData
, debugstr_w(szFieldName
), pvarField
);
475 if(!pvData
|| !szFieldName
|| !pvarField
476 || (wFlags
!= INVOKE_PROPERTYPUTREF
&& wFlags
!= INVOKE_PROPERTYPUT
))
479 for(i
=0; i
<This
->n_vars
; i
++)
480 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
482 if(i
== This
->n_vars
)
483 return TYPE_E_FIELDNOTFOUND
;
488 static HRESULT WINAPI
IRecordInfoImpl_GetFieldNames(IRecordInfo
*iface
, ULONG
*pcNames
,
491 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
492 ULONG n
= This
->n_vars
, i
;
494 TRACE("(%p)->(%p %p)\n", This
, pcNames
, rgBstrNames
);
504 rgBstrNames
[i
] = SysAllocString(This
->fields
[i
].name
);
511 static BOOL WINAPI
IRecordInfoImpl_IsMatchingType(IRecordInfo
*iface
, IRecordInfo
*info2
)
513 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
516 TRACE( "(%p)->(%p)\n", This
, info2
);
518 IRecordInfo_GetGuid( info2
, &guid2
);
519 return IsEqualGUID( &This
->guid
, &guid2
);
522 static PVOID WINAPI
IRecordInfoImpl_RecordCreate(IRecordInfo
*iface
)
524 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
527 TRACE("(%p)\n", This
);
529 record
= HeapAlloc(GetProcessHeap(), 0, This
->size
);
530 IRecordInfo_RecordInit(iface
, record
);
531 TRACE("created record at %p\n", record
);
535 static HRESULT WINAPI
IRecordInfoImpl_RecordCreateCopy(IRecordInfo
*iface
, PVOID pvSource
,
538 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
540 TRACE("(%p)->(%p %p)\n", This
, pvSource
, ppvDest
);
542 if(!pvSource
|| !ppvDest
)
545 *ppvDest
= IRecordInfo_RecordCreate(iface
);
546 return IRecordInfo_RecordCopy(iface
, pvSource
, *ppvDest
);
549 static HRESULT WINAPI
IRecordInfoImpl_RecordDestroy(IRecordInfo
*iface
, PVOID pvRecord
)
551 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
554 TRACE("(%p)->(%p)\n", This
, pvRecord
);
556 hres
= IRecordInfo_RecordClear(iface
, pvRecord
);
560 if(!HeapFree(GetProcessHeap(), 0, pvRecord
))
566 static const IRecordInfoVtbl IRecordInfoImplVtbl
= {
567 IRecordInfoImpl_QueryInterface
,
568 IRecordInfoImpl_AddRef
,
569 IRecordInfoImpl_Release
,
570 IRecordInfoImpl_RecordInit
,
571 IRecordInfoImpl_RecordClear
,
572 IRecordInfoImpl_RecordCopy
,
573 IRecordInfoImpl_GetGuid
,
574 IRecordInfoImpl_GetName
,
575 IRecordInfoImpl_GetSize
,
576 IRecordInfoImpl_GetTypeInfo
,
577 IRecordInfoImpl_GetField
,
578 IRecordInfoImpl_GetFieldNoCopy
,
579 IRecordInfoImpl_PutField
,
580 IRecordInfoImpl_PutFieldNoCopy
,
581 IRecordInfoImpl_GetFieldNames
,
582 IRecordInfoImpl_IsMatchingType
,
583 IRecordInfoImpl_RecordCreate
,
584 IRecordInfoImpl_RecordCreateCopy
,
585 IRecordInfoImpl_RecordDestroy
588 /******************************************************************************
589 * GetRecordInfoFromGuids [OLEAUT32.322]
593 * Failure: E_INVALIDARG, if any argument is invalid.
595 HRESULT WINAPI
GetRecordInfoFromGuids(REFGUID rGuidTypeLib
, ULONG uVerMajor
,
596 ULONG uVerMinor
, LCID lcid
, REFGUID rGuidTypeInfo
, IRecordInfo
** ppRecInfo
)
598 ITypeInfo
*pTypeInfo
;
602 TRACE("(%p,%d,%d,%d,%s,%p)\n", rGuidTypeLib
, uVerMajor
, uVerMinor
,
603 lcid
, debugstr_guid(rGuidTypeInfo
), ppRecInfo
);
605 hres
= LoadRegTypeLib(rGuidTypeLib
, uVerMajor
, uVerMinor
, lcid
, &pTypeLib
);
607 WARN("LoadRegTypeLib failed!\n");
611 hres
= ITypeLib_GetTypeInfoOfGuid(pTypeLib
, rGuidTypeInfo
, &pTypeInfo
);
612 ITypeLib_Release(pTypeLib
);
614 WARN("GetTypeInfoOfGuid failed!\n");
618 hres
= GetRecordInfoFromTypeInfo(pTypeInfo
, ppRecInfo
);
619 ITypeInfo_Release(pTypeInfo
);
623 /******************************************************************************
624 * GetRecordInfoFromTypeInfo [OLEAUT32.332]
626 HRESULT WINAPI
GetRecordInfoFromTypeInfo(ITypeInfo
* pTI
, IRecordInfo
** ppRecInfo
) {
629 IRecordInfoImpl
*ret
;
630 ITypeInfo
*pTypeInfo
;
634 TRACE("(%p %p)\n", pTI
, ppRecInfo
);
636 if(!pTI
|| !ppRecInfo
)
639 hres
= ITypeInfo_GetTypeAttr(pTI
, &typeattr
);
640 if(FAILED(hres
) || !typeattr
) {
641 WARN("GetTypeAttr failed: %08x\n", hres
);
645 if(typeattr
->typekind
== TKIND_ALIAS
) {
646 hres
= ITypeInfo_GetRefTypeInfo(pTI
, typeattr
->tdescAlias
.u
.hreftype
, &pTypeInfo
);
647 guid
= typeattr
->guid
;
648 ITypeInfo_ReleaseTypeAttr(pTI
, typeattr
);
650 WARN("GetRefTypeInfo failed: %08x\n", hres
);
653 hres
= ITypeInfo_GetTypeAttr(pTypeInfo
, &typeattr
);
655 ITypeInfo_Release(pTypeInfo
);
656 WARN("GetTypeAttr failed for referenced type: %08x\n", hres
);
661 ITypeInfo_AddRef(pTypeInfo
);
662 guid
= typeattr
->guid
;
665 if(typeattr
->typekind
!= TKIND_RECORD
) {
666 WARN("typekind != TKIND_RECORD\n");
667 ITypeInfo_ReleaseTypeAttr(pTypeInfo
, typeattr
);
668 ITypeInfo_Release(pTypeInfo
);
672 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
));
673 ret
->IRecordInfo_iface
.lpVtbl
= &IRecordInfoImplVtbl
;
675 ret
->pTypeInfo
= pTypeInfo
;
676 ret
->n_vars
= typeattr
->cVars
;
677 ret
->size
= typeattr
->cbSizeInstance
;
678 ITypeInfo_ReleaseTypeAttr(pTypeInfo
, typeattr
);
682 /* NOTE: Windows implementation calls ITypeInfo::GetCantainingTypeLib and
683 * ITypeLib::GetLibAttr, but we currently don't need this.
686 hres
= ITypeInfo_GetDocumentation(pTypeInfo
, MEMBERID_NIL
, &ret
->name
, NULL
, NULL
, NULL
);
688 WARN("ITypeInfo::GetDocumentation failed\n");
692 ret
->fields
= HeapAlloc(GetProcessHeap(), 0, ret
->n_vars
*sizeof(fieldstr
));
693 for(i
= 0; i
<ret
->n_vars
; i
++) {
695 hres
= ITypeInfo_GetVarDesc(pTypeInfo
, i
, &vardesc
);
697 WARN("GetVarDesc failed\n");
700 ret
->fields
[i
].vt
= vardesc
->elemdescVar
.tdesc
.vt
;
701 ret
->fields
[i
].varkind
= vardesc
->varkind
;
702 ret
->fields
[i
].offset
= vardesc
->u
.oInst
;
703 hres
= ITypeInfo_GetDocumentation(pTypeInfo
, vardesc
->memid
, &ret
->fields
[i
].name
,
706 WARN("GetDocumentation failed: %08x\n", hres
);
707 TRACE("field=%s, offset=%d\n", debugstr_w(ret
->fields
[i
].name
), ret
->fields
[i
].offset
);
708 ITypeInfo_ReleaseVarDesc(pTypeInfo
, vardesc
);
711 *ppRecInfo
= &ret
->IRecordInfo_iface
;