msvcrt/tests: Remove a space before a '\n'.
[wine/gsoc-2012-control.git] / dlls / oleaut32 / recinfo.c
blob19ae217cb9493e63fd4c740f3d6fcd5e359d96d7
1 /*
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
19 #include <stdarg.h>
21 #define COBJMACROS
22 #define NONAMELESSUNION
23 #define NONAMELESSSTRUCT
25 #include "windef.h"
26 #include "winbase.h"
27 #include "objbase.h"
28 #include "oaidl.h"
29 #include "oleauto.h"
31 #include "wine/unicode.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(ole);
36 typedef struct {
37 enum VARENUM vt;
38 VARKIND varkind;
39 ULONG offset;
40 BSTR name;
41 } fieldstr;
43 typedef struct {
44 const IRecordInfoVtbl *lpVtbl;
45 LONG ref;
47 GUID guid;
48 UINT lib_index;
49 WORD n_vars;
50 ULONG size;
51 BSTR name;
52 fieldstr *fields;
53 ITypeInfo *pTypeInfo;
54 } IRecordInfoImpl;
56 static HRESULT copy_to_variant(void *src, VARIANT *pvar, enum VARENUM vt)
58 TRACE("%p %p %d\n", src, pvar, vt);
60 #define CASE_COPY(x) \
61 case VT_ ## x: \
62 memcpy(&V_ ## x(pvar), src, sizeof(V_ ## x(pvar))); \
63 break
65 switch(vt) {
66 CASE_COPY(I2);
67 CASE_COPY(I4);
68 CASE_COPY(R4);
69 CASE_COPY(R8);
70 CASE_COPY(CY);
71 CASE_COPY(DATE);
72 CASE_COPY(BSTR);
73 CASE_COPY(ERROR);
74 CASE_COPY(BOOL);
75 CASE_COPY(DECIMAL);
76 CASE_COPY(I1);
77 CASE_COPY(UI1);
78 CASE_COPY(UI2);
79 CASE_COPY(UI4);
80 CASE_COPY(I8);
81 CASE_COPY(UI8);
82 CASE_COPY(INT);
83 CASE_COPY(UINT);
84 CASE_COPY(INT_PTR);
85 CASE_COPY(UINT_PTR);
86 default:
87 FIXME("Not supported type: %d\n", vt);
88 return E_NOTIMPL;
90 #undef CASE_COPY
92 V_VT(pvar) = vt;
93 return S_OK;
96 static HRESULT copy_from_variant(VARIANT *src, void *dest, enum VARENUM vt)
98 VARIANT var;
99 HRESULT hres;
101 TRACE("(%p(%d) %p %d)\n", src, V_VT(src), dest, vt);
103 hres = VariantChangeType(&var, src, 0, vt);
104 if(FAILED(hres))
105 return hres;
107 #define CASE_COPY(x) \
108 case VT_ ## x: \
109 memcpy(dest, &V_ ## x(&var), sizeof(V_ ## x(&var))); \
110 break
112 switch(vt) {
113 CASE_COPY(I2);
114 CASE_COPY(I4);
115 CASE_COPY(R4);
116 CASE_COPY(R8);
117 CASE_COPY(CY);
118 CASE_COPY(DATE);
119 CASE_COPY(BSTR);
120 CASE_COPY(ERROR);
121 CASE_COPY(BOOL);
122 CASE_COPY(DECIMAL);
123 CASE_COPY(I1);
124 CASE_COPY(UI1);
125 CASE_COPY(UI2);
126 CASE_COPY(UI4);
127 CASE_COPY(I8);
128 CASE_COPY(UI8);
129 CASE_COPY(INT);
130 CASE_COPY(UINT);
131 CASE_COPY(INT_PTR);
132 CASE_COPY(UINT_PTR);
133 default:
134 FIXME("Not supported type: %d\n", V_VT(&var));
135 return E_NOTIMPL;
137 #undef CASE_COPY
138 return S_OK;
141 static HRESULT WINAPI IRecordInfoImpl_QueryInterface(IRecordInfo *iface, REFIID riid,
142 void **ppvObject)
144 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppvObject);
146 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IRecordInfo, riid)) {
147 *ppvObject = iface;
148 IRecordInfo_AddRef(iface);
149 return S_OK;
152 FIXME("Not supported interface: %s\n", debugstr_guid(riid));
153 return E_NOINTERFACE;
156 static ULONG WINAPI IRecordInfoImpl_AddRef(IRecordInfo *iface)
158 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
159 ULONG ref = InterlockedIncrement(&This->ref);
160 TRACE("(%p) -> %d\n", This, ref);
161 return ref;
164 static ULONG WINAPI IRecordInfoImpl_Release(IRecordInfo *iface)
166 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
167 ULONG ref = InterlockedDecrement(&This->ref);
169 TRACE("(%p) -> %d\n", This, ref);
171 if(!ref) {
172 int i;
173 for(i=0; i<This->n_vars; i++)
174 SysFreeString(This->fields[i].name);
175 HeapFree(GetProcessHeap(), 0, This->name);
176 HeapFree(GetProcessHeap(), 0, This->fields);
177 ITypeInfo_Release(This->pTypeInfo);
178 HeapFree(GetProcessHeap(), 0, This);
180 return ref;
183 static HRESULT WINAPI IRecordInfoImpl_RecordInit(IRecordInfo *iface, PVOID pvNew)
185 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
186 TRACE("(%p)->(%p)\n", This, pvNew);
188 if(!pvNew)
189 return E_INVALIDARG;
191 memset(pvNew, 0, This->size);
192 return S_OK;
195 static HRESULT WINAPI IRecordInfoImpl_RecordClear(IRecordInfo *iface, PVOID pvExisting)
197 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
198 int i;
199 PVOID var;
201 TRACE("(%p)->(%p)\n", This, pvExisting);
203 if(!pvExisting)
204 return E_INVALIDARG;
206 for(i=0; i<This->n_vars; i++) {
207 if(This->fields[i].varkind != VAR_PERINSTANCE) {
208 ERR("varkind != VAR_PERINSTANCE\n");
209 continue;
211 var = ((PBYTE)pvExisting)+This->fields[i].offset;
212 switch(This->fields[i].vt) {
213 case VT_BSTR:
214 SysFreeString(*(BSTR*)var);
215 *(BSTR*)var = NULL;
216 break;
217 case VT_I2:
218 case VT_I4:
219 case VT_R4:
220 case VT_R8:
221 case VT_CY:
222 case VT_DATE:
223 case VT_ERROR:
224 case VT_BOOL:
225 case VT_DECIMAL:
226 case VT_I1:
227 case VT_UI1:
228 case VT_UI2:
229 case VT_UI4:
230 case VT_I8:
231 case VT_UI8:
232 case VT_INT:
233 case VT_UINT:
234 break;
235 case VT_INT_PTR:
236 case VT_UINT_PTR:
237 *(void**)var = NULL;
238 break;
239 case VT_SAFEARRAY:
240 SafeArrayDestroy((SAFEARRAY*)var);
241 break;
242 default:
243 FIXME("Not supported vt = %d\n", This->fields[i].vt);
244 break;
248 return S_OK;
251 static HRESULT WINAPI IRecordInfoImpl_RecordCopy(IRecordInfo *iface, PVOID pvExisting,
252 PVOID pvNew)
254 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
256 TRACE("(%p)->(%p %p)\n", This, pvExisting, pvNew);
258 if(!pvExisting || !pvNew)
259 return E_INVALIDARG;
261 memcpy(pvExisting, pvNew, This->size);
262 return S_OK;
265 static HRESULT WINAPI IRecordInfoImpl_GetGuid(IRecordInfo *iface, GUID *pguid)
267 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
269 TRACE("(%p)->(%p)\n", This, pguid);
271 if(!pguid)
272 return E_INVALIDARG;
274 *pguid = This->guid;
275 return S_OK;
278 static HRESULT WINAPI IRecordInfoImpl_GetName(IRecordInfo *iface, BSTR *pbstrName)
280 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
282 TRACE("(%p)->(%p)\n", This, pbstrName);
284 if(!pbstrName)
285 return E_INVALIDARG;
287 *pbstrName = SysAllocString(This->name);
288 return S_OK;
291 static HRESULT WINAPI IRecordInfoImpl_GetSize(IRecordInfo *iface, ULONG *pcbSize)
293 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
295 TRACE("(%p)->(%p)\n", This, pcbSize);
297 if(!pcbSize)
298 return E_INVALIDARG;
300 *pcbSize = This->size;
301 return S_OK;
304 static HRESULT WINAPI IRecordInfoImpl_GetTypeInfo(IRecordInfo *iface, ITypeInfo **ppTypeInfo)
306 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
308 TRACE("(%p)->(%p)\n", This, ppTypeInfo);
310 if(!ppTypeInfo)
311 return E_INVALIDARG;
313 ITypeInfo_AddRef(This->pTypeInfo);
314 *ppTypeInfo = This->pTypeInfo;
316 return S_OK;
319 static HRESULT WINAPI IRecordInfoImpl_GetField(IRecordInfo *iface, PVOID pvData,
320 LPCOLESTR szFieldName, VARIANT *pvarField)
322 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
323 int i;
325 TRACE("(%p)->(%p %s %p)\n", This, pvData, debugstr_w(szFieldName), pvarField);
327 if(!pvData || !szFieldName || !pvarField)
328 return E_INVALIDARG;
330 for(i=0; i<This->n_vars; i++)
331 if(!strcmpW(This->fields[i].name, szFieldName))
332 break;
333 if(i == This->n_vars)
334 return TYPE_E_FIELDNOTFOUND;
336 VariantClear(pvarField);
337 return copy_to_variant(((PBYTE)pvData)+This->fields[i].offset, pvarField,
338 This->fields[i].vt);
341 static HRESULT WINAPI IRecordInfoImpl_GetFieldNoCopy(IRecordInfo *iface, PVOID pvData,
342 LPCOLESTR szFieldName, VARIANT *pvarField, PVOID *ppvDataCArray)
344 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
345 int i;
347 TRACE("(%p)->(%p %s %p %p)\n", This, pvData, debugstr_w(szFieldName), pvarField, ppvDataCArray);
349 if(!pvData || !szFieldName || !pvarField)
350 return E_INVALIDARG;
352 for(i=0; i<This->n_vars; i++)
353 if(!strcmpW(This->fields[i].name, szFieldName))
354 break;
355 if(i == This->n_vars)
356 return TYPE_E_FIELDNOTFOUND;
358 VariantClear(pvarField);
359 V_VT(pvarField) = VT_BYREF|This->fields[i].vt;
360 V_BYREF(pvarField) = ((PBYTE)pvData)+This->fields[i].offset;
361 *ppvDataCArray = NULL;
362 return S_OK;
365 static HRESULT WINAPI IRecordInfoImpl_PutField(IRecordInfo *iface, ULONG wFlags, PVOID pvData,
366 LPCOLESTR szFieldName, VARIANT *pvarField)
368 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
369 int i;
371 TRACE("(%p)->(%08x %p %s %p)\n", This, wFlags, pvData, debugstr_w(szFieldName),
372 pvarField);
374 if(!pvData || !szFieldName || !pvarField
375 || (wFlags != INVOKE_PROPERTYPUTREF && wFlags != INVOKE_PROPERTYPUT))
376 return E_INVALIDARG;
378 if(wFlags == INVOKE_PROPERTYPUTREF) {
379 FIXME("wFlag == INVOKE_PROPERTYPUTREF not supported\n");
380 return E_NOTIMPL;
383 for(i=0; i<This->n_vars; i++)
384 if(!strcmpW(This->fields[i].name, szFieldName))
385 break;
386 if(i == This->n_vars)
387 return TYPE_E_FIELDNOTFOUND;
389 return copy_from_variant(pvarField, ((PBYTE)pvData)+This->fields[i].offset,
390 This->fields[i].vt);
393 static HRESULT WINAPI IRecordInfoImpl_PutFieldNoCopy(IRecordInfo *iface, ULONG wFlags,
394 PVOID pvData, LPCOLESTR szFieldName, VARIANT *pvarField)
396 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
397 int i;
399 FIXME("(%p)->(%08x %p %s %p) stub\n", This, wFlags, pvData, debugstr_w(szFieldName), pvarField);
401 if(!pvData || !szFieldName || !pvarField
402 || (wFlags != INVOKE_PROPERTYPUTREF && wFlags != INVOKE_PROPERTYPUT))
403 return E_INVALIDARG;
405 for(i=0; i<This->n_vars; i++)
406 if(!strcmpW(This->fields[i].name, szFieldName))
407 break;
408 if(i == This->n_vars)
409 return TYPE_E_FIELDNOTFOUND;
411 return E_NOTIMPL;
414 static HRESULT WINAPI IRecordInfoImpl_GetFieldNames(IRecordInfo *iface, ULONG *pcNames,
415 BSTR *rgBstrNames)
417 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
418 ULONG n = This->n_vars, i;
420 TRACE("(%p)->(%p %p)\n", This, pcNames, rgBstrNames);
422 if(!pcNames)
423 return E_INVALIDARG;
425 if(*pcNames < n)
426 n = *pcNames;
428 if(rgBstrNames) {
429 for(i=0; i<n; i++)
430 rgBstrNames[i] = SysAllocString(This->fields[i].name);
433 *pcNames = n;
434 return S_OK;
437 static BOOL WINAPI IRecordInfoImpl_IsMatchingType(IRecordInfo *iface, IRecordInfo *pRecordInfo)
439 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
441 FIXME("(%p)->(%p) stub\n", This, pRecordInfo);
443 return FALSE;
446 static PVOID WINAPI IRecordInfoImpl_RecordCreate(IRecordInfo *iface)
448 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
450 TRACE("(%p)\n", This);
452 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->size);
455 static HRESULT WINAPI IRecordInfoImpl_RecordCreateCopy(IRecordInfo *iface, PVOID pvSource,
456 PVOID *ppvDest)
458 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
460 TRACE("(%p)->(%p %p)\n", This, pvSource, ppvDest);
462 if(!pvSource || !ppvDest)
463 return E_INVALIDARG;
465 *ppvDest = IRecordInfo_RecordCreate(iface);
466 return IRecordInfo_RecordCopy(iface, pvSource, *ppvDest);
469 static HRESULT WINAPI IRecordInfoImpl_RecordDestroy(IRecordInfo *iface, PVOID pvRecord)
471 IRecordInfoImpl *This = (IRecordInfoImpl*)iface;
472 HRESULT hres;
474 TRACE("(%p)->(%p)\n", This, pvRecord);
476 hres = IRecordInfo_RecordClear(iface, pvRecord);
477 if(FAILED(hres))
478 return hres;
480 if(!HeapFree(GetProcessHeap(), 0, pvRecord))
481 return E_INVALIDARG;
483 return S_OK;
486 static const IRecordInfoVtbl IRecordInfoImplVtbl = {
487 IRecordInfoImpl_QueryInterface,
488 IRecordInfoImpl_AddRef,
489 IRecordInfoImpl_Release,
490 IRecordInfoImpl_RecordInit,
491 IRecordInfoImpl_RecordClear,
492 IRecordInfoImpl_RecordCopy,
493 IRecordInfoImpl_GetGuid,
494 IRecordInfoImpl_GetName,
495 IRecordInfoImpl_GetSize,
496 IRecordInfoImpl_GetTypeInfo,
497 IRecordInfoImpl_GetField,
498 IRecordInfoImpl_GetFieldNoCopy,
499 IRecordInfoImpl_PutField,
500 IRecordInfoImpl_PutFieldNoCopy,
501 IRecordInfoImpl_GetFieldNames,
502 IRecordInfoImpl_IsMatchingType,
503 IRecordInfoImpl_RecordCreate,
504 IRecordInfoImpl_RecordCreateCopy,
505 IRecordInfoImpl_RecordDestroy
508 /******************************************************************************
509 * GetRecordInfoFromGuids [OLEAUT32.322]
511 * RETURNS
512 * Success: S_OK
513 * Failure: E_INVALIDARG, if any argument is invalid.
515 HRESULT WINAPI GetRecordInfoFromGuids(REFGUID rGuidTypeLib, ULONG uVerMajor,
516 ULONG uVerMinor, LCID lcid, REFGUID rGuidTypeInfo, IRecordInfo** ppRecInfo)
518 ITypeInfo *pTypeInfo;
519 ITypeLib *pTypeLib;
520 HRESULT hres;
522 TRACE("(%p,%d,%d,%d,%p,%p)\n", rGuidTypeLib, uVerMajor, uVerMinor,
523 lcid, rGuidTypeInfo, ppRecInfo);
525 hres = LoadRegTypeLib(rGuidTypeLib, uVerMajor, uVerMinor, lcid, &pTypeLib);
526 if(FAILED(hres)) {
527 WARN("LoadRegTypeLib failed!\n");
528 return hres;
531 hres = ITypeLib_GetTypeInfoOfGuid(pTypeLib, rGuidTypeInfo, &pTypeInfo);
532 ITypeLib_Release(pTypeLib);
533 if(FAILED(hres)) {
534 WARN("GetTypeInfoOfGuid failed!\n");
535 return hres;
538 hres = GetRecordInfoFromTypeInfo(pTypeInfo, ppRecInfo);
539 ITypeInfo_Release(pTypeInfo);
540 return hres;
543 /******************************************************************************
544 * GetRecordInfoFromTypeInfo [OLEAUT32.332]
546 HRESULT WINAPI GetRecordInfoFromTypeInfo(ITypeInfo* pTI, IRecordInfo** ppRecInfo) {
547 HRESULT hres;
548 TYPEATTR *typeattr;
549 IRecordInfoImpl *ret;
550 ITypeInfo *pTypeInfo;
551 int i;
552 GUID guid;
554 TRACE("(%p %p)\n", pTI, ppRecInfo);
556 if(!pTI || !ppRecInfo)
557 return E_INVALIDARG;
559 hres = ITypeInfo_GetTypeAttr(pTI, &typeattr);
560 if(FAILED(hres) || !typeattr) {
561 WARN("GetTypeAttr failed: %08x\n", hres);
562 return hres;
565 if(typeattr->typekind == TKIND_ALIAS) {
566 hres = ITypeInfo_GetRefTypeInfo(pTI, typeattr->tdescAlias.u.hreftype, &pTypeInfo);
567 guid = typeattr->guid;
568 ITypeInfo_ReleaseTypeAttr(pTI, typeattr);
569 if(FAILED(hres)) {
570 WARN("GetRefTypeInfo failed: %08x\n", hres);
571 return hres;
573 ITypeInfo_GetTypeAttr(pTypeInfo, &typeattr);
574 }else {
575 pTypeInfo = pTI;
576 ITypeInfo_AddRef(pTypeInfo);
577 guid = typeattr->guid;
580 if(typeattr->typekind != TKIND_RECORD) {
581 WARN("typekind != TKIND_RECORD\n");
582 ITypeInfo_ReleaseTypeAttr(pTypeInfo, typeattr);
583 ITypeInfo_Release(pTypeInfo);
584 return E_INVALIDARG;
587 ret = HeapAlloc(GetProcessHeap(), 0, sizeof(*ret));
588 ret->lpVtbl = &IRecordInfoImplVtbl;
589 ret->ref = 1;
590 ret->pTypeInfo = pTypeInfo;
591 ret->n_vars = typeattr->cVars;
592 ret->size = typeattr->cbSizeInstance;
593 ITypeInfo_ReleaseTypeAttr(pTypeInfo, typeattr);
595 ret->guid = guid;
597 /* NOTE: Windows implementation calls ITypeInfo::GetCantainingTypeLib and
598 * ITypeLib::GetLibAttr, but we currently don't need this.
601 hres = ITypeInfo_GetDocumentation(pTypeInfo, MEMBERID_NIL, &ret->name, NULL, NULL, NULL);
602 if(FAILED(hres)) {
603 WARN("ITypeInfo::GetDocumentation failed\n");
604 ret->name = NULL;
607 ret->fields = HeapAlloc(GetProcessHeap(), 0, ret->n_vars*sizeof(VARDESC));
608 for(i = 0; i<ret->n_vars; i++) {
609 VARDESC *vardesc;
610 hres = ITypeInfo_GetVarDesc(pTypeInfo, i, &vardesc);
611 if(FAILED(hres)) {
612 WARN("GetVarDesc failed\n");
613 continue;
615 ret->fields[i].vt = vardesc->elemdescVar.tdesc.vt;
616 ret->fields[i].varkind = vardesc->varkind;
617 ret->fields[i].offset = vardesc->u.oInst;
618 hres = ITypeInfo_GetDocumentation(pTypeInfo, vardesc->memid, &ret->fields[i].name,
619 NULL, NULL, NULL);
620 if(FAILED(hres))
621 WARN("GetDocumentation failed: %08x\n", hres);
622 ITypeInfo_ReleaseVarDesc(pTypeInfo, vardesc);
625 *ppRecInfo = (IRecordInfo*)ret;
627 return S_OK;