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
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
41 IRecordInfo IRecordInfo_iface
;
53 static inline IRecordInfoImpl
*impl_from_IRecordInfo(IRecordInfo
*iface
)
55 return CONTAINING_RECORD(iface
, IRecordInfoImpl
, IRecordInfo_iface
);
58 static HRESULT
copy_to_variant(void *src
, VARIANT
*pvar
, enum VARENUM vt
)
60 TRACE("%p %p %d\n", src
, pvar
, vt
);
62 #define CASE_COPY(x) \
64 memcpy(&V_ ## x(pvar), src, sizeof(V_ ## x(pvar))); \
89 FIXME("Not supported type: %d\n", vt
);
98 static HRESULT
copy_from_variant(VARIANT
*src
, void *dest
, enum VARENUM vt
)
103 TRACE("(%p(%d) %p %d)\n", src
, V_VT(src
), dest
, vt
);
105 hres
= VariantChangeType(&var
, src
, 0, vt
);
109 #define CASE_COPY(x) \
111 memcpy(dest, &V_ ## x(&var), sizeof(V_ ## x(&var))); \
136 FIXME("Not supported type: %d\n", V_VT(&var
));
143 static HRESULT WINAPI
IRecordInfoImpl_QueryInterface(IRecordInfo
*iface
, REFIID riid
,
146 TRACE("(%p)->(%s %p)\n", iface
, debugstr_guid(riid
), ppvObject
);
150 if(IsEqualGUID(&IID_IUnknown
, riid
) || IsEqualGUID(&IID_IRecordInfo
, riid
)) {
152 IRecordInfo_AddRef(iface
);
156 FIXME("Not supported interface: %s\n", debugstr_guid(riid
));
157 return E_NOINTERFACE
;
160 static ULONG WINAPI
IRecordInfoImpl_AddRef(IRecordInfo
*iface
)
162 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
163 ULONG ref
= InterlockedIncrement(&This
->ref
);
164 TRACE("%p, refcount %lu.\n", iface
, ref
);
168 static ULONG WINAPI
IRecordInfoImpl_Release(IRecordInfo
*iface
)
170 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
171 ULONG ref
= InterlockedDecrement(&This
->ref
);
173 TRACE("%p, refcount %lu.\n", iface
, ref
);
177 for(i
=0; i
<This
->n_vars
; i
++)
178 SysFreeString(This
->fields
[i
].name
);
179 SysFreeString(This
->name
);
181 ITypeInfo_Release(This
->pTypeInfo
);
187 static HRESULT WINAPI
IRecordInfoImpl_RecordInit(IRecordInfo
*iface
, PVOID pvNew
)
189 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
190 TRACE("(%p)->(%p)\n", This
, pvNew
);
195 memset(pvNew
, 0, This
->size
);
199 static HRESULT WINAPI
IRecordInfoImpl_RecordClear(IRecordInfo
*iface
, PVOID pvExisting
)
201 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
205 TRACE("(%p)->(%p)\n", This
, pvExisting
);
210 for(i
=0; i
<This
->n_vars
; i
++) {
211 if(This
->fields
[i
].varkind
!= VAR_PERINSTANCE
) {
212 ERR("varkind != VAR_PERINSTANCE\n");
215 var
= ((PBYTE
)pvExisting
)+This
->fields
[i
].offset
;
216 switch(This
->fields
[i
].vt
) {
218 SysFreeString(*(BSTR
*)var
);
245 SafeArrayDestroy(var
);
250 IUnknown
*unk
= *(IUnknown
**)var
;
252 IUnknown_Release(unk
);
257 FIXME("Not supported vt = %d\n", This
->fields
[i
].vt
);
265 static HRESULT WINAPI
IRecordInfoImpl_RecordCopy(IRecordInfo
*iface
, void *src_rec
, void *dest_rec
)
267 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
271 TRACE("(%p)->(%p %p)\n", This
, src_rec
, dest_rec
);
273 if(!src_rec
|| !dest_rec
)
276 /* release already stored data */
277 IRecordInfo_RecordClear(iface
, dest_rec
);
279 for (i
= 0; i
< This
->n_vars
; i
++)
283 if (This
->fields
[i
].varkind
!= VAR_PERINSTANCE
) {
284 ERR("varkind != VAR_PERINSTANCE\n");
288 src
= ((BYTE
*)src_rec
) + This
->fields
[i
].offset
;
289 dest
= ((BYTE
*)dest_rec
) + This
->fields
[i
].offset
;
290 switch (This
->fields
[i
].vt
)
294 BSTR src_str
= *(BSTR
*)src
;
298 BSTR str
= SysAllocString(*(BSTR
*)src
);
299 if (!str
) hr
= E_OUTOFMEMORY
;
310 IUnknown
*unk
= *(IUnknown
**)src
;
311 *(IUnknown
**)dest
= unk
;
312 if (unk
) IUnknown_AddRef(unk
);
316 hr
= SafeArrayCopy(src
, dest
);
320 /* copy directly for types that don't need deep copy */
321 int len
= get_type_size(NULL
, This
->fields
[i
].vt
);
322 memcpy(dest
, src
, len
);
327 if (FAILED(hr
)) break;
331 IRecordInfo_RecordClear(iface
, dest_rec
);
336 static HRESULT WINAPI
IRecordInfoImpl_GetGuid(IRecordInfo
*iface
, GUID
*pguid
)
338 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
340 TRACE("(%p)->(%p)\n", This
, pguid
);
349 static HRESULT WINAPI
IRecordInfoImpl_GetName(IRecordInfo
*iface
, BSTR
*pbstrName
)
351 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
353 TRACE("(%p)->(%p)\n", This
, pbstrName
);
358 *pbstrName
= SysAllocString(This
->name
);
362 static HRESULT WINAPI
IRecordInfoImpl_GetSize(IRecordInfo
*iface
, ULONG
*pcbSize
)
364 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
366 TRACE("(%p)->(%p)\n", This
, pcbSize
);
371 *pcbSize
= This
->size
;
375 static HRESULT WINAPI
IRecordInfoImpl_GetTypeInfo(IRecordInfo
*iface
, ITypeInfo
**ppTypeInfo
)
377 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
379 TRACE("(%p)->(%p)\n", This
, ppTypeInfo
);
384 ITypeInfo_AddRef(This
->pTypeInfo
);
385 *ppTypeInfo
= This
->pTypeInfo
;
390 static HRESULT WINAPI
IRecordInfoImpl_GetField(IRecordInfo
*iface
, PVOID pvData
,
391 LPCOLESTR szFieldName
, VARIANT
*pvarField
)
393 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
396 TRACE("(%p)->(%p %s %p)\n", This
, pvData
, debugstr_w(szFieldName
), pvarField
);
398 if(!pvData
|| !szFieldName
|| !pvarField
)
401 for(i
=0; i
<This
->n_vars
; i
++)
402 if(!wcscmp(This
->fields
[i
].name
, szFieldName
))
404 if(i
== This
->n_vars
)
405 return TYPE_E_FIELDNOTFOUND
;
407 VariantClear(pvarField
);
408 return copy_to_variant(((PBYTE
)pvData
)+This
->fields
[i
].offset
, pvarField
,
412 static HRESULT WINAPI
IRecordInfoImpl_GetFieldNoCopy(IRecordInfo
*iface
, PVOID pvData
,
413 LPCOLESTR szFieldName
, VARIANT
*pvarField
, PVOID
*ppvDataCArray
)
415 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
418 TRACE("(%p)->(%p %s %p %p)\n", This
, pvData
, debugstr_w(szFieldName
), pvarField
, ppvDataCArray
);
420 if(!pvData
|| !szFieldName
|| !pvarField
)
423 for(i
=0; i
<This
->n_vars
; i
++)
424 if(!wcscmp(This
->fields
[i
].name
, szFieldName
))
426 if(i
== This
->n_vars
)
427 return TYPE_E_FIELDNOTFOUND
;
429 VariantClear(pvarField
);
430 V_VT(pvarField
) = VT_BYREF
|This
->fields
[i
].vt
;
431 V_BYREF(pvarField
) = ((PBYTE
)pvData
)+This
->fields
[i
].offset
;
432 *ppvDataCArray
= NULL
;
436 static HRESULT WINAPI
IRecordInfoImpl_PutField(IRecordInfo
*iface
, ULONG wFlags
, PVOID pvData
,
437 LPCOLESTR szFieldName
, VARIANT
*pvarField
)
439 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
442 TRACE("%p, %#lx, %p, %s, %p.\n", iface
, wFlags
, pvData
, debugstr_w(szFieldName
), pvarField
);
444 if(!pvData
|| !szFieldName
|| !pvarField
445 || (wFlags
!= INVOKE_PROPERTYPUTREF
&& wFlags
!= INVOKE_PROPERTYPUT
))
448 if(wFlags
== INVOKE_PROPERTYPUTREF
) {
449 FIXME("wFlag == INVOKE_PROPERTYPUTREF not supported\n");
453 for(i
=0; i
<This
->n_vars
; i
++)
454 if(!wcscmp(This
->fields
[i
].name
, szFieldName
))
456 if(i
== This
->n_vars
)
457 return TYPE_E_FIELDNOTFOUND
;
459 return copy_from_variant(pvarField
, ((PBYTE
)pvData
)+This
->fields
[i
].offset
,
463 static HRESULT WINAPI
IRecordInfoImpl_PutFieldNoCopy(IRecordInfo
*iface
, ULONG wFlags
,
464 PVOID pvData
, LPCOLESTR szFieldName
, VARIANT
*pvarField
)
466 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
469 FIXME("%p, %#lx, %p, %s, %p stub\n", iface
, wFlags
, pvData
, debugstr_w(szFieldName
), pvarField
);
471 if(!pvData
|| !szFieldName
|| !pvarField
472 || (wFlags
!= INVOKE_PROPERTYPUTREF
&& wFlags
!= INVOKE_PROPERTYPUT
))
475 for(i
=0; i
<This
->n_vars
; i
++)
476 if(!wcscmp(This
->fields
[i
].name
, szFieldName
))
478 if(i
== This
->n_vars
)
479 return TYPE_E_FIELDNOTFOUND
;
484 static HRESULT WINAPI
IRecordInfoImpl_GetFieldNames(IRecordInfo
*iface
, ULONG
*pcNames
,
487 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
488 ULONG n
= This
->n_vars
, i
;
490 TRACE("(%p)->(%p %p)\n", This
, pcNames
, rgBstrNames
);
500 rgBstrNames
[i
] = SysAllocString(This
->fields
[i
].name
);
507 static BOOL WINAPI
IRecordInfoImpl_IsMatchingType(IRecordInfo
*iface
, IRecordInfo
*info2
)
509 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
512 TRACE( "(%p)->(%p)\n", This
, info2
);
514 IRecordInfo_GetGuid( info2
, &guid2
);
515 return IsEqualGUID( &This
->guid
, &guid2
);
518 static PVOID WINAPI
IRecordInfoImpl_RecordCreate(IRecordInfo
*iface
)
520 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
523 TRACE("(%p)\n", This
);
525 record
= CoTaskMemAlloc(This
->size
);
526 IRecordInfo_RecordInit(iface
, record
);
527 TRACE("created record at %p\n", record
);
531 static HRESULT WINAPI
IRecordInfoImpl_RecordCreateCopy(IRecordInfo
*iface
, PVOID pvSource
,
534 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
536 TRACE("(%p)->(%p %p)\n", This
, pvSource
, ppvDest
);
538 if(!pvSource
|| !ppvDest
)
541 *ppvDest
= IRecordInfo_RecordCreate(iface
);
542 return IRecordInfo_RecordCopy(iface
, pvSource
, *ppvDest
);
545 static HRESULT WINAPI
IRecordInfoImpl_RecordDestroy(IRecordInfo
*iface
, PVOID pvRecord
)
547 IRecordInfoImpl
*This
= impl_from_IRecordInfo(iface
);
550 TRACE("(%p)->(%p)\n", This
, pvRecord
);
552 hres
= IRecordInfo_RecordClear(iface
, pvRecord
);
556 CoTaskMemFree(pvRecord
);
560 static const IRecordInfoVtbl IRecordInfoImplVtbl
= {
561 IRecordInfoImpl_QueryInterface
,
562 IRecordInfoImpl_AddRef
,
563 IRecordInfoImpl_Release
,
564 IRecordInfoImpl_RecordInit
,
565 IRecordInfoImpl_RecordClear
,
566 IRecordInfoImpl_RecordCopy
,
567 IRecordInfoImpl_GetGuid
,
568 IRecordInfoImpl_GetName
,
569 IRecordInfoImpl_GetSize
,
570 IRecordInfoImpl_GetTypeInfo
,
571 IRecordInfoImpl_GetField
,
572 IRecordInfoImpl_GetFieldNoCopy
,
573 IRecordInfoImpl_PutField
,
574 IRecordInfoImpl_PutFieldNoCopy
,
575 IRecordInfoImpl_GetFieldNames
,
576 IRecordInfoImpl_IsMatchingType
,
577 IRecordInfoImpl_RecordCreate
,
578 IRecordInfoImpl_RecordCreateCopy
,
579 IRecordInfoImpl_RecordDestroy
582 /******************************************************************************
583 * GetRecordInfoFromGuids [OLEAUT32.322]
587 * Failure: E_INVALIDARG, if any argument is invalid.
589 HRESULT WINAPI
GetRecordInfoFromGuids(REFGUID rGuidTypeLib
, ULONG uVerMajor
,
590 ULONG uVerMinor
, LCID lcid
, REFGUID rGuidTypeInfo
, IRecordInfo
** ppRecInfo
)
592 ITypeInfo
*pTypeInfo
;
596 TRACE("%s, %lu, %lu, %#lx, %s, %p.\n", debugstr_guid(rGuidTypeLib
), uVerMajor
, uVerMinor
,
597 lcid
, debugstr_guid(rGuidTypeInfo
), ppRecInfo
);
599 hres
= LoadRegTypeLib(rGuidTypeLib
, uVerMajor
, uVerMinor
, lcid
, &pTypeLib
);
601 WARN("LoadRegTypeLib failed!\n");
605 hres
= ITypeLib_GetTypeInfoOfGuid(pTypeLib
, rGuidTypeInfo
, &pTypeInfo
);
606 ITypeLib_Release(pTypeLib
);
608 WARN("GetTypeInfoOfGuid failed!\n");
612 hres
= GetRecordInfoFromTypeInfo(pTypeInfo
, ppRecInfo
);
613 ITypeInfo_Release(pTypeInfo
);
617 /******************************************************************************
618 * GetRecordInfoFromTypeInfo [OLEAUT32.332]
620 HRESULT WINAPI
GetRecordInfoFromTypeInfo(ITypeInfo
* pTI
, IRecordInfo
** ppRecInfo
) {
623 IRecordInfoImpl
*ret
;
624 ITypeInfo
*pTypeInfo
;
628 TRACE("(%p %p)\n", pTI
, ppRecInfo
);
630 if(!pTI
|| !ppRecInfo
)
633 hres
= ITypeInfo_GetTypeAttr(pTI
, &typeattr
);
634 if(FAILED(hres
) || !typeattr
) {
635 WARN("GetTypeAttr failed: %#lx.\n", hres
);
639 if(typeattr
->typekind
== TKIND_ALIAS
) {
640 hres
= ITypeInfo_GetRefTypeInfo(pTI
, typeattr
->tdescAlias
.hreftype
, &pTypeInfo
);
641 guid
= typeattr
->guid
;
642 ITypeInfo_ReleaseTypeAttr(pTI
, typeattr
);
644 WARN("GetRefTypeInfo failed: %#lx.\n", hres
);
647 hres
= ITypeInfo_GetTypeAttr(pTypeInfo
, &typeattr
);
649 ITypeInfo_Release(pTypeInfo
);
650 WARN("GetTypeAttr failed for referenced type: %#lx.\n", hres
);
655 ITypeInfo_AddRef(pTypeInfo
);
656 guid
= typeattr
->guid
;
659 if(typeattr
->typekind
!= TKIND_RECORD
) {
660 WARN("typekind != TKIND_RECORD\n");
661 ITypeInfo_ReleaseTypeAttr(pTypeInfo
, typeattr
);
662 ITypeInfo_Release(pTypeInfo
);
666 ret
= calloc(1, sizeof(*ret
));
667 ret
->IRecordInfo_iface
.lpVtbl
= &IRecordInfoImplVtbl
;
669 ret
->pTypeInfo
= pTypeInfo
;
670 ret
->n_vars
= typeattr
->cVars
;
671 ret
->size
= typeattr
->cbSizeInstance
;
672 ITypeInfo_ReleaseTypeAttr(pTypeInfo
, typeattr
);
676 /* NOTE: Windows implementation calls ITypeInfo::GetCantainingTypeLib and
677 * ITypeLib::GetLibAttr, but we currently don't need this.
680 hres
= ITypeInfo_GetDocumentation(pTypeInfo
, MEMBERID_NIL
, &ret
->name
, NULL
, NULL
, NULL
);
682 WARN("ITypeInfo::GetDocumentation failed\n");
686 ret
->fields
= calloc(ret
->n_vars
, sizeof(fieldstr
));
687 for(i
= 0; i
<ret
->n_vars
; i
++) {
689 hres
= ITypeInfo_GetVarDesc(pTypeInfo
, i
, &vardesc
);
691 WARN("GetVarDesc failed\n");
694 ret
->fields
[i
].vt
= vardesc
->elemdescVar
.tdesc
.vt
;
695 ret
->fields
[i
].varkind
= vardesc
->varkind
;
696 ret
->fields
[i
].offset
= vardesc
->oInst
;
697 hres
= ITypeInfo_GetDocumentation(pTypeInfo
, vardesc
->memid
, &ret
->fields
[i
].name
,
700 WARN("GetDocumentation failed: %#lx.\n", hres
);
701 TRACE("field=%s, offset=%ld\n", debugstr_w(ret
->fields
[i
].name
), ret
->fields
[i
].offset
);
702 ITypeInfo_ReleaseVarDesc(pTypeInfo
, vardesc
);
705 *ppRecInfo
= &ret
->IRecordInfo_iface
;