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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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))
31 * 0x10: SAFEARRAYBOUNDS[0...]
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(variant);
50 /************************************************************************
51 * SafeArray {OLEAUT32}
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
71 * The following types of data can be stored within a SafeArray.
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
76 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
78 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
85 /* Undocumented hidden space before the start of a SafeArray descriptor */
86 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
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);
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)
109 case VT_UI1: return sizeof(BYTE);
112 case VT_UI2: return sizeof(SHORT);
116 case VT_ERROR: return sizeof(LONG);
119 case VT_UI8: return sizeof(LONG64);
121 case VT_UINT: return sizeof(INT);
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;
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;
147 /* Get the hidden data from an array */
148 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
150 LPDWORD lpDw = (LPDWORD)psa;
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;
163 /* This is a valid bordercase. See testcases. -Marcus */
164 if (!psab->cElements)
166 ulNumCells *= psab->cElements;
172 /* Allocate a descriptor for an array */
173 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
175 *ppsaOut = (SAFEARRAY*)((char*)SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE) + SAFEARRAY_HIDDEN_SIZE);
183 /* Set the features of an array */
184 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
186 /* Set the IID if we have one, otherwise set the type */
187 if (vt == VT_DISPATCH)
189 psa->fFeatures = FADF_HAVEIID;
190 SafeArraySetIID(psa, &IID_IDispatch);
192 else if (vt == VT_UNKNOWN)
194 psa->fFeatures = FADF_HAVEIID;
195 SafeArraySetIID(psa, &IID_IUnknown);
197 else if (vt == VT_RECORD)
198 psa->fFeatures = FADF_RECORD;
201 psa->fFeatures = FADF_HAVEVARTYPE;
202 SAFEARRAY_SetHiddenDWORD(psa, vt);
206 /* Create an array */
207 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, ULONG ulSize)
209 SAFEARRAY *psa = NULL;
215 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
219 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
220 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
221 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
222 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
225 for (i = 0; i < cDims; i++)
226 memcpy(psa->rgsabound + i, rgsabound + cDims - 1 - i, sizeof(SAFEARRAYBOUND));
229 psa->cbElements = ulSize;
231 if (FAILED(SafeArrayAllocData(psa)))
233 SafeArrayDestroyDescriptor(psa);
240 /* Create an array as a vector */
241 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
243 SAFEARRAY *psa = NULL;
245 if (ulSize || (vt == VT_RECORD))
247 /* Allocate the header and data together */
248 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
250 SAFEARRAY_SetFeatures(vt, psa);
253 psa->fFeatures |= FADF_CREATEVECTOR;
254 psa->pvData = &psa[1]; /* Data follows the header */
255 psa->cbElements = ulSize;
256 psa->rgsabound[0].cElements = cElements;
257 psa->rgsabound[0].lLbound = lLbound;
261 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
262 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
263 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
264 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
271 /* Free data items in an array */
272 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
274 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
276 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
278 if (ulStartCell > ulCellCount) {
279 FIXME("unexpted ulcellcount %d, start %d\n",ulCellCount,ulStartCell);
283 ulCellCount -= ulStartCell;
285 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
287 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell;
292 IUnknown_Release(*lpUnknown);
296 else if (psa->fFeatures & (FADF_RECORD))
298 IRecordInfo *lpRecInfo;
300 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
302 PBYTE pRecordData = (PBYTE)psa->pvData;
305 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
306 pRecordData += psa->cbElements;
308 IRecordInfo_Release(lpRecInfo);
311 else if (psa->fFeatures & FADF_BSTR)
313 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell;
318 SysFreeString(*lpBstr);
322 else if (psa->fFeatures & FADF_VARIANT)
324 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell;
328 HRESULT hRet = VariantClear(lpVariant);
330 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
338 /* Copy data items from one array to another */
339 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
343 else if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
347 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
349 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
350 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
352 if (psa->fFeatures & FADF_VARIANT)
354 VARIANT* lpVariant = (VARIANT*)psa->pvData;
355 VARIANT* lpDest = (VARIANT*)dest->pvData;
361 hRet = VariantCopy(lpDest, lpVariant);
362 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
367 else if (psa->fFeatures & FADF_BSTR)
369 BSTR* lpBstr = (BSTR*)psa->pvData;
370 BSTR* lpDest = (BSTR*)dest->pvData;
376 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
378 return E_OUTOFMEMORY;
388 /* Copy the data over */
389 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
391 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
393 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)dest->pvData;
398 IUnknown_AddRef(*lpUnknown);
404 if (psa->fFeatures & FADF_RECORD)
406 IRecordInfo* pRecInfo = NULL;
408 SafeArrayGetRecordInfo(psa, &pRecInfo);
409 SafeArraySetRecordInfo(dest, pRecInfo);
413 /* Release because Get() adds a reference */
414 IRecordInfo_Release(pRecInfo);
417 else if (psa->fFeatures & FADF_HAVEIID)
420 SafeArrayGetIID(psa, &guid);
421 SafeArraySetIID(dest, &guid);
423 else if (psa->fFeatures & FADF_HAVEVARTYPE)
425 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
431 /*************************************************************************
432 * SafeArrayAllocDescriptor (OLEAUT32.36)
434 * Allocate and initialise a descriptor for a SafeArray.
437 * cDims [I] Number of dimensions of the array
438 * ppsaOut [O] Destination for new descriptor
441 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
442 * Failure: An HRESULT error code indicating the error.
447 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
451 TRACE("(%d,%p)\n", cDims, ppsaOut);
453 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
459 /* We need enough space for the header and its bounds */
460 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
462 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
465 (*ppsaOut)->cDims = cDims;
467 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims, allocSize);
471 /*************************************************************************
472 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
474 * Allocate and initialise a descriptor for a SafeArray of a given type.
477 * vt [I] The type of items to store in the array
478 * cDims [I] Number of dimensions of the array
479 * ppsaOut [O] Destination for new descriptor
482 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
483 * Failure: An HRESULT error code indicating the error.
486 * - This function does not chack that vt is an allowed VARTYPE.
487 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
490 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
493 HRESULT hRet = E_UNEXPECTED;
495 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
497 cbElements = SAFEARRAY_GetVTSize(vt);
499 WARN("Creating a descriptor with an invalid VARTYPE!\n");
501 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
505 SAFEARRAY_SetFeatures(vt, *ppsaOut);
506 (*ppsaOut)->cbElements = cbElements;
511 /*************************************************************************
512 * SafeArrayAllocData (OLEAUT32.37)
514 * Allocate the data area of a SafeArray.
517 * psa [I] SafeArray to allocate the data area of.
520 * Success: S_OK. The data area is allocated and initialised.
521 * Failure: An HRESULT error code indicating the error.
526 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
528 HRESULT hRet = E_INVALIDARG;
530 TRACE("(%p)\n", psa);
534 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
536 hRet = E_OUTOFMEMORY;
540 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
545 TRACE("%u bytes allocated for data at %p (%u objects).\n",
546 ulSize * psa->cbElements, psa->pvData, ulSize);
553 /*************************************************************************
554 * SafeArrayCreate (OLEAUT32.15)
556 * Create a new SafeArray.
559 * vt [I] Type to store in the safe array
560 * cDims [I] Number of array dimensions
561 * rgsabound [I] Bounds of the array dimensions
564 * Success: A pointer to a new array object.
565 * Failure: NULL, if any parameter is invalid or memory allocation fails.
568 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
569 * in the Wine implementation.
572 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
574 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
579 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
582 /*************************************************************************
583 * SafeArrayCreateEx (OLEAUT32.15)
585 * Create a new SafeArray.
588 * vt [I] Type to store in the safe array
589 * cDims [I] Number of array dimensions
590 * rgsabound [I] Bounds of the array dimensions
591 * pvExtra [I] Extra data
594 * Success: A pointer to a new array object.
595 * Failure: NULL, if any parameter is invalid or memory allocation fails.
600 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
603 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
606 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
612 IRecordInfo_GetSize(iRecInfo, &ulSize);
614 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
621 SafeArraySetRecordInfo(psa, pvExtra);
625 SafeArraySetIID(psa, pvExtra);
632 /************************************************************************
633 * SafeArrayCreateVector (OLEAUT32.411)
635 * Create a one dimensional, contiguous SafeArray.
638 * vt [I] Type to store in the safe array
639 * lLbound [I] Lower bound of the array
640 * cElements [I] Number of elements in the array
643 * Success: A pointer to a new array object.
644 * Failure: NULL, if any parameter is invalid or memory allocation fails.
649 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
651 TRACE("(%d->%s,%d,%d\n", vt, debugstr_vt(vt), lLbound, cElements);
656 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
659 /************************************************************************
660 * SafeArrayCreateVectorEx (OLEAUT32.411)
662 * Create a one dimensional, contiguous SafeArray.
665 * vt [I] Type to store in the safe array
666 * lLbound [I] Lower bound of the array
667 * cElements [I] Number of elements in the array
668 * pvExtra [I] Extra data
671 * Success: A pointer to a new array object.
672 * Failure: NULL, if any parameter is invalid or memory allocation fails.
677 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
680 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
683 TRACE("(%d->%s,%d,%d,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
689 IRecordInfo_GetSize(iRecInfo, &ulSize);
692 ulSize = SAFEARRAY_GetVTSize(vt);
694 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
701 SafeArraySetRecordInfo(psa, iRecInfo);
705 SafeArraySetIID(psa, pvExtra);
712 /*************************************************************************
713 * SafeArrayDestroyDescriptor (OLEAUT32.38)
715 * Destroy a SafeArray.
718 * psa [I] SafeArray to destroy.
721 * Success: S_OK. The resources used by the array are freed.
722 * Failure: An HRESULT error code indicating the error.
727 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
729 TRACE("(%p)\n", psa);
733 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
736 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
738 if (psa->fFeatures & FADF_RECORD)
739 SafeArraySetRecordInfo(psa, NULL);
741 if (psa->fFeatures & FADF_CREATEVECTOR &&
742 !(psa->fFeatures & FADF_DATADELETED))
743 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
745 if (!SAFEARRAY_Free(lpv))
751 /*************************************************************************
752 * SafeArrayLock (OLEAUT32.21)
754 * Increment the lock counter of a SafeArray.
757 * psa [O] SafeArray to lock
760 * Success: S_OK. The array lock is incremented.
761 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
762 * are held on the array at once.
765 * In Win32 these locks are not thread safe.
768 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
772 TRACE("(%p)\n", psa);
777 ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
779 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
781 WARN("Out of locks!\n");
782 InterlockedDecrement( (LONG*) &psa->cLocks);
788 /*************************************************************************
789 * SafeArrayUnlock (OLEAUT32.22)
791 * Decrement the lock counter of a SafeArray.
794 * psa [O] SafeArray to unlock
797 * Success: S_OK. The array lock is decremented.
798 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
804 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
806 TRACE("(%p)\n", psa);
811 if ((LONG)InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
813 WARN("Unlocked but no lock held!\n");
814 InterlockedIncrement( (LONG*) &psa->cLocks);
820 /*************************************************************************
821 * SafeArrayPutElement (OLEAUT32.26)
823 * Put an item into a SafeArray.
826 * psa [I] SafeArray to insert into
827 * rgIndices [I] Indices to insert at
828 * pvData [I] Data to insert
831 * Success: S_OK. The item is inserted
832 * Failure: An HRESULT error code indicating the error.
837 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
841 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
843 if (!psa || !rgIndices)
846 hRet = SafeArrayLock(psa);
852 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
856 if (psa->fFeatures & FADF_VARIANT)
858 VARIANT* lpVariant = (VARIANT*)pvData;
859 VARIANT* lpDest = (VARIANT*)lpvDest;
861 hRet = VariantClear(lpDest);
862 if (FAILED(hRet)) FIXME("VariantClear failed with 0x%x\n", hRet);
863 hRet = VariantCopy(lpDest, lpVariant);
864 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
866 else if (psa->fFeatures & FADF_BSTR)
868 BSTR lpBstr = (BSTR)pvData;
869 BSTR* lpDest = (BSTR*)lpvDest;
872 SysFreeString(*lpDest);
874 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
876 hRet = E_OUTOFMEMORY;
880 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
882 LPUNKNOWN lpUnknown = (LPUNKNOWN)pvData;
883 LPUNKNOWN *lpDest = (LPUNKNOWN *)lpvDest;
886 IUnknown_AddRef(lpUnknown);
888 IUnknown_Release(*lpDest);
891 /* Copy the data over */
892 memcpy(lpvDest, pvData, psa->cbElements);
896 SafeArrayUnlock(psa);
902 /*************************************************************************
903 * SafeArrayGetElement (OLEAUT32.25)
905 * Get an item from a SafeArray.
908 * psa [I] SafeArray to get from
909 * rgIndices [I] Indices to get from
910 * pvData [O] Destination for data
913 * Success: S_OK. The item data is returned in pvData.
914 * Failure: An HRESULT error code indicating the error.
919 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
923 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
925 if (!psa || !rgIndices || !pvData)
928 hRet = SafeArrayLock(psa);
934 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
938 if (psa->fFeatures & FADF_VARIANT)
940 VARIANT* lpVariant = (VARIANT*)lpvSrc;
941 VARIANT* lpDest = (VARIANT*)pvData;
943 /* The original content of pvData is ignored. */
944 V_VT(lpDest) = VT_EMPTY;
945 hRet = VariantCopy(lpDest, lpVariant);
946 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
948 else if (psa->fFeatures & FADF_BSTR)
950 BSTR* lpBstr = (BSTR*)lpvSrc;
951 BSTR* lpDest = (BSTR*)pvData;
955 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
957 hRet = E_OUTOFMEMORY;
964 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
966 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)lpvSrc;
969 IUnknown_AddRef(*lpUnknown);
971 /* Copy the data over */
972 memcpy(pvData, lpvSrc, psa->cbElements);
975 SafeArrayUnlock(psa);
980 /*************************************************************************
981 * SafeArrayGetUBound (OLEAUT32.19)
983 * Get the upper bound for a given SafeArray dimension
986 * psa [I] Array to get dimension upper bound from
987 * nDim [I] The dimension number to get the upper bound of
988 * plUbound [O] Destination for the upper bound
991 * Success: S_OK. plUbound contains the dimensions upper bound.
992 * Failure: An HRESULT error code indicating the error.
997 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
999 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1001 if (!psa || !plUbound)
1002 return E_INVALIDARG;
1004 if(!nDim || nDim > psa->cDims)
1005 return DISP_E_BADINDEX;
1007 *plUbound = psa->rgsabound[psa->cDims - nDim].lLbound +
1008 psa->rgsabound[psa->cDims - nDim].cElements - 1;
1013 /*************************************************************************
1014 * SafeArrayGetLBound (OLEAUT32.20)
1016 * Get the lower bound for a given SafeArray dimension
1019 * psa [I] Array to get dimension lower bound from
1020 * nDim [I] The dimension number to get the lowe bound of
1021 * plLbound [O] Destination for the lower bound
1024 * Success: S_OK. plUbound contains the dimensions lower bound.
1025 * Failure: An HRESULT error code indicating the error.
1030 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1032 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1034 if (!psa || !plLbound)
1035 return E_INVALIDARG;
1037 if(!nDim || nDim > psa->cDims)
1038 return DISP_E_BADINDEX;
1040 *plLbound = psa->rgsabound[psa->cDims - nDim].lLbound;
1044 /*************************************************************************
1045 * SafeArrayGetDim (OLEAUT32.17)
1047 * Get the number of dimensions in a SafeArray.
1050 * psa [I] Array to get the dimensions of
1053 * The number of array dimensions in psa, or 0 if psa is NULL.
1058 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1060 TRACE("(%p) returning %d\n", psa, psa ? psa->cDims : 0u);
1061 return psa ? psa->cDims : 0;
1064 /*************************************************************************
1065 * SafeArrayGetElemsize (OLEAUT32.18)
1067 * Get the size of an element in a SafeArray.
1070 * psa [I] Array to get the element size from
1073 * The size of a single element in psa, or 0 if psa is NULL.
1078 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1080 TRACE("(%p) returning %d\n", psa, psa ? psa->cbElements : 0u);
1081 return psa ? psa->cbElements : 0;
1084 /*************************************************************************
1085 * SafeArrayAccessData (OLEAUT32.23)
1087 * Lock a SafeArray and return a pointer to its data.
1090 * psa [I] Array to get the data pointer from
1091 * ppvData [O] Destination for the arrays data pointer
1094 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1096 * Failure: An HRESULT error code indicating the error.
1101 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1103 TRACE("(%p,%p)\n", psa, ppvData);
1105 if(!psa || !ppvData)
1106 return E_INVALIDARG;
1108 if (SUCCEEDED(SafeArrayLock(psa)))
1110 *ppvData = psa->pvData;
1114 return E_UNEXPECTED;
1118 /*************************************************************************
1119 * SafeArrayUnaccessData (OLEAUT32.24)
1121 * Unlock a SafeArray after accessing its data.
1124 * psa [I] Array to unlock
1127 * Success: S_OK. The array is unlocked.
1128 * Failure: An HRESULT error code indicating the error.
1133 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1135 TRACE("(%p)\n", psa);
1136 return SafeArrayUnlock(psa);
1139 /************************************************************************
1140 * SafeArrayPtrOfIndex (OLEAUT32.148)
1142 * Get the address of an item in a SafeArray.
1145 * psa [I] Array to get the items address from
1146 * rgIndices [I] Index of the item in the array
1147 * ppvData [O] Destination for item address
1150 * Success: S_OK. ppvData contains a pointer to the item.
1151 * Failure: An HRESULT error code indicating the error.
1154 * This function does not lock the array.
1159 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1162 ULONG cell = 0, dimensionSize = 1;
1163 SAFEARRAYBOUND* psab;
1166 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1168 /* The general formula for locating the cell number of an entry in an n
1169 * dimensional array (where cn = coordinate in dimension dn) is:
1171 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1173 * We calculate the size of the last dimension at each step through the
1174 * dimensions to avoid recursing to calculate the last dimensions size.
1176 if (!psa || !rgIndices || !ppvData)
1177 return E_INVALIDARG;
1179 psab = psa->rgsabound + psa->cDims - 1;
1182 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1183 return DISP_E_BADINDEX; /* Initial index out of bounds */
1185 for (dim = 1; dim < psa->cDims; dim++)
1187 dimensionSize *= psab->cElements;
1191 if (!psab->cElements ||
1192 *rgIndices < psab->lLbound ||
1193 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1194 return DISP_E_BADINDEX; /* Index out of bounds */
1196 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1200 cell += (c1 - psa->rgsabound[psa->cDims - 1].lLbound);
1202 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1206 /************************************************************************
1207 * SafeArrayDestroyData (OLEAUT32.39)
1209 * Destroy the data associated with a SafeArray.
1212 * psa [I] Array to delete the data from
1215 * Success: S_OK. All items and the item data are freed.
1216 * Failure: An HRESULT error code indicating the error.
1221 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1223 TRACE("(%p)\n", psa);
1226 return E_INVALIDARG;
1229 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1231 /* Delete the actual item data */
1232 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1233 return E_UNEXPECTED;
1237 if (psa->fFeatures & FADF_STATIC)
1239 ZeroMemory(psa->pvData, SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1242 /* If this is not a vector, free the data memory block */
1243 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1245 if (!SAFEARRAY_Free(psa->pvData))
1246 return E_UNEXPECTED;
1250 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1256 /************************************************************************
1257 * SafeArrayCopyData (OLEAUT32.412)
1259 * Copy all data from one SafeArray to another.
1262 * psaSource [I] Source for copy
1263 * psaTarget [O] Destination for copy
1266 * Success: S_OK. psaTarget contains a copy of psaSource.
1267 * Failure: An HRESULT error code indicating the error.
1270 * The two arrays must have the same number of dimensions and elements.
1275 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1279 TRACE("(%p,%p)\n", psaSource, psaTarget);
1281 if (!psaSource || !psaTarget ||
1282 psaSource->cDims != psaTarget->cDims ||
1283 psaSource->cbElements != psaTarget->cbElements)
1284 return E_INVALIDARG;
1286 /* Each dimension must be the same size */
1287 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1288 if (psaSource->rgsabound[dim].cElements !=
1289 psaTarget->rgsabound[dim].cElements)
1290 return E_INVALIDARG;
1292 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1293 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1295 return E_UNEXPECTED;
1298 /************************************************************************
1299 * SafeArrayDestroy (OLEAUT32.16)
1301 * Destroy a SafeArray.
1304 * psa [I] Array to destroy
1307 * Success: S_OK. All resources used by the array are freed.
1308 * Failure: An HRESULT error code indicating the error.
1313 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1315 TRACE("(%p)\n", psa);
1321 return DISP_E_ARRAYISLOCKED;
1323 /* Native doesn't check to see if the free succeeds */
1324 SafeArrayDestroyData(psa);
1325 SafeArrayDestroyDescriptor(psa);
1329 /************************************************************************
1330 * SafeArrayCopy (OLEAUT32.27)
1332 * Make a duplicate of a SafeArray.
1335 * psa [I] Source for copy
1336 * ppsaOut [O] Destination for new copy
1339 * Success: S_OK. ppsaOut contains a copy of the array.
1340 * Failure: An HRESULT error code indicating the error.
1345 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1349 TRACE("(%p,%p)\n", psa, ppsaOut);
1352 return E_INVALIDARG;
1357 return S_OK; /* Handles copying of NULL arrays */
1359 if (!psa->cbElements)
1361 ERR("not copying an array of 0 elements\n");
1362 return E_INVALIDARG;
1365 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1368 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1369 hRet = E_UNEXPECTED;
1371 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1375 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1376 if (SUCCEEDED(hRet))
1378 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1379 (*ppsaOut)->cbElements = psa->cbElements;
1383 if (SUCCEEDED(hRet))
1385 /* Copy dimension bounds */
1386 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1388 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1390 if ((*ppsaOut)->pvData)
1392 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1394 if (SUCCEEDED(hRet))
1397 SAFEARRAY_Free((*ppsaOut)->pvData);
1399 SafeArrayDestroyDescriptor(*ppsaOut);
1405 /************************************************************************
1406 * SafeArrayRedim (OLEAUT32.40)
1408 * Changes the characteristics of the last dimension of a SafeArray
1411 * psa [I] Array to change
1412 * psabound [I] New bound details for the last dimension
1415 * Success: S_OK. psa is updated to reflect the new bounds.
1416 * Failure: An HRESULT error code indicating the error.
1421 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1423 SAFEARRAYBOUND *oldBounds;
1425 TRACE("(%p,%p)\n", psa, psabound);
1427 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1428 return E_INVALIDARG;
1430 if (psa->cLocks > 0)
1431 return DISP_E_ARRAYISLOCKED;
1433 if (FAILED(SafeArrayLock(psa)))
1434 return E_UNEXPECTED;
1436 oldBounds = psa->rgsabound;
1437 oldBounds->lLbound = psabound->lLbound;
1439 if (psabound->cElements != oldBounds->cElements)
1441 if (psabound->cElements < oldBounds->cElements)
1443 /* Shorten the final dimension. */
1444 ULONG ulStartCell = psabound->cElements *
1445 (SAFEARRAY_GetCellCount(psa) / oldBounds->cElements);
1446 SAFEARRAY_DestroyData(psa, ulStartCell);
1450 /* Lengthen the final dimension */
1451 ULONG ulOldSize, ulNewSize;
1454 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1456 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1458 int oldelems = oldBounds->cElements;
1459 oldBounds->cElements = psabound->cElements;
1460 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1461 oldBounds->cElements = oldelems;
1464 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1466 SafeArrayUnlock(psa);
1467 return E_UNEXPECTED;
1470 memcpy(pvNewData, psa->pvData, ulOldSize);
1471 SAFEARRAY_Free(psa->pvData);
1472 psa->pvData = pvNewData;
1474 oldBounds->cElements = psabound->cElements;
1477 SafeArrayUnlock(psa);
1481 /************************************************************************
1482 * SafeArrayGetVartype (OLEAUT32.77)
1484 * Get the type of the items in a SafeArray.
1487 * psa [I] Array to get the type from
1488 * pvt [O] Destination for the type
1491 * Success: S_OK. pvt contains the type of the items.
1492 * Failure: An HRESULT error code indicating the error.
1497 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1499 TRACE("(%p,%p)\n", psa, pvt);
1502 return E_INVALIDARG;
1504 if (psa->fFeatures & FADF_RECORD)
1506 else if (psa->fFeatures & FADF_HAVEIID)
1508 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1510 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1514 return E_INVALIDARG;
1519 /************************************************************************
1520 * SafeArraySetRecordInfo (OLEAUT32.@)
1522 * Set the record info for a SafeArray.
1525 * psa [I] Array to set the record info for
1526 * pRinfo [I] Record info
1529 * Success: S_OK. The record info is stored with the array.
1530 * Failure: An HRESULT error code indicating the error.
1535 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1537 IRecordInfo** dest = (IRecordInfo**)psa;
1539 TRACE("(%p,%p)\n", psa, pRinfo);
1541 if (!psa || !(psa->fFeatures & FADF_RECORD))
1542 return E_INVALIDARG;
1545 IRecordInfo_AddRef(pRinfo);
1548 IRecordInfo_Release(dest[-1]);
1554 /************************************************************************
1555 * SafeArrayGetRecordInfo (OLEAUT32.@)
1557 * Get the record info from a SafeArray.
1560 * psa [I] Array to get the record info from
1561 * pRinfo [O] Destination for the record info
1564 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1565 * Failure: An HRESULT error code indicating the error.
1570 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1572 IRecordInfo** src = (IRecordInfo**)psa;
1574 TRACE("(%p,%p)\n", psa, pRinfo);
1576 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1577 return E_INVALIDARG;
1582 IRecordInfo_AddRef(*pRinfo);
1586 /************************************************************************
1587 * SafeArraySetIID (OLEAUT32.@)
1589 * Set the IID for a SafeArray.
1592 * psa [I] Array to set the IID from
1596 * Success: S_OK. The IID is stored with the array
1597 * Failure: An HRESULT error code indicating the error.
1602 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1604 GUID* dest = (GUID*)psa;
1606 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1608 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1609 return E_INVALIDARG;
1615 /************************************************************************
1616 * SafeArrayGetIID (OLEAUT32.@)
1618 * Get the IID from a SafeArray.
1621 * psa [I] Array to get the ID from
1622 * pGuid [O] Destination for the IID
1625 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1626 * Failure: An HRESULT error code indicating the error.
1631 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1633 GUID* src = (GUID*)psa;
1635 TRACE("(%p,%p)\n", psa, pGuid);
1637 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1638 return E_INVALIDARG;
1644 /************************************************************************
1645 * VectorFromBstr (OLEAUT32.@)
1647 * Create a SafeArray Vector from the bytes of a BSTR.
1650 * bstr [I] String to get bytes from
1651 * ppsa [O] Destination for the array
1654 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1655 * Failure: An HRESULT error code indicating the error.
1660 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1664 TRACE("(%p,%p)\n", bstr, ppsa);
1667 return E_INVALIDARG;
1670 sab.cElements = SysStringByteLen(bstr);
1672 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1676 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1679 return E_OUTOFMEMORY;
1682 /************************************************************************
1683 * BstrFromVector (OLEAUT32.@)
1685 * Create a BSTR from a SafeArray.
1688 * psa [I] Source array
1689 * pbstr [O] Destination for output BSTR
1692 * Success: S_OK. pbstr contains the arrays data.
1693 * Failure: An HRESULT error code indicating the error.
1696 * psa must be a 1 dimensional array of a 1 byte type.
1701 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1703 TRACE("(%p,%p)\n", psa, pbstr);
1706 return E_INVALIDARG;
1710 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1711 return E_INVALIDARG;
1713 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1715 return E_OUTOFMEMORY;