oleaut: Reduce an ERR down to a WARN since a NULL interface pointer
[wine/multimedia.git] / dlls / oleaut32 / safearray.c
blob4ff056ab6a75ece4364f65069f229cb7c20071f4
1 /*************************************************************************
2 * OLE Automation - SafeArray
4 * This file contains the implementation of the SafeArray functions.
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 2002-2003 Marcus Meissner
8 * Copyright 2003 Jon Griffiths
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* Memory Layout of a SafeArray:
26 * -0x10: start of memory.
27 * -0x10: GUID for VT_DISPATCH and VT_UNKNOWN safearrays (if FADF_HAVEIID)
28 * -0x04: DWORD varianttype; (for all others, except VT_RECORD) (if FADF_HAVEVARTYPE)
29 * -0x4: IRecordInfo* iface; (if FADF_RECORD, for VT_RECORD (can be NULL))
30 * 0x00: SAFEARRAY,
31 * 0x10: SAFEARRAYBOUNDS[0...]
34 #include "config.h"
36 #include <string.h>
37 #include <stdarg.h>
38 #include <stdio.h>
40 #define COBJMACROS
42 #include "windef.h"
43 #include "winerror.h"
44 #include "winbase.h"
45 #include "variant.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(variant);
50 /************************************************************************
51 * SafeArray {OLEAUT32}
53 * NOTES
54 * The SafeArray data type provides the underlying interface for Ole
55 * Automations arrays, used for example to represent array types in
56 * Visual Basic(tm) and to gather user defined parameters for invocation through
57 * an IDispatch interface.
59 * Safe arrays provide bounds checking and automatically manage the data
60 * types they contain, for example handing reference counting and copying
61 * of interface pointers. User defined types can be stored in arrays
62 * using the IRecordInfo interface.
64 * There are two types of SafeArray, normal and vectors. Normal arrays can have
65 * multiple dimensions and the data for the array is allocated separately from
66 * the array header. This is the most flexible type of array. Vectors, on the
67 * other hand, are fixed in size and consist of a single allocated block, and a
68 * single dimension.
70 * DATATYPES
71 * The following types of data can be stored within a SafeArray.
72 * Numeric:
73 *| VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8, VT_UI8, VT_INT, VT_UINT,
74 *| VT_R4, VT_R8, VT_CY, VT_DECIMAL
75 * Interfaces:
76 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
77 * Other:
78 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
80 * FUNCTIONS
81 * BstrFromVector()
82 * VectorFromBstr()
85 /* Undocumented hidden space before the start of a SafeArray descriptor */
86 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
88 /* Allocate memory */
89 static inline LPVOID SAFEARRAY_Malloc(ULONG ulSize)
91 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
92 * instance returned from CoGetMalloc().
94 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulSize);
97 /* Free memory */
98 static inline BOOL SAFEARRAY_Free(LPVOID lpData)
100 return HeapFree(GetProcessHeap(), 0, lpData);
103 /* Get the size of a supported VT type (0 means unsupported) */
104 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
106 switch (vt)
108 case VT_I1:
109 case VT_UI1: return sizeof(BYTE);
110 case VT_BOOL:
111 case VT_I2:
112 case VT_UI2: return sizeof(SHORT);
113 case VT_I4:
114 case VT_UI4:
115 case VT_R4:
116 case VT_ERROR: return sizeof(LONG);
117 case VT_R8:
118 case VT_I8:
119 case VT_UI8: return sizeof(LONG64);
120 case VT_INT:
121 case VT_UINT: return sizeof(INT);
122 case VT_INT_PTR:
123 case VT_UINT_PTR: return sizeof(UINT_PTR);
124 case VT_CY: return sizeof(CY);
125 case VT_DATE: return sizeof(DATE);
126 case VT_BSTR: return sizeof(BSTR);
127 case VT_DISPATCH: return sizeof(LPDISPATCH);
128 case VT_VARIANT: return sizeof(VARIANT);
129 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
130 case VT_DECIMAL: return sizeof(DECIMAL);
131 /* Note: Return a non-zero size to indicate vt is valid. The actual size
132 * of a UDT is taken from the result of IRecordInfo_GetSize().
134 case VT_RECORD: return 32;
136 return 0;
139 /* Set the hidden data for an array */
140 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
142 /* Implementation data is stored in the 4 bytes before the header */
143 LPDWORD lpDw = (LPDWORD)psa;
144 lpDw[-1] = dw;
147 /* Get the hidden data from an array */
148 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
150 LPDWORD lpDw = (LPDWORD)psa;
151 return lpDw[-1];
154 /* Get the number of cells in a SafeArray */
155 static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
157 const SAFEARRAYBOUND* psab = psa->rgsabound;
158 USHORT cCount = psa->cDims;
159 ULONG ulNumCells = 1;
161 while (cCount--)
163 /* This is a valid bordercase. See testcases. -Marcus */
164 if (!psab->cElements)
165 return 0;
166 ulNumCells *= psab->cElements;
167 psab++;
169 return ulNumCells;
172 /* Get the 0 based index of an index into a dimension */
173 static inline ULONG SAFEARRAY_GetDimensionIndex(SAFEARRAYBOUND *psab, ULONG ulIndex)
175 return ulIndex - psab->lLbound;
178 /* Get the size of a dimension in cells */
179 static inline ULONG SAFEARRAY_GetDimensionCells(SAFEARRAY *psa, ULONG ulDim)
181 ULONG size = psa->rgsabound[0].cElements;
183 while (ulDim)
185 size *= psa->rgsabound[ulDim].cElements;
186 ulDim--;
188 return size;
191 /* Allocate a descriptor for an array */
192 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
194 *ppsaOut = (SAFEARRAY*)((char*)SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE) + SAFEARRAY_HIDDEN_SIZE);
196 if (!*ppsaOut)
197 return E_UNEXPECTED;
199 return S_OK;
202 /* Set the features of an array */
203 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
205 /* Set the IID if we have one, otherwise set the type */
206 if (vt == VT_DISPATCH)
208 psa->fFeatures = FADF_HAVEIID;
209 SafeArraySetIID(psa, &IID_IDispatch);
211 else if (vt == VT_UNKNOWN)
213 psa->fFeatures = FADF_HAVEIID;
214 SafeArraySetIID(psa, &IID_IUnknown);
216 else if (vt == VT_RECORD)
217 psa->fFeatures = FADF_RECORD;
218 else
220 psa->fFeatures = FADF_HAVEVARTYPE;
221 SAFEARRAY_SetHiddenDWORD(psa, vt);
225 /* Create an array */
226 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, ULONG ulSize)
228 SAFEARRAY *psa = NULL;
230 if (!rgsabound)
231 return NULL;
233 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
235 switch (vt)
237 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
238 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
239 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
240 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
243 memcpy(psa->rgsabound, rgsabound, cDims * sizeof(SAFEARRAYBOUND));
245 if (ulSize)
246 psa->cbElements = ulSize;
248 if (FAILED(SafeArrayAllocData(psa)))
250 SafeArrayDestroyDescriptor(psa);
251 psa = NULL;
254 return psa;
257 /* Create an array as a vector */
258 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
260 SAFEARRAY *psa = NULL;
262 if (ulSize || (vt == VT_RECORD))
264 /* Allocate the header and data together */
265 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
267 SAFEARRAY_SetFeatures(vt, psa);
269 psa->cDims = 1;
270 psa->fFeatures |= FADF_CREATEVECTOR;
271 psa->pvData = &psa[1]; /* Data follows the header */
272 psa->cbElements = ulSize;
273 psa->rgsabound[0].cElements = cElements;
274 psa->rgsabound[0].lLbound = lLbound;
276 switch (vt)
278 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
279 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
280 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
281 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
285 return psa;
288 /* Free data items in an array */
289 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
291 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
293 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
295 if (ulStartCell > ulCellCount) {
296 FIXME("unexpted ulcellcount %ld, start %ld\n",ulCellCount,ulStartCell);
297 return E_UNEXPECTED;
300 ulCellCount -= ulStartCell;
302 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
304 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell * psa->cbElements;
306 while(ulCellCount--)
308 if (*lpUnknown)
309 IUnknown_Release(*lpUnknown);
310 lpUnknown++;
313 else if (psa->fFeatures & (FADF_RECORD))
315 IRecordInfo *lpRecInfo;
317 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
319 PBYTE pRecordData = (PBYTE)psa->pvData;
320 while(ulCellCount--)
322 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
323 pRecordData += psa->cbElements;
325 IRecordInfo_Release(lpRecInfo);
328 else if (psa->fFeatures & FADF_BSTR)
330 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell * psa->cbElements;
332 while(ulCellCount--)
334 if (*lpBstr)
335 SysFreeString(*lpBstr);
336 lpBstr++;
339 else if (psa->fFeatures & FADF_VARIANT)
341 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell * psa->cbElements;
343 while(ulCellCount--)
345 HRESULT hRet = VariantClear(lpVariant);
347 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
348 lpVariant++;
352 return S_OK;
355 /* Copy data items from one array to another */
356 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
358 if (!psa->pvData)
359 return S_OK;
360 else if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
361 return E_INVALIDARG;
362 else
364 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
366 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
367 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
369 if (psa->fFeatures & FADF_VARIANT)
371 VARIANT* lpVariant = (VARIANT*)psa->pvData;
372 VARIANT* lpDest = (VARIANT*)dest->pvData;
374 while(ulCellCount--)
376 HRESULT hRet;
378 hRet = VariantCopy(lpDest, lpVariant);
379 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%lx\n", hRet);
380 lpVariant++;
381 lpDest++;
384 else if (psa->fFeatures & FADF_BSTR)
386 BSTR* lpBstr = (BSTR*)psa->pvData;
387 BSTR* lpDest = (BSTR*)dest->pvData;
389 while(ulCellCount--)
391 if (*lpBstr)
393 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
394 if (!*lpDest)
395 return E_OUTOFMEMORY;
397 else
398 *lpDest = NULL;
399 lpBstr++;
400 lpDest++;
403 else
405 /* Copy the data over */
406 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
408 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
410 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)dest->pvData;
412 while(ulCellCount--)
414 if (*lpUnknown)
415 IUnknown_AddRef(*lpUnknown);
416 lpUnknown++;
421 if (psa->fFeatures & FADF_RECORD)
423 IRecordInfo* pRecInfo = NULL;
425 SafeArrayGetRecordInfo(psa, &pRecInfo);
426 SafeArraySetRecordInfo(dest, pRecInfo);
428 if (pRecInfo)
430 /* Release because Get() adds a reference */
431 IRecordInfo_Release(pRecInfo);
434 else if (psa->fFeatures & FADF_HAVEIID)
436 GUID guid;
437 SafeArrayGetIID(psa, &guid);
438 SafeArraySetIID(dest, &guid);
440 else if (psa->fFeatures & FADF_HAVEVARTYPE)
442 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
445 return S_OK;
448 /*************************************************************************
449 * SafeArrayAllocDescriptor (OLEAUT32.36)
451 * Allocate and initialise a descriptor for a SafeArray.
453 * PARAMS
454 * cDims [I] Number of dimensions of the array
455 * ppsaOut [O] Destination for new descriptor
457 * RETURNS
458 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
459 * Failure: An HRESULT error code indicating the error.
461 * NOTES
462 * See SafeArray.
464 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
466 LONG allocSize;
468 TRACE("(%d,%p)\n", cDims, ppsaOut);
470 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
471 return E_INVALIDARG;
473 if (!ppsaOut)
474 return E_POINTER;
476 /* We need enough space for the header and its bounds */
477 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
479 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
480 return E_UNEXPECTED;
482 (*ppsaOut)->cDims = cDims;
484 TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims, allocSize);
485 return S_OK;
488 /*************************************************************************
489 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
491 * Allocate and initialise a descriptor for a SafeArray of a given type.
493 * PARAMS
494 * vt [I] The type of items to store in the array
495 * cDims [I] Number of dimensions of the array
496 * ppsaOut [O] Destination for new descriptor
498 * RETURNS
499 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
500 * Failure: An HRESULT error code indicating the error.
502 * NOTES
503 * - This function does not chack that vt is an allowed VARTYPE.
504 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
505 * See SafeArray.
507 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
509 ULONG cbElements;
510 HRESULT hRet = E_UNEXPECTED;
512 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
514 cbElements = SAFEARRAY_GetVTSize(vt);
515 if (!cbElements)
516 WARN("Creating a descriptor with an invalid VARTYPE!\n");
518 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
520 if (SUCCEEDED(hRet))
522 SAFEARRAY_SetFeatures(vt, *ppsaOut);
523 (*ppsaOut)->cbElements = cbElements;
525 return hRet;
528 /*************************************************************************
529 * SafeArrayAllocData (OLEAUT32.37)
531 * Allocate the data area of a SafeArray.
533 * PARAMS
534 * psa [I] SafeArray to allocate the data area of.
536 * RETURNS
537 * Success: S_OK. The data area is allocated and initialised.
538 * Failure: An HRESULT error code indicating the error.
540 * NOTES
541 * See SafeArray.
543 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
545 HRESULT hRet = E_INVALIDARG;
547 TRACE("(%p)\n", psa);
549 if (psa)
551 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
553 hRet = E_OUTOFMEMORY;
555 if (psa->cbElements)
557 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
559 if (psa->pvData)
561 hRet = S_OK;
562 TRACE("%lu bytes allocated for data at %p (%lu objects).\n",
563 ulSize * psa->cbElements, psa->pvData, ulSize);
567 return hRet;
570 /*************************************************************************
571 * SafeArrayCreate (OLEAUT32.15)
573 * Create a new SafeArray.
575 * PARAMS
576 * vt [I] Type to store in the safe array
577 * cDims [I] Number of array dimensions
578 * rgsabound [I] Bounds of the array dimensions
580 * RETURNS
581 * Success: A pointer to a new array object.
582 * Failure: NULL, if any parameter is invalid or memory allocation fails.
584 * NOTES
585 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
586 * in the Wine implementation.
587 * See SafeArray.
589 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
591 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
593 if (vt == VT_RECORD)
594 return NULL;
596 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
599 /*************************************************************************
600 * SafeArrayCreateEx (OLEAUT32.15)
602 * Create a new SafeArray.
604 * PARAMS
605 * vt [I] Type to store in the safe array
606 * cDims [I] Number of array dimensions
607 * rgsabound [I] Bounds of the array dimensions
608 * pvExtra [I] Extra data
610 * RETURNS
611 * Success: A pointer to a new array object.
612 * Failure: NULL, if any parameter is invalid or memory allocation fails.
614 * NOTES
615 * See SafeArray.
617 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
619 ULONG ulSize = 0;
620 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
621 SAFEARRAY* psa;
623 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
625 if (vt == VT_RECORD)
627 if (!iRecInfo)
628 return NULL;
629 IRecordInfo_GetSize(iRecInfo, &ulSize);
631 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
633 if (pvExtra)
635 switch(vt)
637 case VT_RECORD:
638 SafeArraySetRecordInfo(psa, pvExtra);
639 break;
640 case VT_UNKNOWN:
641 case VT_DISPATCH:
642 SafeArraySetIID(psa, pvExtra);
643 break;
646 return psa;
649 /************************************************************************
650 * SafeArrayCreateVector (OLEAUT32.411)
652 * Create a one dimensional, contiguous SafeArray.
654 * PARAMS
655 * vt [I] Type to store in the safe array
656 * lLbound [I] Lower bound of the array
657 * cElements [I] Number of elements in the array
659 * RETURNS
660 * Success: A pointer to a new array object.
661 * Failure: NULL, if any parameter is invalid or memory allocation fails.
663 * NOTES
664 * See SafeArray.
666 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
668 TRACE("(%d->%s,%ld,%ld\n", vt, debugstr_vt(vt), lLbound, cElements);
670 if (vt == VT_RECORD)
671 return NULL;
673 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
676 /************************************************************************
677 * SafeArrayCreateVectorEx (OLEAUT32.411)
679 * Create a one dimensional, contiguous SafeArray.
681 * PARAMS
682 * vt [I] Type to store in the safe array
683 * lLbound [I] Lower bound of the array
684 * cElements [I] Number of elements in the array
685 * pvExtra [I] Extra data
687 * RETURNS
688 * Success: A pointer to a new array object.
689 * Failure: NULL, if any parameter is invalid or memory allocation fails.
691 * NOTES
692 * See SafeArray.
694 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
696 ULONG ulSize;
697 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
698 SAFEARRAY* psa;
700 TRACE("(%d->%s,%ld,%ld,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
702 if (vt == VT_RECORD)
704 if (!iRecInfo)
705 return NULL;
706 IRecordInfo_GetSize(iRecInfo, &ulSize);
708 else
709 ulSize = SAFEARRAY_GetVTSize(vt);
711 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
713 if (pvExtra)
715 switch(vt)
717 case VT_RECORD:
718 SafeArraySetRecordInfo(psa, iRecInfo);
719 break;
720 case VT_UNKNOWN:
721 case VT_DISPATCH:
722 SafeArraySetIID(psa, pvExtra);
723 break;
726 return psa;
729 /*************************************************************************
730 * SafeArrayDestroyDescriptor (OLEAUT32.38)
732 * Destroy a SafeArray.
734 * PARAMS
735 * psa [I] SafeArray to destroy.
737 * RETURNS
738 * Success: S_OK. The resources used by the array are freed.
739 * Failure: An HRESULT error code indicating the error.
741 * NOTES
742 * See SafeArray.
744 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
746 TRACE("(%p)\n", psa);
748 if (psa)
750 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
752 if (psa->cLocks)
753 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
755 if (psa->fFeatures & FADF_RECORD)
756 SafeArraySetRecordInfo(psa, NULL);
758 if (psa->fFeatures & FADF_CREATEVECTOR &&
759 !(psa->fFeatures & FADF_DATADELETED))
760 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
762 if (!SAFEARRAY_Free(lpv))
763 return E_UNEXPECTED;
765 return S_OK;
768 /*************************************************************************
769 * SafeArrayLock (OLEAUT32.21)
771 * Increment the lock counter of a SafeArray.
773 * PARAMS
774 * psa [O] SafeArray to lock
776 * RETURNS
777 * Success: S_OK. The array lock is incremented.
778 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
779 * are held on the array at once.
781 * NOTES
782 * In Win32 these locks are not thread safe.
783 * See SafeArray.
785 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
787 ULONG ulLocks;
789 TRACE("(%p)\n", psa);
791 if (!psa)
792 return E_INVALIDARG;
794 ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
796 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
798 WARN("Out of locks!\n");
799 InterlockedDecrement( (LONG*) &psa->cLocks);
800 return E_UNEXPECTED;
802 return S_OK;
805 /*************************************************************************
806 * SafeArrayUnlock (OLEAUT32.22)
808 * Decrement the lock counter of a SafeArray.
810 * PARAMS
811 * psa [O] SafeArray to unlock
813 * RETURNS
814 * Success: S_OK. The array lock is decremented.
815 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
816 * held on the array.
818 * NOTES
819 * See SafeArray.
821 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
823 TRACE("(%p)\n", psa);
825 if (!psa)
826 return E_INVALIDARG;
828 if ((LONG)InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
830 WARN("Unlocked but no lock held!\n");
831 InterlockedIncrement( (LONG*) &psa->cLocks);
832 return E_UNEXPECTED;
834 return S_OK;
837 /*************************************************************************
838 * SafeArrayPutElement (OLEAUT32.26)
840 * Put an item into a SafeArray.
842 * PARAMS
843 * psa [I] SafeArray to insert into
844 * rgIndices [I] Indices to insert at
845 * pvData [I] Data to insert
847 * RETURNS
848 * Success: S_OK. The item is inserted
849 * Failure: An HRESULT error code indicating the error.
851 * NOTES
852 * See SafeArray.
854 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
856 HRESULT hRet;
858 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
860 if (!psa || !rgIndices)
861 return E_INVALIDARG;
863 if (!pvData)
865 ERR("Invalid pvData would crash under Win32!\n");
866 return E_INVALIDARG;
869 hRet = SafeArrayLock(psa);
871 if (SUCCEEDED(hRet))
873 PVOID lpvDest;
875 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
877 if (SUCCEEDED(hRet))
879 if (psa->fFeatures & FADF_VARIANT)
881 VARIANT* lpVariant = (VARIANT*)pvData;
882 VARIANT* lpDest = (VARIANT*)lpvDest;
884 hRet = VariantClear(lpDest);
885 if (FAILED(hRet)) FIXME("VariantClear failed with 0x%lx\n", hRet);
886 hRet = VariantCopy(lpDest, lpVariant);
887 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%lx\n", hRet);
889 else if (psa->fFeatures & FADF_BSTR)
891 BSTR lpBstr = (BSTR)pvData;
892 BSTR* lpDest = (BSTR*)lpvDest;
894 if (*lpDest)
895 SysFreeString(*lpDest);
897 if (lpBstr)
899 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
900 if (!*lpDest)
901 hRet = E_OUTOFMEMORY;
903 else
904 *lpDest = NULL;
906 else
908 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
910 LPUNKNOWN lpUnknown = (LPUNKNOWN)pvData;
911 LPUNKNOWN *lpDest = (LPUNKNOWN *)lpvDest;
913 if (lpUnknown)
914 IUnknown_AddRef(lpUnknown);
915 if (*lpDest)
916 IUnknown_Release(*lpDest);
917 *lpDest = lpUnknown;
918 } else {
919 /* Copy the data over */
920 memcpy(lpvDest, pvData, psa->cbElements);
924 SafeArrayUnlock(psa);
926 return hRet;
930 /*************************************************************************
931 * SafeArrayGetElement (OLEAUT32.25)
933 * Get an item from a SafeArray.
935 * PARAMS
936 * psa [I] SafeArray to get from
937 * rgIndices [I] Indices to get from
938 * pvData [O] Destination for data
940 * RETURNS
941 * Success: S_OK. The item data is returned in pvData.
942 * Failure: An HRESULT error code indicating the error.
944 * NOTES
945 * See SafeArray.
947 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
949 HRESULT hRet;
951 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
953 if (!psa || !rgIndices || !pvData)
954 return E_INVALIDARG;
956 hRet = SafeArrayLock(psa);
958 if (SUCCEEDED(hRet))
960 PVOID lpvSrc;
962 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
964 if (SUCCEEDED(hRet))
966 if (psa->fFeatures & FADF_VARIANT)
968 VARIANT* lpVariant = (VARIANT*)lpvSrc;
969 VARIANT* lpDest = (VARIANT*)pvData;
971 /* The original content of pvData is ignored. */
972 V_VT(lpDest) = VT_EMPTY;
973 hRet = VariantCopy(lpDest, lpVariant);
974 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%lx\n", hRet);
976 else if (psa->fFeatures & FADF_BSTR)
978 BSTR* lpBstr = (BSTR*)lpvSrc;
979 BSTR* lpDest = (BSTR*)pvData;
981 if (*lpBstr)
983 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
984 if (!*lpBstr)
985 hRet = E_OUTOFMEMORY;
987 else
988 *lpDest = NULL;
990 else
992 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
994 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)lpvSrc;
996 if (*lpUnknown)
997 IUnknown_AddRef(*lpUnknown);
999 /* Copy the data over */
1000 memcpy(pvData, lpvSrc, psa->cbElements);
1003 SafeArrayUnlock(psa);
1005 return hRet;
1008 /*************************************************************************
1009 * SafeArrayGetUBound (OLEAUT32.19)
1011 * Get the upper bound for a given SafeArray dimension
1013 * PARAMS
1014 * psa [I] Array to get dimension upper bound from
1015 * nDim [I] The dimension number to get the upper bound of
1016 * plUbound [O] Destination for the upper bound
1018 * RETURNS
1019 * Success: S_OK. plUbound contains the dimensions upper bound.
1020 * Failure: An HRESULT error code indicating the error.
1022 * NOTES
1023 * See SafeArray.
1025 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
1027 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1029 if (!psa || !plUbound)
1030 return E_INVALIDARG;
1032 if(!nDim || nDim > psa->cDims)
1033 return DISP_E_BADINDEX;
1035 *plUbound = psa->rgsabound[nDim - 1].lLbound +
1036 psa->rgsabound[nDim - 1].cElements - 1;
1038 return S_OK;
1041 /*************************************************************************
1042 * SafeArrayGetLBound (OLEAUT32.20)
1044 * Get the lower bound for a given SafeArray dimension
1046 * PARAMS
1047 * psa [I] Array to get dimension lower bound from
1048 * nDim [I] The dimension number to get the lowe bound of
1049 * plLbound [O] Destination for the lower bound
1051 * RETURNS
1052 * Success: S_OK. plUbound contains the dimensions lower bound.
1053 * Failure: An HRESULT error code indicating the error.
1055 * NOTES
1056 * See SafeArray.
1058 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1060 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1062 if (!psa || !plLbound)
1063 return E_INVALIDARG;
1065 if(!nDim || nDim > psa->cDims)
1066 return DISP_E_BADINDEX;
1068 *plLbound = psa->rgsabound[nDim - 1].lLbound;
1069 return S_OK;
1072 /*************************************************************************
1073 * SafeArrayGetDim (OLEAUT32.17)
1075 * Get the number of dimensions in a SafeArray.
1077 * PARAMS
1078 * psa [I] Array to get the dimensions of
1080 * RETURNS
1081 * The number of array dimensions in psa, or 0 if psa is NULL.
1083 * NOTES
1084 * See SafeArray.
1086 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1088 TRACE("(%p) returning %ld\n", psa, psa ? psa->cDims : 0ul);
1089 return psa ? psa->cDims : 0;
1092 /*************************************************************************
1093 * SafeArrayGetElemsize (OLEAUT32.18)
1095 * Get the size of an element in a SafeArray.
1097 * PARAMS
1098 * psa [I] Array to get the element size from
1100 * RETURNS
1101 * The size of a single element in psa, or 0 if psa is NULL.
1103 * NOTES
1104 * See SafeArray.
1106 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1108 TRACE("(%p) returning %ld\n", psa, psa ? psa->cbElements : 0ul);
1109 return psa ? psa->cbElements : 0;
1112 /*************************************************************************
1113 * SafeArrayAccessData (OLEAUT32.23)
1115 * Lock a SafeArray and return a pointer to its data.
1117 * PARAMS
1118 * psa [I] Array to get the data pointer from
1119 * ppvData [O] Destination for the arrays data pointer
1121 * RETURNS
1122 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1123 * is locked.
1124 * Failure: An HRESULT error code indicating the error.
1126 * NOTES
1127 * See SafeArray.
1129 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1131 TRACE("(%p,%p)\n", psa, ppvData);
1133 if(!psa || !ppvData)
1134 return E_INVALIDARG;
1136 if (SUCCEEDED(SafeArrayLock(psa)))
1138 *ppvData = psa->pvData;
1139 return S_OK;
1141 *ppvData = NULL;
1142 return E_UNEXPECTED;
1146 /*************************************************************************
1147 * SafeArrayUnaccessData (OLEAUT32.24)
1149 * Unlock a SafeArray after accessing its data.
1151 * PARAMS
1152 * psa [I] Array to unlock
1154 * RETURNS
1155 * Success: S_OK. The array is unlocked.
1156 * Failure: An HRESULT error code indicating the error.
1158 * NOTES
1159 * See SafeArray.
1161 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1163 TRACE("(%p)\n", psa);
1164 return SafeArrayUnlock(psa);
1167 /************************************************************************
1168 * SafeArrayPtrOfIndex (OLEAUT32.148)
1170 * Get the address of an item in a SafeArray.
1172 * PARAMS
1173 * psa [I] Array to get the items address from
1174 * rgIndices [I] Index of the item in the array
1175 * ppvData [O] Destination for item address
1177 * RETURNS
1178 * Success: S_OK. ppvData contains a pointer to the item.
1179 * Failure: An HRESULT error code indicating the error.
1181 * NOTES
1182 * This function does not lock the array.
1184 * NOTES
1185 * See SafeArray.
1187 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1189 USHORT dim;
1190 ULONG cell = 0, dimensionSize = 1;
1191 SAFEARRAYBOUND* psab;
1192 LONG c1;
1194 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1196 /* The general formula for locating the cell number of an entry in an n
1197 * dimensional array (where cn = coordinate in dimension dn) is:
1199 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1201 * We calculate the size of the last dimension at each step through the
1202 * dimensions to avoid recursing to calculate the last dimensions size.
1204 if (!psa || !rgIndices || !ppvData)
1205 return E_INVALIDARG;
1207 psab = psa->rgsabound;
1208 c1 = *rgIndices++;
1210 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1211 return DISP_E_BADINDEX; /* Initial index out of bounds */
1213 for (dim = 1; dim < psa->cDims; dim++)
1215 dimensionSize *= psab->cElements;
1217 psab++;
1219 if (!psab->cElements ||
1220 *rgIndices < psab->lLbound ||
1221 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1222 return DISP_E_BADINDEX; /* Index out of bounds */
1224 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1225 rgIndices++;
1228 cell += (c1 - psa->rgsabound[0].lLbound);
1230 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1231 return S_OK;
1234 /************************************************************************
1235 * SafeArrayDestroyData (OLEAUT32.39)
1237 * Destroy the data associated with a SafeArray.
1239 * PARAMS
1240 * psa [I] Array to delete the data from
1242 * RETURNS
1243 * Success: S_OK. All items and the item data are freed.
1244 * Failure: An HRESULT error code indicating the error.
1246 * NOTES
1247 * See SafeArray.
1249 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1251 TRACE("(%p)\n", psa);
1253 if (!psa)
1254 return E_INVALIDARG;
1256 if (psa->cLocks)
1257 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1259 /* If static, keep pvData and don't free */
1260 if (psa->pvData && !(psa->fFeatures & FADF_STATIC))
1262 /* Delete the actual item data */
1263 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1264 return E_UNEXPECTED;
1266 /* If this is not a vector, free the data memory block */
1267 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1269 if (!SAFEARRAY_Free(psa->pvData))
1270 return E_UNEXPECTED;
1271 psa->pvData = NULL;
1273 else
1274 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1277 return S_OK;
1280 /************************************************************************
1281 * SafeArrayCopyData (OLEAUT32.412)
1283 * Copy all data from one SafeArray to another.
1285 * PARAMS
1286 * psaSource [I] Source for copy
1287 * psaTarget [O] Destination for copy
1289 * RETURNS
1290 * Success: S_OK. psaTarget contains a copy of psaSource.
1291 * Failure: An HRESULT error code indicating the error.
1293 * NOTES
1294 * The two arrays must have the same number of dimensions and elements.
1296 * NOTES
1297 * See SafeArray.
1299 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1301 int dim;
1303 TRACE("(%p,%p)\n", psaSource, psaTarget);
1305 if (!psaSource || !psaTarget ||
1306 psaSource->cDims != psaTarget->cDims ||
1307 psaSource->cbElements != psaTarget->cbElements)
1308 return E_INVALIDARG;
1310 /* Each dimension must be the same size */
1311 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1312 if (psaSource->rgsabound[dim].cElements !=
1313 psaTarget->rgsabound[dim].cElements)
1314 return E_INVALIDARG;
1316 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1317 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1318 return S_OK;
1319 return E_UNEXPECTED;
1322 /************************************************************************
1323 * SafeArrayDestroy (OLEAUT32.16)
1325 * Destroy a SafeArray.
1327 * PARAMS
1328 * psa [I] Array to destroy
1330 * RETURNS
1331 * Success: S_OK. All resources used by the array are freed.
1332 * Failure: An HRESULT error code indicating the error.
1334 * NOTES
1335 * See SafeArray.
1337 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1339 TRACE("(%p)\n", psa);
1341 if(!psa)
1342 return S_OK;
1344 if(psa->cLocks > 0)
1345 return DISP_E_ARRAYISLOCKED;
1347 /* Native doesn't check to see if the free succeeds */
1348 SafeArrayDestroyData(psa);
1349 SafeArrayDestroyDescriptor(psa);
1350 return S_OK;
1353 /************************************************************************
1354 * SafeArrayCopy (OLEAUT32.27)
1356 * Make a duplicate of a SafeArray.
1358 * PARAMS
1359 * psa [I] Source for copy
1360 * ppsaOut [O] Destination for new copy
1362 * RETURNS
1363 * Success: S_OK. ppsaOut contains a copy of the array.
1364 * Failure: An HRESULT error code indicating the error.
1366 * NOTES
1367 * See SafeArray.
1369 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1371 HRESULT hRet;
1373 TRACE("(%p,%p)\n", psa, ppsaOut);
1375 if (!ppsaOut)
1376 return E_INVALIDARG;
1378 *ppsaOut = NULL;
1380 if (!psa)
1381 return S_OK; /* Handles copying of NULL arrays */
1383 if (!psa->cbElements)
1385 ERR("not copying an array of 0 elements\n");
1386 return E_INVALIDARG;
1389 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1391 VARTYPE vt;
1392 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1393 hRet = E_UNEXPECTED;
1394 else
1395 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1397 else
1399 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1400 if (SUCCEEDED(hRet))
1402 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1403 (*ppsaOut)->cbElements = psa->cbElements;
1407 if (SUCCEEDED(hRet))
1409 /* Copy dimension bounds */
1410 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1412 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1414 if ((*ppsaOut)->pvData)
1416 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1418 if (SUCCEEDED(hRet))
1419 return hRet;
1421 SAFEARRAY_Free((*ppsaOut)->pvData);
1423 SafeArrayDestroyDescriptor(*ppsaOut);
1425 *ppsaOut = NULL;
1426 return hRet;
1429 /************************************************************************
1430 * SafeArrayRedim (OLEAUT32.40)
1432 * Changes the characteristics of the last dimension of a SafeArray
1434 * PARAMS
1435 * psa [I] Array to change
1436 * psabound [I] New bound details for the last dimension
1438 * RETURNS
1439 * Success: S_OK. psa is updated to reflect the new bounds.
1440 * Failure: An HRESULT error code indicating the error.
1442 * NOTES
1443 * See SafeArray.
1445 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1447 SAFEARRAYBOUND *oldBounds;
1449 TRACE("(%p,%p)\n", psa, psabound);
1451 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1452 return E_INVALIDARG;
1454 if (psa->cLocks > 0)
1455 return DISP_E_ARRAYISLOCKED;
1457 if (FAILED(SafeArrayLock(psa)))
1458 return E_UNEXPECTED;
1460 oldBounds = &psa->rgsabound[psa->cDims - 1];
1461 oldBounds->lLbound = psabound->lLbound;
1463 if (psabound->cElements != oldBounds->cElements)
1465 if (psabound->cElements < oldBounds->cElements)
1467 /* Shorten the final dimension. */
1468 ULONG ulStartCell = psa->cDims == 1 ? 0 : SAFEARRAY_GetDimensionCells(psa, psa->cDims - 1);
1470 ulStartCell += psabound->cElements;
1471 SAFEARRAY_DestroyData(psa, ulStartCell);
1473 else
1475 /* Lengthen the final dimension */
1476 ULONG ulOldSize, ulNewSize;
1477 PVOID pvNewData;
1479 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1480 if (ulOldSize)
1481 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1482 else {
1483 int oldelems = oldBounds->cElements;
1484 oldBounds->cElements = psabound->cElements;
1485 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1486 oldBounds->cElements = oldelems;
1489 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1491 SafeArrayUnlock(psa);
1492 return E_UNEXPECTED;
1495 memcpy(pvNewData, psa->pvData, ulOldSize);
1496 SAFEARRAY_Free(psa->pvData);
1497 psa->pvData = pvNewData;
1499 oldBounds->cElements = psabound->cElements;
1502 SafeArrayUnlock(psa);
1503 return S_OK;
1506 /************************************************************************
1507 * SafeArrayGetVartype (OLEAUT32.77)
1509 * Get the type of the items in a SafeArray.
1511 * PARAMS
1512 * psa [I] Array to get the type from
1513 * pvt [O] Destination for the type
1515 * RETURNS
1516 * Success: S_OK. pvt contains the type of the items.
1517 * Failure: An HRESULT error code indicating the error.
1519 * NOTES
1520 * See SafeArray.
1522 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1524 TRACE("(%p,%p)\n", psa, pvt);
1526 if (!psa || !pvt)
1527 return E_INVALIDARG;
1529 if (psa->fFeatures & FADF_RECORD)
1530 *pvt = VT_RECORD;
1531 else if (psa->fFeatures & FADF_HAVEIID)
1532 *pvt = VT_UNKNOWN;
1533 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1535 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1536 *pvt = vt;
1538 else
1539 return E_INVALIDARG;
1541 return S_OK;
1544 /************************************************************************
1545 * SafeArraySetRecordInfo (OLEAUT32.@)
1547 * Set the record info for a SafeArray.
1549 * PARAMS
1550 * psa [I] Array to set the record info for
1551 * pRinfo [I] Record info
1553 * RETURNS
1554 * Success: S_OK. The record info is stored with the array.
1555 * Failure: An HRESULT error code indicating the error.
1557 * NOTES
1558 * See SafeArray.
1560 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1562 IRecordInfo** dest = (IRecordInfo**)psa;
1564 TRACE("(%p,%p)\n", psa, pRinfo);
1566 if (!psa || !(psa->fFeatures & FADF_RECORD))
1567 return E_INVALIDARG;
1569 if (pRinfo)
1570 IRecordInfo_AddRef(pRinfo);
1572 if (dest[-1])
1573 IRecordInfo_Release(dest[-1]);
1575 dest[-1] = pRinfo;
1576 return S_OK;
1579 /************************************************************************
1580 * SafeArrayGetRecordInfo (OLEAUT32.@)
1582 * Get the record info from a SafeArray.
1584 * PARAMS
1585 * psa [I] Array to get the record info from
1586 * pRinfo [O] Destination for the record info
1588 * RETURNS
1589 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1590 * Failure: An HRESULT error code indicating the error.
1592 * NOTES
1593 * See SafeArray.
1595 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1597 IRecordInfo** src = (IRecordInfo**)psa;
1599 TRACE("(%p,%p)\n", psa, pRinfo);
1601 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1602 return E_INVALIDARG;
1604 *pRinfo = src[-1];
1606 if (*pRinfo)
1607 IRecordInfo_AddRef(*pRinfo);
1608 return S_OK;
1611 /************************************************************************
1612 * SafeArraySetIID (OLEAUT32.@)
1614 * Set the IID for a SafeArray.
1616 * PARAMS
1617 * psa [I] Array to set the IID from
1618 * guid [I] IID
1620 * RETURNS
1621 * Success: S_OK. The IID is stored with the array
1622 * Failure: An HRESULT error code indicating the error.
1624 * NOTES
1625 * See SafeArray.
1627 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1629 GUID* dest = (GUID*)psa;
1631 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1633 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1634 return E_INVALIDARG;
1636 dest[-1] = *guid;
1637 return S_OK;
1640 /************************************************************************
1641 * SafeArrayGetIID (OLEAUT32.@)
1643 * Get the IID from a SafeArray.
1645 * PARAMS
1646 * psa [I] Array to get the ID from
1647 * pGuid [O] Destination for the IID
1649 * RETURNS
1650 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1651 * Failure: An HRESULT error code indicating the error.
1653 * NOTES
1654 * See SafeArray.
1656 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1658 GUID* src = (GUID*)psa;
1660 TRACE("(%p,%p)\n", psa, pGuid);
1662 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1663 return E_INVALIDARG;
1665 *pGuid = src[-1];
1666 return S_OK;
1669 /************************************************************************
1670 * VectorFromBstr (OLEAUT32.@)
1672 * Create a SafeArray Vector from the bytes of a BSTR.
1674 * PARAMS
1675 * bstr [I] String to get bytes from
1676 * ppsa [O] Destination for the array
1678 * RETURNS
1679 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1680 * Failure: An HRESULT error code indicating the error.
1682 * NOTES
1683 * See SafeArray.
1685 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1687 SAFEARRAYBOUND sab;
1689 TRACE("(%p,%p)\n", bstr, ppsa);
1691 if (!ppsa)
1692 return E_INVALIDARG;
1694 sab.lLbound = 0;
1695 sab.cElements = SysStringByteLen(bstr);
1697 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1699 if (*ppsa)
1701 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1702 return S_OK;
1704 return E_OUTOFMEMORY;
1707 /************************************************************************
1708 * BstrFromVector (OLEAUT32.@)
1710 * Create a BSTR from a SafeArray.
1712 * PARAMS
1713 * psa [I] Source array
1714 * pbstr [O] Destination for output BSTR
1716 * RETURNS
1717 * Success: S_OK. pbstr contains the arrays data.
1718 * Failure: An HRESULT error code indicating the error.
1720 * NOTES
1721 * psa must be a 1 dimensional array of a 1 byte type.
1723 * NOTES
1724 * See SafeArray.
1726 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1728 TRACE("(%p,%p)\n", psa, pbstr);
1730 if (!pbstr)
1731 return E_INVALIDARG;
1733 *pbstr = NULL;
1735 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1736 return E_INVALIDARG;
1738 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1739 if (!*pbstr)
1740 return E_OUTOFMEMORY;
1741 return S_OK;