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
23 #define NONAMELESSSTRUCT
32 #include "wine/unicode.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
45 IRecordInfo IRecordInfo_iface
;
57 static inline IRecordInfoImpl
*impl_from_IRecordInfo(IRecordInfo
*iface
)
59 return CONTAINING_RECORD(iface
, IRecordInfoImpl
, IRecordInfo_iface
);
62 static HRESULT
copy_to_variant(void *src
, VARIANT
*pvar
, enum VARENUM vt
)
64 TRACE("%p %p %d\n", src
, pvar
, vt
);
66 #define CASE_COPY(x) \
68 memcpy(&V_ ## x(pvar), src, sizeof(V_ ## x(pvar))); \
93 FIXME("Not supported type: %d\n", vt
);
102 static HRESULT
copy_from_variant(VARIANT
*src
, void *dest
, enum VARENUM vt
)
107 TRACE("(%p(%d) %p %d)\n", src
, V_VT(src
), dest
, vt
);
109 hres
= VariantChangeType(&var
, src
, 0, vt
);
113 #define CASE_COPY(x) \
115 memcpy(dest, &V_ ## x(&var), sizeof(V_ ## x(&var))); \
140 FIXME("Not supported type: %d\n", V_VT(&var
));
147 static HRESULT WINAPI
IRecordInfoImpl_QueryInterface(IRecordInfo
*iface
, REFIID riid
,
150 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
154 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IRecordInfo
, riid
)) {
156 IRecordInfo_AddRef(iface
);
160 FIXME("Not supported interface: %s\n", debugstr_guid(riid
));
161 return E_NOINTERFACE
;
164 static ULONG WINAPI
IRecordInfoImpl_AddRef(IRecordInfo
*iface
)
166 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
167 ULONG ref
= InterlockedIncrement(&This
->ref
);
168 TRACE("(%p) -> %d\n", This
, ref
);
172 static ULONG WINAPI
IRecordInfoImpl_Release(IRecordInfo
*iface
)
174 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
175 ULONG ref
= InterlockedDecrement(&This
->ref
);
177 TRACE("(%p) -> %d\n", This
, ref
);
181 for(i
=0; i
<This
->n_vars
; i
++)
182 SysFreeString(This
->fields
[i
].name
);
183 SysFreeString(This
->name
);
184 HeapFree(GetProcessHeap(), 0, This
->fields
);
185 ITypeInfo_Release(This
->pTypeInfo
);
186 HeapFree(GetProcessHeap(), 0, This
);
191 static HRESULT WINAPI
IRecordInfoImpl_RecordInit(IRecordInfo
*iface
, PVOID pvNew
)
193 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
194 TRACE("(%p)->(%p)\n", This
, pvNew
);
199 memset(pvNew
, 0, This
->size
);
203 static HRESULT WINAPI
IRecordInfoImpl_RecordClear(IRecordInfo
*iface
, PVOID pvExisting
)
205 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
209 TRACE("(%p)->(%p)\n", This
, pvExisting
);
214 for(i
=0; i
<This
->n_vars
; i
++) {
215 if(This
->fields
[i
].varkind
!= VAR_PERINSTANCE
) {
216 ERR("varkind != VAR_PERINSTANCE\n");
219 var
= ((PBYTE
)pvExisting
)+This
->fields
[i
].offset
;
220 switch(This
->fields
[i
].vt
) {
222 SysFreeString(*(BSTR
*)var
);
249 SafeArrayDestroy(var
);
254 IUnknown
*unk
= *(IUnknown
**)var
;
256 IUnknown_Release(unk
);
261 FIXME("Not supported vt = %d\n", This
->fields
[i
].vt
);
269 static HRESULT WINAPI
IRecordInfoImpl_RecordCopy(IRecordInfo
*iface
, void *src_rec
, void *dest_rec
)
271 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
275 TRACE("(%p)->(%p %p)\n", This
, src_rec
, dest_rec
);
277 if(!src_rec
|| !dest_rec
)
280 /* release already stored data */
281 IRecordInfo_RecordClear(iface
, dest_rec
);
283 for (i
= 0; i
< This
->n_vars
; i
++)
287 if (This
->fields
[i
].varkind
!= VAR_PERINSTANCE
) {
288 ERR("varkind != VAR_PERINSTANCE\n");
292 src
= ((BYTE
*)src_rec
) + This
->fields
[i
].offset
;
293 dest
= ((BYTE
*)dest_rec
) + This
->fields
[i
].offset
;
294 switch (This
->fields
[i
].vt
)
298 BSTR src_str
= *(BSTR
*)src
;
302 BSTR str
= SysAllocString(*(BSTR
*)src
);
303 if (!str
) hr
= E_OUTOFMEMORY
;
314 IUnknown
*unk
= *(IUnknown
**)src
;
315 *(IUnknown
**)dest
= unk
;
316 if (unk
) IUnknown_AddRef(unk
);
320 hr
= SafeArrayCopy(src
, dest
);
324 /* copy directly for types that don't need deep copy */
325 int len
= get_type_size(NULL
, This
->fields
[i
].vt
);
326 memcpy(dest
, src
, len
);
331 if (FAILED(hr
)) break;
335 IRecordInfo_RecordClear(iface
, dest_rec
);
340 static HRESULT WINAPI
IRecordInfoImpl_GetGuid(IRecordInfo
*iface
, GUID
*pguid
)
342 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
344 TRACE("(%p)->(%p)\n", This
, pguid
);
353 static HRESULT WINAPI
IRecordInfoImpl_GetName(IRecordInfo
*iface
, BSTR
*pbstrName
)
355 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
357 TRACE("(%p)->(%p)\n", This
, pbstrName
);
362 *pbstrName
= SysAllocString(This
->name
);
366 static HRESULT WINAPI
IRecordInfoImpl_GetSize(IRecordInfo
*iface
, ULONG
*pcbSize
)
368 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
370 TRACE("(%p)->(%p)\n", This
, pcbSize
);
375 *pcbSize
= This
->size
;
379 static HRESULT WINAPI
IRecordInfoImpl_GetTypeInfo(IRecordInfo
*iface
, ITypeInfo
**ppTypeInfo
)
381 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
383 TRACE("(%p)->(%p)\n", This
, ppTypeInfo
);
388 ITypeInfo_AddRef(This
->pTypeInfo
);
389 *ppTypeInfo
= This
->pTypeInfo
;
394 static HRESULT WINAPI
IRecordInfoImpl_GetField(IRecordInfo
*iface
, PVOID pvData
,
395 LPCOLESTR szFieldName
, VARIANT
*pvarField
)
397 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
400 TRACE("(%p)->(%p %s %p)\n", This
, pvData
, debugstr_w(szFieldName
), pvarField
);
402 if(!pvData
|| !szFieldName
|| !pvarField
)
405 for(i
=0; i
<This
->n_vars
; i
++)
406 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
408 if(i
== This
->n_vars
)
409 return TYPE_E_FIELDNOTFOUND
;
411 VariantClear(pvarField
);
412 return copy_to_variant(((PBYTE
)pvData
)+This
->fields
[i
].offset
, pvarField
,
416 static HRESULT WINAPI
IRecordInfoImpl_GetFieldNoCopy(IRecordInfo
*iface
, PVOID pvData
,
417 LPCOLESTR szFieldName
, VARIANT
*pvarField
, PVOID
*ppvDataCArray
)
419 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
422 TRACE("(%p)->(%p %s %p %p)\n", This
, pvData
, debugstr_w(szFieldName
), pvarField
, ppvDataCArray
);
424 if(!pvData
|| !szFieldName
|| !pvarField
)
427 for(i
=0; i
<This
->n_vars
; i
++)
428 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
430 if(i
== This
->n_vars
)
431 return TYPE_E_FIELDNOTFOUND
;
433 VariantClear(pvarField
);
434 V_VT(pvarField
) = VT_BYREF
|This
->fields
[i
].vt
;
435 V_BYREF(pvarField
) = ((PBYTE
)pvData
)+This
->fields
[i
].offset
;
436 *ppvDataCArray
= NULL
;
440 static HRESULT WINAPI
IRecordInfoImpl_PutField(IRecordInfo
*iface
, ULONG wFlags
, PVOID pvData
,
441 LPCOLESTR szFieldName
, VARIANT
*pvarField
)
443 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
446 TRACE("(%p)->(%08x %p %s %p)\n", This
, wFlags
, pvData
, debugstr_w(szFieldName
),
449 if(!pvData
|| !szFieldName
|| !pvarField
450 || (wFlags
!= INVOKE_PROPERTYPUTREF
&& wFlags
!= INVOKE_PROPERTYPUT
))
453 if(wFlags
== INVOKE_PROPERTYPUTREF
) {
454 FIXME("wFlag == INVOKE_PROPERTYPUTREF not supported\n");
458 for(i
=0; i
<This
->n_vars
; i
++)
459 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
461 if(i
== This
->n_vars
)
462 return TYPE_E_FIELDNOTFOUND
;
464 return copy_from_variant(pvarField
, ((PBYTE
)pvData
)+This
->fields
[i
].offset
,
468 static HRESULT WINAPI
IRecordInfoImpl_PutFieldNoCopy(IRecordInfo
*iface
, ULONG wFlags
,
469 PVOID pvData
, LPCOLESTR szFieldName
, VARIANT
*pvarField
)
471 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
474 FIXME("(%p)->(%08x %p %s %p) stub\n", This
, wFlags
, pvData
, debugstr_w(szFieldName
), pvarField
);
476 if(!pvData
|| !szFieldName
|| !pvarField
477 || (wFlags
!= INVOKE_PROPERTYPUTREF
&& wFlags
!= INVOKE_PROPERTYPUT
))
480 for(i
=0; i
<This
->n_vars
; i
++)
481 if(!strcmpW(This
->fields
[i
].name
, szFieldName
))
483 if(i
== This
->n_vars
)
484 return TYPE_E_FIELDNOTFOUND
;
489 static HRESULT WINAPI
IRecordInfoImpl_GetFieldNames(IRecordInfo
*iface
, ULONG
*pcNames
,
492 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
493 ULONG n
= This
->n_vars
, i
;
495 TRACE("(%p)->(%p %p)\n", This
, pcNames
, rgBstrNames
);
505 rgBstrNames
[i
] = SysAllocString(This
->fields
[i
].name
);
512 static BOOL WINAPI
IRecordInfoImpl_IsMatchingType(IRecordInfo
*iface
, IRecordInfo
*info2
)
514 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
517 TRACE( "(%p)->(%p)\n", This
, info2
);
519 IRecordInfo_GetGuid( info2
, &guid2
);
520 if (IsEqualGUID( &This
->guid
, &guid2
)) return TRUE
;
522 FIXME( "records have different guids (%s %s) but could still match\n",
523 debugstr_guid( &This
->guid
), debugstr_guid( &guid2
) );
528 static PVOID WINAPI
IRecordInfoImpl_RecordCreate(IRecordInfo
*iface
)
530 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
533 TRACE("(%p)\n", This
);
535 record
= HeapAlloc(GetProcessHeap(), 0, This
->size
);
536 IRecordInfo_RecordInit(iface
, record
);
537 TRACE("created record at %p\n", record
);
541 static HRESULT WINAPI
IRecordInfoImpl_RecordCreateCopy(IRecordInfo
*iface
, PVOID pvSource
,
544 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
546 TRACE("(%p)->(%p %p)\n", This
, pvSource
, ppvDest
);
548 if(!pvSource
|| !ppvDest
)
551 *ppvDest
= IRecordInfo_RecordCreate(iface
);
552 return IRecordInfo_RecordCopy(iface
, pvSource
, *ppvDest
);
555 static HRESULT WINAPI
IRecordInfoImpl_RecordDestroy(IRecordInfo
*iface
, PVOID pvRecord
)
557 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
560 TRACE("(%p)->(%p)\n", This
, pvRecord
);
562 hres
= IRecordInfo_RecordClear(iface
, pvRecord
);
566 if(!HeapFree(GetProcessHeap(), 0, pvRecord
))
572 static const IRecordInfoVtbl IRecordInfoImplVtbl
= {
573 IRecordInfoImpl_QueryInterface
,
574 IRecordInfoImpl_AddRef
,
575 IRecordInfoImpl_Release
,
576 IRecordInfoImpl_RecordInit
,
577 IRecordInfoImpl_RecordClear
,
578 IRecordInfoImpl_RecordCopy
,
579 IRecordInfoImpl_GetGuid
,
580 IRecordInfoImpl_GetName
,
581 IRecordInfoImpl_GetSize
,
582 IRecordInfoImpl_GetTypeInfo
,
583 IRecordInfoImpl_GetField
,
584 IRecordInfoImpl_GetFieldNoCopy
,
585 IRecordInfoImpl_PutField
,
586 IRecordInfoImpl_PutFieldNoCopy
,
587 IRecordInfoImpl_GetFieldNames
,
588 IRecordInfoImpl_IsMatchingType
,
589 IRecordInfoImpl_RecordCreate
,
590 IRecordInfoImpl_RecordCreateCopy
,
591 IRecordInfoImpl_RecordDestroy
594 /******************************************************************************
595 * GetRecordInfoFromGuids [OLEAUT32.322]
599 * Failure: E_INVALIDARG, if any argument is invalid.
601 HRESULT WINAPI
GetRecordInfoFromGuids(REFGUID rGuidTypeLib
, ULONG uVerMajor
,
602 ULONG uVerMinor
, LCID lcid
, REFGUID rGuidTypeInfo
, IRecordInfo
** ppRecInfo
)
604 ITypeInfo
*pTypeInfo
;
608 TRACE("(%p,%d,%d,%d,%s,%p)\n", rGuidTypeLib
, uVerMajor
, uVerMinor
,
609 lcid
, debugstr_guid(rGuidTypeInfo
), ppRecInfo
);
611 hres
= LoadRegTypeLib(rGuidTypeLib
, uVerMajor
, uVerMinor
, lcid
, &pTypeLib
);
613 WARN("LoadRegTypeLib failed!\n");
617 hres
= ITypeLib_GetTypeInfoOfGuid(pTypeLib
, rGuidTypeInfo
, &pTypeInfo
);
618 ITypeLib_Release(pTypeLib
);
620 WARN("GetTypeInfoOfGuid failed!\n");
624 hres
= GetRecordInfoFromTypeInfo(pTypeInfo
, ppRecInfo
);
625 ITypeInfo_Release(pTypeInfo
);
629 /******************************************************************************
630 * GetRecordInfoFromTypeInfo [OLEAUT32.332]
632 HRESULT WINAPI
GetRecordInfoFromTypeInfo(ITypeInfo
* pTI
, IRecordInfo
** ppRecInfo
) {
635 IRecordInfoImpl
*ret
;
636 ITypeInfo
*pTypeInfo
;
640 TRACE("(%p %p)\n", pTI
, ppRecInfo
);
642 if(!pTI
|| !ppRecInfo
)
645 hres
= ITypeInfo_GetTypeAttr(pTI
, &typeattr
);
646 if(FAILED(hres
) || !typeattr
) {
647 WARN("GetTypeAttr failed: %08x\n", hres
);
651 if(typeattr
->typekind
== TKIND_ALIAS
) {
652 hres
= ITypeInfo_GetRefTypeInfo(pTI
, typeattr
->tdescAlias
.u
.hreftype
, &pTypeInfo
);
653 guid
= typeattr
->guid
;
654 ITypeInfo_ReleaseTypeAttr(pTI
, typeattr
);
656 WARN("GetRefTypeInfo failed: %08x\n", hres
);
659 ITypeInfo_GetTypeAttr(pTypeInfo
, &typeattr
);
662 ITypeInfo_AddRef(pTypeInfo
);
663 guid
= typeattr
->guid
;
666 if(typeattr
->typekind
!= TKIND_RECORD
) {
667 WARN("typekind != TKIND_RECORD\n");
668 ITypeInfo_ReleaseTypeAttr(pTypeInfo
, typeattr
);
669 ITypeInfo_Release(pTypeInfo
);
673 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(*ret
));
674 ret
->IRecordInfo_iface
.lpVtbl
= &IRecordInfoImplVtbl
;
676 ret
->pTypeInfo
= pTypeInfo
;
677 ret
->n_vars
= typeattr
->cVars
;
678 ret
->size
= typeattr
->cbSizeInstance
;
679 ITypeInfo_ReleaseTypeAttr(pTypeInfo
, typeattr
);
683 /* NOTE: Windows implementation calls ITypeInfo::GetCantainingTypeLib and
684 * ITypeLib::GetLibAttr, but we currently don't need this.
687 hres
= ITypeInfo_GetDocumentation(pTypeInfo
, MEMBERID_NIL
, &ret
->name
, NULL
, NULL
, NULL
);
689 WARN("ITypeInfo::GetDocumentation failed\n");
693 ret
->fields
= HeapAlloc(GetProcessHeap(), 0, ret
->n_vars
*sizeof(fieldstr
));
694 for(i
= 0; i
<ret
->n_vars
; i
++) {
696 hres
= ITypeInfo_GetVarDesc(pTypeInfo
, i
, &vardesc
);
698 WARN("GetVarDesc failed\n");
701 ret
->fields
[i
].vt
= vardesc
->elemdescVar
.tdesc
.vt
;
702 ret
->fields
[i
].varkind
= vardesc
->varkind
;
703 ret
->fields
[i
].offset
= vardesc
->u
.oInst
;
704 hres
= ITypeInfo_GetDocumentation(pTypeInfo
, vardesc
->memid
, &ret
->fields
[i
].name
,
707 WARN("GetDocumentation failed: %08x\n", hres
);
708 TRACE("field=%s, offset=%d\n", debugstr_w(ret
->fields
[i
].name
), ret
->fields
[i
].offset
);
709 ITypeInfo_ReleaseVarDesc(pTypeInfo
, vardesc
);
712 *ppRecInfo
= &ret
->IRecordInfo_iface
;