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))
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 /* 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
;
185 size
*= psa
->rgsabound
[ulDim
].cElements
;
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
);
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
;
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
;
233 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt
, cDims
, &psa
)))
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
));
246 psa
->cbElements
= ulSize
;
248 if (FAILED(SafeArrayAllocData(psa
)))
250 SafeArrayDestroyDescriptor(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
);
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
;
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;
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
);
300 ulCellCount
-= ulStartCell
;
302 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
304 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
309 IUnknown_Release(*lpUnknown
);
313 else if (psa
->fFeatures
& (FADF_RECORD
))
315 IRecordInfo
*lpRecInfo
;
317 if (SUCCEEDED(SafeArrayGetRecordInfo(psa
, &lpRecInfo
)))
319 PBYTE pRecordData
= (PBYTE
)psa
->pvData
;
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
;
335 SysFreeString(*lpBstr
);
339 else if (psa
->fFeatures
& FADF_VARIANT
)
341 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
345 HRESULT hRet
= VariantClear(lpVariant
);
347 if (FAILED(hRet
)) FIXME("VariantClear of element failed!\n");
355 /* Copy data items from one array to another */
356 static HRESULT
SAFEARRAY_CopyData(SAFEARRAY
*psa
, SAFEARRAY
*dest
)
358 if (!psa
->pvData
|| !dest
->pvData
|| psa
->fFeatures
& FADF_DATADELETED
)
362 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
364 dest
->fFeatures
= (dest
->fFeatures
& FADF_CREATEVECTOR
) |
365 (psa
->fFeatures
& ~(FADF_CREATEVECTOR
|FADF_DATADELETED
));
367 if (psa
->fFeatures
& FADF_VARIANT
)
369 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
;
370 VARIANT
* lpDest
= (VARIANT
*)dest
->pvData
;
376 hRet
= VariantCopy(lpDest
, lpVariant
);
377 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%lx\n", hRet
);
382 else if (psa
->fFeatures
& FADF_BSTR
)
384 BSTR
* lpBstr
= (BSTR
*)psa
->pvData
;
385 BSTR
* lpDest
= (BSTR
*)dest
->pvData
;
391 *lpDest
= SysAllocStringByteLen((char*)*lpBstr
, SysStringByteLen(*lpBstr
));
393 return E_OUTOFMEMORY
;
403 /* Copy the data over */
404 memcpy(dest
->pvData
, psa
->pvData
, ulCellCount
* psa
->cbElements
);
406 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
408 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)dest
->pvData
;
413 IUnknown_AddRef(*lpUnknown
);
419 if (psa
->fFeatures
& FADF_RECORD
)
421 IRecordInfo
* pRecInfo
= NULL
;
423 SafeArrayGetRecordInfo(psa
, &pRecInfo
);
424 SafeArraySetRecordInfo(dest
, pRecInfo
);
428 /* Release because Get() adds a reference */
429 IRecordInfo_Release(pRecInfo
);
432 else if (psa
->fFeatures
& FADF_HAVEIID
)
435 SafeArrayGetIID(psa
, &guid
);
436 SafeArraySetIID(dest
, &guid
);
438 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
440 SAFEARRAY_SetHiddenDWORD(dest
, SAFEARRAY_GetHiddenDWORD(psa
));
446 /*************************************************************************
447 * SafeArrayAllocDescriptor (OLEAUT32.36)
449 * Allocate and initialise a descriptor for a SafeArray.
452 * cDims [I] Number of dimensions of the array
453 * ppsaOut [O] Destination for new descriptor
456 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
457 * Failure: An HRESULT error code indicating the error.
462 HRESULT WINAPI
SafeArrayAllocDescriptor(UINT cDims
, SAFEARRAY
**ppsaOut
)
466 TRACE("(%d,%p)\n", cDims
, ppsaOut
);
468 if (!cDims
|| cDims
>= 0x10000) /* Maximum 65535 dimensions */
474 /* We need enough space for the header and its bounds */
475 allocSize
= sizeof(SAFEARRAY
) + sizeof(SAFEARRAYBOUND
) * (cDims
- 1);
477 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize
, ppsaOut
)))
480 (*ppsaOut
)->cDims
= cDims
;
482 TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims
, allocSize
);
486 /*************************************************************************
487 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
489 * Allocate and initialise a descriptor for a SafeArray of a given type.
492 * vt [I] The type of items to store in the array
493 * cDims [I] Number of dimensions of the array
494 * ppsaOut [O] Destination for new descriptor
497 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
498 * Failure: An HRESULT error code indicating the error.
501 * - This function does not chack that vt is an allowed VARTYPE.
502 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
505 HRESULT WINAPI
SafeArrayAllocDescriptorEx(VARTYPE vt
, UINT cDims
, SAFEARRAY
**ppsaOut
)
508 HRESULT hRet
= E_UNEXPECTED
;
510 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, ppsaOut
);
512 cbElements
= SAFEARRAY_GetVTSize(vt
);
514 WARN("Creating a descriptor with an invalid VARTYPE!\n");
516 hRet
= SafeArrayAllocDescriptor(cDims
, ppsaOut
);
520 SAFEARRAY_SetFeatures(vt
, *ppsaOut
);
521 (*ppsaOut
)->cbElements
= cbElements
;
526 /*************************************************************************
527 * SafeArrayAllocData (OLEAUT32.37)
529 * Allocate the data area of a SafeArray.
532 * psa [I] SafeArray to allocate the data area of.
535 * Success: S_OK. The data area is allocated and initialised.
536 * Failure: An HRESULT error code indicating the error.
541 HRESULT WINAPI
SafeArrayAllocData(SAFEARRAY
*psa
)
543 HRESULT hRet
= E_INVALIDARG
;
545 TRACE("(%p)\n", psa
);
549 ULONG ulSize
= SAFEARRAY_GetCellCount(psa
);
551 hRet
= E_OUTOFMEMORY
;
555 psa
->pvData
= SAFEARRAY_Malloc(ulSize
* psa
->cbElements
);
560 TRACE("%lu bytes allocated for data at %p (%lu objects).\n",
561 ulSize
* psa
->cbElements
, psa
->pvData
, ulSize
);
568 /*************************************************************************
569 * SafeArrayCreate (OLEAUT32.15)
571 * Create a new SafeArray.
574 * vt [I] Type to store in the safe array
575 * cDims [I] Number of array dimensions
576 * rgsabound [I] Bounds of the array dimensions
579 * Success: A pointer to a new array object.
580 * Failure: NULL, if any parameter is invalid or memory allocation fails.
583 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
584 * in the Wine implementation.
587 SAFEARRAY
* WINAPI
SafeArrayCreate(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
)
589 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
);
594 return SAFEARRAY_Create(vt
, cDims
, rgsabound
, 0);
597 /*************************************************************************
598 * SafeArrayCreateEx (OLEAUT32.15)
600 * Create a new SafeArray.
603 * vt [I] Type to store in the safe array
604 * cDims [I] Number of array dimensions
605 * rgsabound [I] Bounds of the array dimensions
606 * pvExtra [I] Extra data
609 * Success: A pointer to a new array object.
610 * Failure: NULL, if any parameter is invalid or memory allocation fails.
615 SAFEARRAY
* WINAPI
SafeArrayCreateEx(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
, LPVOID pvExtra
)
618 IRecordInfo
* iRecInfo
= (IRecordInfo
*)pvExtra
;
621 TRACE("(%d->%s,%d,%p,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
, pvExtra
);
627 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
629 psa
= SAFEARRAY_Create(vt
, cDims
, rgsabound
, ulSize
);
636 SafeArraySetRecordInfo(psa
, pvExtra
);
640 SafeArraySetIID(psa
, pvExtra
);
647 /************************************************************************
648 * SafeArrayCreateVector (OLEAUT32.411)
650 * Create a one dimensional, contiguous SafeArray.
653 * vt [I] Type to store in the safe array
654 * lLbound [I] Lower bound of the array
655 * cElements [I] Number of elements in the array
658 * Success: A pointer to a new array object.
659 * Failure: NULL, if any parameter is invalid or memory allocation fails.
664 SAFEARRAY
* WINAPI
SafeArrayCreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
)
666 TRACE("(%d->%s,%ld,%ld\n", vt
, debugstr_vt(vt
), lLbound
, cElements
);
671 return SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, SAFEARRAY_GetVTSize(vt
));
674 /************************************************************************
675 * SafeArrayCreateVectorEx (OLEAUT32.411)
677 * Create a one dimensional, contiguous SafeArray.
680 * vt [I] Type to store in the safe array
681 * lLbound [I] Lower bound of the array
682 * cElements [I] Number of elements in the array
683 * pvExtra [I] Extra data
686 * Success: A pointer to a new array object.
687 * Failure: NULL, if any parameter is invalid or memory allocation fails.
692 SAFEARRAY
* WINAPI
SafeArrayCreateVectorEx(VARTYPE vt
, LONG lLbound
, ULONG cElements
, LPVOID pvExtra
)
695 IRecordInfo
* iRecInfo
= (IRecordInfo
*)pvExtra
;
698 TRACE("(%d->%s,%ld,%ld,%p\n", vt
, debugstr_vt(vt
), lLbound
, cElements
, pvExtra
);
704 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
707 ulSize
= SAFEARRAY_GetVTSize(vt
);
709 psa
= SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, ulSize
);
716 SafeArraySetRecordInfo(psa
, iRecInfo
);
720 SafeArraySetIID(psa
, pvExtra
);
727 /*************************************************************************
728 * SafeArrayDestroyDescriptor (OLEAUT32.38)
730 * Destroy a SafeArray.
733 * psa [I] SafeArray to destroy.
736 * Success: S_OK. The resources used by the array are freed.
737 * Failure: An HRESULT error code indicating the error.
742 HRESULT WINAPI
SafeArrayDestroyDescriptor(SAFEARRAY
*psa
)
744 TRACE("(%p)\n", psa
);
748 LPVOID lpv
= (char*)psa
- SAFEARRAY_HIDDEN_SIZE
;
751 return DISP_E_ARRAYISLOCKED
; /* Can't destroy a locked array */
753 if (psa
->fFeatures
& FADF_RECORD
)
754 SafeArraySetRecordInfo(psa
, NULL
);
756 if (psa
->fFeatures
& FADF_CREATEVECTOR
&&
757 !(psa
->fFeatures
& FADF_DATADELETED
))
758 SAFEARRAY_DestroyData(psa
, 0); /* Data not previously deleted */
760 if (!SAFEARRAY_Free(lpv
))
766 /*************************************************************************
767 * SafeArrayLock (OLEAUT32.21)
769 * Increment the lock counter of a SafeArray.
772 * psa [O] SafeArray to lock
775 * Success: S_OK. The array lock is incremented.
776 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
777 * are held on the array at once.
780 * In Win32 these locks are not thread safe.
783 HRESULT WINAPI
SafeArrayLock(SAFEARRAY
*psa
)
787 TRACE("(%p)\n", psa
);
792 ulLocks
= InterlockedIncrement(&psa
->cLocks
);
794 if (ulLocks
> 0xffff) /* Maximum of 16384 locks at a time */
796 WARN("Out of locks!\n");
797 InterlockedDecrement(&psa
->cLocks
);
803 /*************************************************************************
804 * SafeArrayUnlock (OLEAUT32.22)
806 * Decrement the lock counter of a SafeArray.
809 * psa [O] SafeArray to unlock
812 * Success: S_OK. The array lock is decremented.
813 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
819 HRESULT WINAPI
SafeArrayUnlock(SAFEARRAY
*psa
)
821 TRACE("(%p)\n", psa
);
826 if ((LONG
)InterlockedDecrement(&psa
->cLocks
) < 0)
828 WARN("Unlocked but no lock held!\n");
829 InterlockedIncrement(&psa
->cLocks
);
835 /*************************************************************************
836 * SafeArrayPutElement (OLEAUT32.26)
838 * Put an item into a SafeArray.
841 * psa [I] SafeArray to insert into
842 * rgIndices [I] Indices to insert at
843 * pvData [I] Data to insert
846 * Success: S_OK. The item is inserted
847 * Failure: An HRESULT error code indicating the error.
852 HRESULT WINAPI
SafeArrayPutElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
856 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
858 if (!psa
|| !rgIndices
)
863 ERR("Invalid pvData would crash under Win32!\n");
867 hRet
= SafeArrayLock(psa
);
873 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvDest
);
877 if (psa
->fFeatures
& FADF_VARIANT
)
879 VARIANT
* lpVariant
= (VARIANT
*)pvData
;
880 VARIANT
* lpDest
= (VARIANT
*)lpvDest
;
882 hRet
= VariantClear(lpDest
);
883 if (FAILED(hRet
)) FIXME("VariantClear failed with 0x%lx\n", hRet
);
884 hRet
= VariantCopy(lpDest
, lpVariant
);
885 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%lx\n", hRet
);
887 else if (psa
->fFeatures
& FADF_BSTR
)
889 BSTR lpBstr
= (BSTR
)pvData
;
890 BSTR
* lpDest
= (BSTR
*)lpvDest
;
893 SysFreeString(*lpDest
);
897 *lpDest
= SysAllocStringByteLen((char*)lpBstr
, SysStringByteLen(lpBstr
));
899 hRet
= E_OUTOFMEMORY
;
906 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
908 LPUNKNOWN lpUnknown
= (LPUNKNOWN
)pvData
;
909 LPUNKNOWN
*lpDest
= (LPUNKNOWN
*)lpvDest
;
912 IUnknown_AddRef(lpUnknown
);
914 IUnknown_Release(*lpDest
);
917 /* Copy the data over */
918 memcpy(lpvDest
, pvData
, psa
->cbElements
);
922 SafeArrayUnlock(psa
);
928 /*************************************************************************
929 * SafeArrayGetElement (OLEAUT32.25)
931 * Get an item from a SafeArray.
934 * psa [I] SafeArray to get from
935 * rgIndices [I] Indices to get from
936 * pvData [O] Destination for data
939 * Success: S_OK. The item data is returned in pvData.
940 * Failure: An HRESULT error code indicating the error.
945 HRESULT WINAPI
SafeArrayGetElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
949 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
951 if (!psa
|| !rgIndices
|| !pvData
)
954 hRet
= SafeArrayLock(psa
);
960 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvSrc
);
964 if (psa
->fFeatures
& FADF_VARIANT
)
966 VARIANT
* lpVariant
= (VARIANT
*)lpvSrc
;
967 VARIANT
* lpDest
= (VARIANT
*)pvData
;
969 /* The original content of pvData is ignored. */
970 V_VT(lpDest
) = VT_EMPTY
;
971 hRet
= VariantCopy(lpDest
, lpVariant
);
972 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%lx\n", hRet
);
974 else if (psa
->fFeatures
& FADF_BSTR
)
976 BSTR
* lpBstr
= (BSTR
*)lpvSrc
;
977 BSTR
* lpDest
= (BSTR
*)pvData
;
981 *lpDest
= SysAllocStringByteLen((char*)*lpBstr
, SysStringByteLen(*lpBstr
));
983 hRet
= E_OUTOFMEMORY
;
990 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
992 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)lpvSrc
;
995 IUnknown_AddRef(*lpUnknown
);
997 /* Copy the data over */
998 memcpy(pvData
, lpvSrc
, psa
->cbElements
);
1001 SafeArrayUnlock(psa
);
1006 /*************************************************************************
1007 * SafeArrayGetUBound (OLEAUT32.19)
1009 * Get the upper bound for a given SafeArray dimension
1012 * psa [I] Array to get dimension upper bound from
1013 * nDim [I] The dimension number to get the upper bound of
1014 * plUbound [O] Destination for the upper bound
1017 * Success: S_OK. plUbound contains the dimensions upper bound.
1018 * Failure: An HRESULT error code indicating the error.
1023 HRESULT WINAPI
SafeArrayGetUBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plUbound
)
1025 TRACE("(%p,%d,%p)\n", psa
, nDim
, plUbound
);
1027 if (!psa
|| !plUbound
)
1028 return E_INVALIDARG
;
1030 if(!nDim
|| nDim
> psa
->cDims
)
1031 return DISP_E_BADINDEX
;
1033 *plUbound
= psa
->rgsabound
[nDim
- 1].lLbound
+
1034 psa
->rgsabound
[nDim
- 1].cElements
- 1;
1039 /*************************************************************************
1040 * SafeArrayGetLBound (OLEAUT32.20)
1042 * Get the lower bound for a given SafeArray dimension
1045 * psa [I] Array to get dimension lower bound from
1046 * nDim [I] The dimension number to get the lowe bound of
1047 * plLbound [O] Destination for the lower bound
1050 * Success: S_OK. plUbound contains the dimensions lower bound.
1051 * Failure: An HRESULT error code indicating the error.
1056 HRESULT WINAPI
SafeArrayGetLBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plLbound
)
1058 TRACE("(%p,%d,%p)\n", psa
, nDim
, plLbound
);
1060 if (!psa
|| !plLbound
)
1061 return E_INVALIDARG
;
1063 if(!nDim
|| nDim
> psa
->cDims
)
1064 return DISP_E_BADINDEX
;
1066 *plLbound
= psa
->rgsabound
[nDim
- 1].lLbound
;
1070 /*************************************************************************
1071 * SafeArrayGetDim (OLEAUT32.17)
1073 * Get the number of dimensions in a SafeArray.
1076 * psa [I] Array to get the dimensions of
1079 * The number of array dimensions in psa, or 0 if psa is NULL.
1084 UINT WINAPI
SafeArrayGetDim(SAFEARRAY
*psa
)
1086 TRACE("(%p) returning %ld\n", psa
, psa
? psa
->cDims
: 0ul);
1087 return psa
? psa
->cDims
: 0;
1090 /*************************************************************************
1091 * SafeArrayGetElemsize (OLEAUT32.18)
1093 * Get the size of an element in a SafeArray.
1096 * psa [I] Array to get the element size from
1099 * The size of a single element in psa, or 0 if psa is NULL.
1104 UINT WINAPI
SafeArrayGetElemsize(SAFEARRAY
*psa
)
1106 TRACE("(%p) returning %ld\n", psa
, psa
? psa
->cbElements
: 0ul);
1107 return psa
? psa
->cbElements
: 0;
1110 /*************************************************************************
1111 * SafeArrayAccessData (OLEAUT32.23)
1113 * Lock a SafeArray and return a pointer to its data.
1116 * psa [I] Array to get the data pointer from
1117 * ppvData [O] Destination for the arrays data pointer
1120 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1122 * Failure: An HRESULT error code indicating the error.
1127 HRESULT WINAPI
SafeArrayAccessData(SAFEARRAY
*psa
, void **ppvData
)
1129 TRACE("(%p,%p)\n", psa
, ppvData
);
1131 if(!psa
|| !ppvData
)
1132 return E_INVALIDARG
;
1134 if (SUCCEEDED(SafeArrayLock(psa
)))
1136 *ppvData
= psa
->pvData
;
1140 return E_UNEXPECTED
;
1144 /*************************************************************************
1145 * SafeArrayUnaccessData (OLEAUT32.24)
1147 * Unlock a SafeArray after accessing its data.
1150 * psa [I] Array to unlock
1153 * Success: S_OK. The array is unlocked.
1154 * Failure: An HRESULT error code indicating the error.
1159 HRESULT WINAPI
SafeArrayUnaccessData(SAFEARRAY
*psa
)
1161 TRACE("(%p)\n", psa
);
1162 return SafeArrayUnlock(psa
);
1165 /************************************************************************
1166 * SafeArrayPtrOfIndex (OLEAUT32.148)
1168 * Get the address of an item in a SafeArray.
1171 * psa [I] Array to get the items address from
1172 * rgIndices [I] Index of the item in the array
1173 * ppvData [O] Destination for item address
1176 * Success: S_OK. ppvData contains a pointer to the item.
1177 * Failure: An HRESULT error code indicating the error.
1180 * This function does not lock the array.
1185 HRESULT WINAPI
SafeArrayPtrOfIndex(SAFEARRAY
*psa
, LONG
*rgIndices
, void **ppvData
)
1188 ULONG cell
= 0, dimensionSize
= 1;
1189 SAFEARRAYBOUND
* psab
;
1192 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, ppvData
);
1194 /* The general formula for locating the cell number of an entry in an n
1195 * dimensional array (where cn = coordinate in dimension dn) is:
1197 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1199 * We calculate the size of the last dimension at each step through the
1200 * dimensions to avoid recursing to calculate the last dimensions size.
1202 if (!psa
|| !rgIndices
|| !ppvData
)
1203 return E_INVALIDARG
;
1205 psab
= psa
->rgsabound
;
1208 if (c1
< psab
->lLbound
|| c1
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1209 return DISP_E_BADINDEX
; /* Initial index out of bounds */
1211 for (dim
= 1; dim
< psa
->cDims
; dim
++)
1213 dimensionSize
*= psab
->cElements
;
1217 if (!psab
->cElements
||
1218 *rgIndices
< psab
->lLbound
||
1219 *rgIndices
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1220 return DISP_E_BADINDEX
; /* Index out of bounds */
1222 cell
+= (*rgIndices
- psab
->lLbound
) * dimensionSize
;
1226 cell
+= (c1
- psa
->rgsabound
[0].lLbound
);
1228 *ppvData
= (char*)psa
->pvData
+ cell
* psa
->cbElements
;
1232 /************************************************************************
1233 * SafeArrayDestroyData (OLEAUT32.39)
1235 * Destroy the data associated with a SafeArray.
1238 * psa [I] Array to delete the data from
1241 * Success: S_OK. All items and the item data are freed.
1242 * Failure: An HRESULT error code indicating the error.
1247 HRESULT WINAPI
SafeArrayDestroyData(SAFEARRAY
*psa
)
1249 TRACE("(%p)\n", psa
);
1252 return E_INVALIDARG
;
1255 return DISP_E_ARRAYISLOCKED
; /* Can't delete a locked array */
1257 /* If static, keep pvData and don't free */
1258 if (psa
->pvData
&& !(psa
->fFeatures
& FADF_STATIC
))
1260 /* Delete the actual item data */
1261 if (FAILED(SAFEARRAY_DestroyData(psa
, 0)))
1262 return E_UNEXPECTED
;
1264 /* If this is not a vector, free the data memory block */
1265 if (!(psa
->fFeatures
& FADF_CREATEVECTOR
))
1267 if (!SAFEARRAY_Free(psa
->pvData
))
1268 return E_UNEXPECTED
;
1272 psa
->fFeatures
|= FADF_DATADELETED
; /* Mark the data deleted */
1278 /************************************************************************
1279 * SafeArrayCopyData (OLEAUT32.412)
1281 * Copy all data from one SafeArray to another.
1284 * psaSource [I] Source for copy
1285 * psaTarget [O] Destination for copy
1288 * Success: S_OK. psaTarget contains a copy of psaSource.
1289 * Failure: An HRESULT error code indicating the error.
1292 * The two arrays must have the same number of dimensions and elements.
1297 HRESULT WINAPI
SafeArrayCopyData(SAFEARRAY
*psaSource
, SAFEARRAY
*psaTarget
)
1301 TRACE("(%p,%p)\n", psaSource
, psaTarget
);
1303 if (!psaSource
|| !psaTarget
||
1304 psaSource
->cDims
!= psaTarget
->cDims
||
1305 psaSource
->cbElements
!= psaTarget
->cbElements
)
1306 return E_INVALIDARG
;
1308 /* Each dimension must be the same size */
1309 for (dim
= psaSource
->cDims
- 1; dim
>= 0 ; dim
--)
1310 if (psaSource
->rgsabound
[dim
].cElements
!=
1311 psaTarget
->rgsabound
[dim
].cElements
)
1312 return E_INVALIDARG
;
1314 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget
, 0)) &&
1315 SUCCEEDED(SAFEARRAY_CopyData(psaSource
, psaTarget
)))
1317 return E_UNEXPECTED
;
1320 /************************************************************************
1321 * SafeArrayDestroy (OLEAUT32.16)
1323 * Destroy a SafeArray.
1326 * psa [I] Array to destroy
1329 * Success: S_OK. All resources used by the array are freed.
1330 * Failure: An HRESULT error code indicating the error.
1335 HRESULT WINAPI
SafeArrayDestroy(SAFEARRAY
*psa
)
1337 TRACE("(%p)\n", psa
);
1343 return DISP_E_ARRAYISLOCKED
;
1345 /* Native doesn't check to see if the free succeeds */
1346 SafeArrayDestroyData(psa
);
1347 SafeArrayDestroyDescriptor(psa
);
1351 /************************************************************************
1352 * SafeArrayCopy (OLEAUT32.27)
1354 * Make a duplicate of a SafeArray.
1357 * psa [I] Source for copy
1358 * ppsaOut [O] Destination for new copy
1361 * Success: S_OK. ppsaOut contains a copy of the array.
1362 * Failure: An HRESULT error code indicating the error.
1367 HRESULT WINAPI
SafeArrayCopy(SAFEARRAY
*psa
, SAFEARRAY
**ppsaOut
)
1371 TRACE("(%p,%p)\n", psa
, ppsaOut
);
1374 return E_INVALIDARG
;
1379 return S_OK
; /* Handles copying of NULL arrays */
1381 if (psa
->fFeatures
& (FADF_RECORD
|FADF_HAVEIID
|FADF_HAVEVARTYPE
))
1384 if (FAILED(SafeArrayGetVartype(psa
, &vt
)))
1385 hRet
= E_UNEXPECTED
;
1387 hRet
= SafeArrayAllocDescriptorEx(vt
, psa
->cDims
, ppsaOut
);
1391 hRet
= SafeArrayAllocDescriptor(psa
->cDims
, ppsaOut
);
1392 if (SUCCEEDED(hRet
))
1394 (*ppsaOut
)->fFeatures
= psa
->fFeatures
& ~FADF_CREATEVECTOR
;
1395 (*ppsaOut
)->cbElements
= psa
->cbElements
;
1399 if (SUCCEEDED(hRet
))
1401 /* Copy dimension bounds */
1402 memcpy((*ppsaOut
)->rgsabound
, psa
->rgsabound
, psa
->cDims
* sizeof(SAFEARRAYBOUND
));
1404 (*ppsaOut
)->pvData
= SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1406 if ((*ppsaOut
)->pvData
)
1408 hRet
= SAFEARRAY_CopyData(psa
, *ppsaOut
);
1410 if (SUCCEEDED(hRet
))
1413 SAFEARRAY_Free((*ppsaOut
)->pvData
);
1415 SafeArrayDestroyDescriptor(*ppsaOut
);
1421 /************************************************************************
1422 * SafeArrayRedim (OLEAUT32.40)
1424 * Changes the characteristics of the last dimension of a SafeArray
1427 * psa [I] Array to change
1428 * psabound [I] New bound details for the last dimension
1431 * Success: S_OK. psa is updated to reflect the new bounds.
1432 * Failure: An HRESULT error code indicating the error.
1437 HRESULT WINAPI
SafeArrayRedim(SAFEARRAY
*psa
, SAFEARRAYBOUND
*psabound
)
1439 SAFEARRAYBOUND
*oldBounds
;
1441 TRACE("(%p,%p)\n", psa
, psabound
);
1443 if (!psa
|| psa
->fFeatures
& FADF_FIXEDSIZE
|| !psabound
)
1444 return E_INVALIDARG
;
1446 if (psa
->cLocks
> 0)
1447 return DISP_E_ARRAYISLOCKED
;
1449 if (FAILED(SafeArrayLock(psa
)))
1450 return E_UNEXPECTED
;
1452 oldBounds
= &psa
->rgsabound
[psa
->cDims
- 1];
1453 oldBounds
->lLbound
= psabound
->lLbound
;
1455 if (psabound
->cElements
!= oldBounds
->cElements
)
1457 if (psabound
->cElements
< oldBounds
->cElements
)
1459 /* Shorten the final dimension. */
1460 ULONG ulStartCell
= psa
->cDims
== 1 ? 0 : SAFEARRAY_GetDimensionCells(psa
, psa
->cDims
- 1);
1462 ulStartCell
+= psabound
->cElements
;
1463 SAFEARRAY_DestroyData(psa
, ulStartCell
);
1467 /* Lengthen the final dimension */
1468 ULONG ulOldSize
, ulNewSize
;
1471 ulOldSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1473 ulNewSize
= (ulOldSize
/ oldBounds
->cElements
) * psabound
->cElements
;
1475 int oldelems
= oldBounds
->cElements
;
1476 oldBounds
->cElements
= psabound
->cElements
;
1477 ulNewSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1478 oldBounds
->cElements
= oldelems
;
1481 if (!(pvNewData
= SAFEARRAY_Malloc(ulNewSize
)))
1483 SafeArrayUnlock(psa
);
1484 return E_UNEXPECTED
;
1487 memcpy(pvNewData
, psa
->pvData
, ulOldSize
);
1488 SAFEARRAY_Free(psa
->pvData
);
1489 psa
->pvData
= pvNewData
;
1491 oldBounds
->cElements
= psabound
->cElements
;
1494 SafeArrayUnlock(psa
);
1498 /************************************************************************
1499 * SafeArrayGetVartype (OLEAUT32.77)
1501 * Get the type of the items in a SafeArray.
1504 * psa [I] Array to get the type from
1505 * pvt [O] Destination for the type
1508 * Success: S_OK. pvt contains the type of the items.
1509 * Failure: An HRESULT error code indicating the error.
1514 HRESULT WINAPI
SafeArrayGetVartype(SAFEARRAY
* psa
, VARTYPE
* pvt
)
1516 TRACE("(%p,%p)\n", psa
, pvt
);
1519 return E_INVALIDARG
;
1521 if (psa
->fFeatures
& FADF_RECORD
)
1523 else if (psa
->fFeatures
& FADF_HAVEIID
)
1525 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
1527 VARTYPE vt
= SAFEARRAY_GetHiddenDWORD(psa
);
1531 return E_INVALIDARG
;
1536 /************************************************************************
1537 * SafeArraySetRecordInfo (OLEAUT32.@)
1539 * Set the record info for a SafeArray.
1542 * psa [I] Array to set the record info for
1543 * pRinfo [I] Record info
1546 * Success: S_OK. The record info is stored with the array.
1547 * Failure: An HRESULT error code indicating the error.
1552 HRESULT WINAPI
SafeArraySetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
*pRinfo
)
1554 IRecordInfo
** dest
= (IRecordInfo
**)psa
;
1556 TRACE("(%p,%p)\n", psa
, pRinfo
);
1558 if (!psa
|| !(psa
->fFeatures
& FADF_RECORD
))
1559 return E_INVALIDARG
;
1562 IRecordInfo_AddRef(pRinfo
);
1565 IRecordInfo_Release(dest
[-1]);
1571 /************************************************************************
1572 * SafeArrayGetRecordInfo (OLEAUT32.@)
1574 * Get the record info from a SafeArray.
1577 * psa [I] Array to get the record info from
1578 * pRinfo [O] Destination for the record info
1581 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1582 * Failure: An HRESULT error code indicating the error.
1587 HRESULT WINAPI
SafeArrayGetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
**pRinfo
)
1589 IRecordInfo
** src
= (IRecordInfo
**)psa
;
1591 TRACE("(%p,%p)\n", psa
, pRinfo
);
1593 if (!psa
|| !pRinfo
|| !(psa
->fFeatures
& FADF_RECORD
))
1594 return E_INVALIDARG
;
1599 IRecordInfo_AddRef(*pRinfo
);
1603 /************************************************************************
1604 * SafeArraySetIID (OLEAUT32.@)
1606 * Set the IID for a SafeArray.
1609 * psa [I] Array to set the IID from
1613 * Success: S_OK. The IID is stored with the array
1614 * Failure: An HRESULT error code indicating the error.
1619 HRESULT WINAPI
SafeArraySetIID(SAFEARRAY
*psa
, REFGUID guid
)
1621 GUID
* dest
= (GUID
*)psa
;
1623 TRACE("(%p,%s)\n", psa
, debugstr_guid(guid
));
1625 if (!psa
|| !guid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1626 return E_INVALIDARG
;
1632 /************************************************************************
1633 * SafeArrayGetIID (OLEAUT32.@)
1635 * Get the IID from a SafeArray.
1638 * psa [I] Array to get the ID from
1639 * pGuid [O] Destination for the IID
1642 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1643 * Failure: An HRESULT error code indicating the error.
1648 HRESULT WINAPI
SafeArrayGetIID(SAFEARRAY
*psa
, GUID
*pGuid
)
1650 GUID
* src
= (GUID
*)psa
;
1652 TRACE("(%p,%p)\n", psa
, pGuid
);
1654 if (!psa
|| !pGuid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1655 return E_INVALIDARG
;
1661 /************************************************************************
1662 * VectorFromBstr (OLEAUT32.@)
1664 * Create a SafeArray Vector from the bytes of a BSTR.
1667 * bstr [I] String to get bytes from
1668 * ppsa [O] Destination for the array
1671 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1672 * Failure: An HRESULT error code indicating the error.
1677 HRESULT WINAPI
VectorFromBstr(BSTR bstr
, SAFEARRAY
**ppsa
)
1681 TRACE("(%p,%p)\n", bstr
, ppsa
);
1684 return E_INVALIDARG
;
1687 sab
.cElements
= SysStringByteLen(bstr
);
1689 *ppsa
= SAFEARRAY_Create(VT_UI1
, 1, &sab
, 0);
1693 memcpy((*ppsa
)->pvData
, bstr
, sab
.cElements
);
1696 return E_OUTOFMEMORY
;
1699 /************************************************************************
1700 * BstrFromVector (OLEAUT32.@)
1702 * Create a BSTR from a SafeArray.
1705 * psa [I] Source array
1706 * pbstr [O] Destination for output BSTR
1709 * Success: S_OK. pbstr contains the arrays data.
1710 * Failure: An HRESULT error code indicating the error.
1713 * psa must be a 1 dimensional array of a 1 byte type.
1718 HRESULT WINAPI
BstrFromVector(SAFEARRAY
*psa
, BSTR
*pbstr
)
1720 TRACE("(%p,%p)\n", psa
, pbstr
);
1723 return E_INVALIDARG
;
1727 if (!psa
|| psa
->cbElements
!= 1 || psa
->cDims
!= 1)
1728 return E_INVALIDARG
;
1730 *pbstr
= SysAllocStringByteLen(psa
->pvData
, psa
->rgsabound
[0].cElements
);
1732 return E_OUTOFMEMORY
;