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)
88 /* features listed here are not propagated to newly created array or data copy
89 created with SafeArrayCopy()/SafeArrayCopyData() */
90 static const USHORT ignored_copy_features
=
98 static inline LPVOID
SAFEARRAY_Malloc(ULONG ulSize
)
100 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
101 * instance returned from CoGetMalloc().
103 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulSize
);
107 static inline BOOL
SAFEARRAY_Free(LPVOID lpData
)
109 return HeapFree(GetProcessHeap(), 0, lpData
);
112 /* Get the size of a supported VT type (0 means unsupported) */
113 static DWORD
SAFEARRAY_GetVTSize(VARTYPE vt
)
118 case VT_UI1
: return sizeof(BYTE
);
121 case VT_UI2
: return sizeof(SHORT
);
125 case VT_ERROR
: return sizeof(LONG
);
128 case VT_UI8
: return sizeof(LONG64
);
130 case VT_UINT
: return sizeof(INT
);
132 case VT_UINT_PTR
: return sizeof(UINT_PTR
);
133 case VT_CY
: return sizeof(CY
);
134 case VT_DATE
: return sizeof(DATE
);
135 case VT_BSTR
: return sizeof(BSTR
);
136 case VT_DISPATCH
: return sizeof(LPDISPATCH
);
137 case VT_VARIANT
: return sizeof(VARIANT
);
138 case VT_UNKNOWN
: return sizeof(LPUNKNOWN
);
139 case VT_DECIMAL
: return sizeof(DECIMAL
);
140 /* Note: Return a non-zero size to indicate vt is valid. The actual size
141 * of a UDT is taken from the result of IRecordInfo_GetSize().
143 case VT_RECORD
: return 32;
148 /* Set the hidden data for an array */
149 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY
* psa
, DWORD dw
)
151 /* Implementation data is stored in the 4 bytes before the header */
152 LPDWORD lpDw
= (LPDWORD
)psa
;
156 /* Get the hidden data from an array */
157 static inline DWORD
SAFEARRAY_GetHiddenDWORD(SAFEARRAY
* psa
)
159 LPDWORD lpDw
= (LPDWORD
)psa
;
163 /* Get the number of cells in a SafeArray */
164 static ULONG
SAFEARRAY_GetCellCount(const SAFEARRAY
*psa
)
166 const SAFEARRAYBOUND
* psab
= psa
->rgsabound
;
167 USHORT cCount
= psa
->cDims
;
168 ULONG ulNumCells
= 1;
172 /* This is a valid bordercase. See testcases. -Marcus */
173 if (!psab
->cElements
)
175 ulNumCells
*= psab
->cElements
;
181 /* Allocate a descriptor for an array */
182 static HRESULT
SAFEARRAY_AllocDescriptor(ULONG ulSize
, SAFEARRAY
**ppsaOut
)
184 char *ptr
= SAFEARRAY_Malloc(ulSize
+ SAFEARRAY_HIDDEN_SIZE
);
189 return E_OUTOFMEMORY
;
192 *ppsaOut
= (SAFEARRAY
*)(ptr
+ SAFEARRAY_HIDDEN_SIZE
);
196 /* Set the features of an array */
197 static void SAFEARRAY_SetFeatures(VARTYPE vt
, SAFEARRAY
*psa
)
199 /* Set the IID if we have one, otherwise set the type */
200 if (vt
== VT_DISPATCH
)
202 psa
->fFeatures
= FADF_HAVEIID
;
203 SafeArraySetIID(psa
, &IID_IDispatch
);
205 else if (vt
== VT_UNKNOWN
)
207 psa
->fFeatures
= FADF_HAVEIID
;
208 SafeArraySetIID(psa
, &IID_IUnknown
);
210 else if (vt
== VT_RECORD
)
211 psa
->fFeatures
= FADF_RECORD
;
214 psa
->fFeatures
= FADF_HAVEVARTYPE
;
215 SAFEARRAY_SetHiddenDWORD(psa
, vt
);
219 /* Create an array */
220 static SAFEARRAY
* SAFEARRAY_Create(VARTYPE vt
, UINT cDims
, const SAFEARRAYBOUND
*rgsabound
, ULONG ulSize
)
222 SAFEARRAY
*psa
= NULL
;
228 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt
, cDims
, &psa
)))
232 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
233 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
234 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
235 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
238 for (i
= 0; i
< cDims
; i
++)
239 memcpy(psa
->rgsabound
+ i
, rgsabound
+ cDims
- 1 - i
, sizeof(SAFEARRAYBOUND
));
242 psa
->cbElements
= ulSize
;
244 if (!psa
->cbElements
|| FAILED(SafeArrayAllocData(psa
)))
246 SafeArrayDestroyDescriptor(psa
);
253 /* Create an array as a vector */
254 static SAFEARRAY
* SAFEARRAY_CreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
, ULONG ulSize
)
256 SAFEARRAY
*psa
= NULL
;
258 if (ulSize
|| (vt
== VT_RECORD
))
260 /* Allocate the header and data together */
261 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY
) + ulSize
* cElements
, &psa
)))
263 SAFEARRAY_SetFeatures(vt
, psa
);
266 psa
->fFeatures
|= FADF_CREATEVECTOR
;
267 psa
->pvData
= &psa
[1]; /* Data follows the header */
268 psa
->cbElements
= ulSize
;
269 psa
->rgsabound
[0].cElements
= cElements
;
270 psa
->rgsabound
[0].lLbound
= lLbound
;
274 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
275 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
276 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
277 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
284 /* Free data items in an array */
285 static HRESULT
SAFEARRAY_DestroyData(SAFEARRAY
*psa
, ULONG ulStartCell
)
287 if (psa
->pvData
&& !(psa
->fFeatures
& FADF_DATADELETED
))
289 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
291 if (ulStartCell
> ulCellCount
) {
292 FIXME("unexpted ulcellcount %d, start %d\n",ulCellCount
,ulStartCell
);
296 ulCellCount
-= ulStartCell
;
298 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
300 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)psa
->pvData
+ ulStartCell
;
305 IUnknown_Release(*lpUnknown
);
309 else if (psa
->fFeatures
& FADF_RECORD
)
311 IRecordInfo
*lpRecInfo
;
313 if (SUCCEEDED(SafeArrayGetRecordInfo(psa
, &lpRecInfo
)))
315 PBYTE pRecordData
= psa
->pvData
;
318 IRecordInfo_RecordClear(lpRecInfo
, pRecordData
);
319 pRecordData
+= psa
->cbElements
;
321 IRecordInfo_Release(lpRecInfo
);
324 else if (psa
->fFeatures
& FADF_BSTR
)
326 BSTR
* lpBstr
= (BSTR
*)psa
->pvData
+ ulStartCell
;
330 SysFreeString(*lpBstr
);
334 else if (psa
->fFeatures
& FADF_VARIANT
)
336 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
+ ulStartCell
;
340 HRESULT hRet
= VariantClear(lpVariant
);
342 if (FAILED(hRet
)) FIXME("VariantClear of element failed!\n");
350 /* Copy data items from one array to another. Destination data is freed before copy. */
351 static HRESULT
SAFEARRAY_CopyData(SAFEARRAY
*psa
, SAFEARRAY
*dest
)
358 if (!dest
->pvData
|| psa
->fFeatures
& FADF_DATADELETED
)
362 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
364 dest
->fFeatures
= (dest
->fFeatures
& FADF_CREATEVECTOR
) | (psa
->fFeatures
& ~ignored_copy_features
);
366 if (psa
->fFeatures
& FADF_VARIANT
)
368 VARIANT
*src_var
= psa
->pvData
;
369 VARIANT
*dest_var
= dest
->pvData
;
375 /* destination is cleared automatically */
376 hRet
= VariantCopy(dest_var
, src_var
);
377 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%08x, element %u\n", hRet
, ulCellCount
);
382 else if (psa
->fFeatures
& FADF_BSTR
)
384 BSTR
*src_bstr
= psa
->pvData
;
385 BSTR
*dest_bstr
= dest
->pvData
;
389 SysFreeString(*dest_bstr
);
392 *dest_bstr
= SysAllocStringByteLen((char*)*src_bstr
, SysStringByteLen(*src_bstr
));
394 return E_OUTOFMEMORY
;
402 else if (psa
->fFeatures
& FADF_RECORD
)
404 BYTE
*dest_data
= dest
->pvData
;
405 BYTE
*src_data
= psa
->pvData
;
408 SafeArrayGetRecordInfo(psa
, &record
);
409 while (ulCellCount
--)
411 /* RecordCopy() clears destination record */
412 hr
= IRecordInfo_RecordCopy(record
, src_data
, dest_data
);
413 if (FAILED(hr
)) break;
414 src_data
+= psa
->cbElements
;
415 dest_data
+= psa
->cbElements
;
418 SafeArraySetRecordInfo(dest
, record
);
419 /* This value is set to 32 bytes by default on descriptor creation,
420 update with actual structure size. */
421 dest
->cbElements
= psa
->cbElements
;
422 IRecordInfo_Release(record
);
424 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
426 IUnknown
**dest_unk
= dest
->pvData
;
427 IUnknown
**src_unk
= psa
->pvData
;
429 /* release old iface, addref new one */
430 while (ulCellCount
--)
433 IUnknown_Release(*dest_unk
);
434 *dest_unk
= *src_unk
;
436 IUnknown_AddRef(*dest_unk
);
443 /* Copy the data over */
444 memcpy(dest
->pvData
, psa
->pvData
, ulCellCount
* psa
->cbElements
);
447 if (psa
->fFeatures
& FADF_HAVEIID
)
450 SafeArrayGetIID(psa
, &guid
);
451 SafeArraySetIID(dest
, &guid
);
453 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
455 SAFEARRAY_SetHiddenDWORD(dest
, SAFEARRAY_GetHiddenDWORD(psa
));
462 /*************************************************************************
463 * SafeArrayAllocDescriptor (OLEAUT32.36)
465 * Allocate and initialise a descriptor for a SafeArray.
468 * cDims [I] Number of dimensions of the array
469 * ppsaOut [O] Destination for new descriptor
472 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
473 * Failure: An HRESULT error code indicating the error.
478 HRESULT WINAPI
SafeArrayAllocDescriptor(UINT cDims
, SAFEARRAY
**ppsaOut
)
483 TRACE("(%d,%p)\n", cDims
, ppsaOut
);
485 if (!cDims
|| cDims
>= 0x10000) /* Maximum 65535 dimensions */
491 /* We need enough space for the header and its bounds */
492 allocSize
= sizeof(SAFEARRAY
) + sizeof(SAFEARRAYBOUND
) * (cDims
- 1);
494 hr
= SAFEARRAY_AllocDescriptor(allocSize
, ppsaOut
);
498 (*ppsaOut
)->cDims
= cDims
;
500 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims
, allocSize
);
504 /*************************************************************************
505 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
507 * Allocate and initialise a descriptor for a SafeArray of a given type.
510 * vt [I] The type of items to store in the array
511 * cDims [I] Number of dimensions of the array
512 * ppsaOut [O] Destination for new descriptor
515 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
516 * Failure: An HRESULT error code indicating the error.
519 * - This function does not check that vt is an allowed VARTYPE.
520 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
523 HRESULT WINAPI
SafeArrayAllocDescriptorEx(VARTYPE vt
, UINT cDims
, SAFEARRAY
**ppsaOut
)
528 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, ppsaOut
);
530 cbElements
= SAFEARRAY_GetVTSize(vt
);
532 WARN("Creating a descriptor with an invalid VARTYPE!\n");
534 hRet
= SafeArrayAllocDescriptor(cDims
, ppsaOut
);
538 SAFEARRAY_SetFeatures(vt
, *ppsaOut
);
539 (*ppsaOut
)->cbElements
= cbElements
;
544 /*************************************************************************
545 * SafeArrayAllocData (OLEAUT32.37)
547 * Allocate the data area of a SafeArray.
550 * psa [I] SafeArray to allocate the data area of.
553 * Success: S_OK. The data area is allocated and initialised.
554 * Failure: An HRESULT error code indicating the error.
559 HRESULT WINAPI
SafeArrayAllocData(SAFEARRAY
*psa
)
561 HRESULT hRet
= E_INVALIDARG
;
563 TRACE("(%p)\n", psa
);
567 ULONG ulSize
= SAFEARRAY_GetCellCount(psa
);
569 psa
->pvData
= SAFEARRAY_Malloc(ulSize
* psa
->cbElements
);
574 TRACE("%u bytes allocated for data at %p (%u objects).\n",
575 ulSize
* psa
->cbElements
, psa
->pvData
, ulSize
);
578 hRet
= E_OUTOFMEMORY
;
583 /*************************************************************************
584 * SafeArrayCreate (OLEAUT32.15)
586 * Create a new SafeArray.
589 * vt [I] Type to store in the safe array
590 * cDims [I] Number of array dimensions
591 * rgsabound [I] Bounds of the array dimensions
594 * Success: A pointer to a new array object.
595 * Failure: NULL, if any parameter is invalid or memory allocation fails.
598 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
599 * in the Wine implementation.
602 SAFEARRAY
* WINAPI
SafeArrayCreate(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
)
604 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
);
609 return SAFEARRAY_Create(vt
, cDims
, rgsabound
, 0);
612 /*************************************************************************
613 * SafeArrayCreateEx (OLEAUT32.15)
615 * Create a new SafeArray.
618 * vt [I] Type to store in the safe array
619 * cDims [I] Number of array dimensions
620 * rgsabound [I] Bounds of the array dimensions
621 * pvExtra [I] Extra data
624 * Success: A pointer to a new array object.
625 * Failure: NULL, if any parameter is invalid or memory allocation fails.
630 SAFEARRAY
* WINAPI
SafeArrayCreateEx(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
, LPVOID pvExtra
)
633 IRecordInfo
* iRecInfo
= pvExtra
;
636 TRACE("(%d->%s,%d,%p,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
, pvExtra
);
642 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
644 psa
= SAFEARRAY_Create(vt
, cDims
, rgsabound
, ulSize
);
651 SafeArraySetRecordInfo(psa
, pvExtra
);
655 SafeArraySetIID(psa
, pvExtra
);
662 /************************************************************************
663 * SafeArrayCreateVector (OLEAUT32.411)
665 * Create a one dimensional, contiguous SafeArray.
668 * vt [I] Type to store in the safe array
669 * lLbound [I] Lower bound of the array
670 * cElements [I] Number of elements in the array
673 * Success: A pointer to a new array object.
674 * Failure: NULL, if any parameter is invalid or memory allocation fails.
679 SAFEARRAY
* WINAPI
SafeArrayCreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
)
681 TRACE("(%d->%s,%d,%d\n", vt
, debugstr_vt(vt
), lLbound
, cElements
);
686 return SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, SAFEARRAY_GetVTSize(vt
));
689 /************************************************************************
690 * SafeArrayCreateVectorEx (OLEAUT32.411)
692 * Create a one dimensional, contiguous SafeArray.
695 * vt [I] Type to store in the safe array
696 * lLbound [I] Lower bound of the array
697 * cElements [I] Number of elements in the array
698 * pvExtra [I] Extra data
701 * Success: A pointer to a new array object.
702 * Failure: NULL, if any parameter is invalid or memory allocation fails.
707 SAFEARRAY
* WINAPI
SafeArrayCreateVectorEx(VARTYPE vt
, LONG lLbound
, ULONG cElements
, LPVOID pvExtra
)
710 IRecordInfo
* iRecInfo
= pvExtra
;
713 TRACE("(%d->%s,%d,%d,%p\n", vt
, debugstr_vt(vt
), lLbound
, cElements
, pvExtra
);
719 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
722 ulSize
= SAFEARRAY_GetVTSize(vt
);
724 psa
= SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, ulSize
);
731 SafeArraySetRecordInfo(psa
, iRecInfo
);
735 SafeArraySetIID(psa
, pvExtra
);
742 /*************************************************************************
743 * SafeArrayDestroyDescriptor (OLEAUT32.38)
745 * Destroy a SafeArray.
748 * psa [I] SafeArray to destroy.
751 * Success: S_OK. The resources used by the array are freed.
752 * Failure: An HRESULT error code indicating the error.
757 HRESULT WINAPI
SafeArrayDestroyDescriptor(SAFEARRAY
*psa
)
759 TRACE("(%p)\n", psa
);
763 LPVOID lpv
= (char*)psa
- SAFEARRAY_HIDDEN_SIZE
;
766 return DISP_E_ARRAYISLOCKED
; /* Can't destroy a locked array */
768 if (psa
->fFeatures
& FADF_RECORD
)
769 SafeArraySetRecordInfo(psa
, NULL
);
771 if (psa
->fFeatures
& FADF_CREATEVECTOR
&&
772 !(psa
->fFeatures
& FADF_DATADELETED
))
773 SAFEARRAY_DestroyData(psa
, 0); /* Data not previously deleted */
775 if (!SAFEARRAY_Free(lpv
))
781 /*************************************************************************
782 * SafeArrayLock (OLEAUT32.21)
784 * Increment the lock counter of a SafeArray.
787 * psa [O] SafeArray to lock
790 * Success: S_OK. The array lock is incremented.
791 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
792 * are held on the array at once.
795 * In Win32 these locks are not thread safe.
798 HRESULT WINAPI
SafeArrayLock(SAFEARRAY
*psa
)
802 TRACE("(%p)\n", psa
);
807 ulLocks
= InterlockedIncrement( (LONG
*) &psa
->cLocks
);
809 if (ulLocks
> 0xffff) /* Maximum of 16384 locks at a time */
811 WARN("Out of locks!\n");
812 InterlockedDecrement( (LONG
*) &psa
->cLocks
);
818 /*************************************************************************
819 * SafeArrayUnlock (OLEAUT32.22)
821 * Decrement the lock counter of a SafeArray.
824 * psa [O] SafeArray to unlock
827 * Success: S_OK. The array lock is decremented.
828 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
834 HRESULT WINAPI
SafeArrayUnlock(SAFEARRAY
*psa
)
836 TRACE("(%p)\n", psa
);
841 if (InterlockedDecrement( (LONG
*) &psa
->cLocks
) < 0)
843 WARN("Unlocked but no lock held!\n");
844 InterlockedIncrement( (LONG
*) &psa
->cLocks
);
850 /*************************************************************************
851 * SafeArrayPutElement (OLEAUT32.26)
853 * Put an item into a SafeArray.
856 * psa [I] SafeArray to insert into
857 * rgIndices [I] Indices to insert at
858 * pvData [I] Data to insert
861 * Success: S_OK. The item is inserted
862 * Failure: An HRESULT error code indicating the error.
867 HRESULT WINAPI
SafeArrayPutElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
871 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
873 if (!psa
|| !rgIndices
)
876 hRet
= SafeArrayLock(psa
);
882 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvDest
);
886 if (psa
->fFeatures
& FADF_VARIANT
)
888 VARIANT
* lpVariant
= pvData
;
889 VARIANT
* lpDest
= lpvDest
;
891 hRet
= VariantCopy(lpDest
, lpVariant
);
892 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%x\n", hRet
);
894 else if (psa
->fFeatures
& FADF_BSTR
)
896 BSTR lpBstr
= (BSTR
)pvData
;
897 BSTR
* lpDest
= lpvDest
;
899 SysFreeString(*lpDest
);
901 *lpDest
= SysAllocStringByteLen((char*)lpBstr
, SysStringByteLen(lpBstr
));
903 hRet
= E_OUTOFMEMORY
;
905 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
907 IUnknown
*lpUnknown
= pvData
;
908 IUnknown
**lpDest
= lpvDest
;
911 IUnknown_AddRef(lpUnknown
);
913 IUnknown_Release(*lpDest
);
916 else if (psa
->fFeatures
& FADF_RECORD
)
920 SafeArrayGetRecordInfo(psa
, &record
);
921 hRet
= IRecordInfo_RecordCopy(record
, pvData
, lpvDest
);
922 IRecordInfo_Release(record
);
924 /* Copy the data over */
925 memcpy(lpvDest
, pvData
, psa
->cbElements
);
927 SafeArrayUnlock(psa
);
933 /*************************************************************************
934 * SafeArrayGetElement (OLEAUT32.25)
936 * Get an item from a SafeArray.
939 * psa [I] SafeArray to get from
940 * rgIndices [I] Indices to get from
941 * pvData [O] Destination for data
944 * Success: S_OK. The item data is returned in pvData.
945 * Failure: An HRESULT error code indicating the error.
950 HRESULT WINAPI
SafeArrayGetElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
954 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
956 if (!psa
|| !rgIndices
|| !pvData
)
959 hRet
= SafeArrayLock(psa
);
965 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvSrc
);
969 if (psa
->fFeatures
& FADF_VARIANT
)
971 VARIANT
* lpVariant
= lpvSrc
;
972 VARIANT
* lpDest
= pvData
;
974 /* The original content of pvData is ignored. */
975 V_VT(lpDest
) = VT_EMPTY
;
976 hRet
= VariantCopy(lpDest
, lpVariant
);
977 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%x\n", hRet
);
979 else if (psa
->fFeatures
& FADF_BSTR
)
981 BSTR
* lpBstr
= lpvSrc
;
982 BSTR
* lpDest
= pvData
;
986 *lpDest
= SysAllocStringByteLen((char*)*lpBstr
, SysStringByteLen(*lpBstr
));
988 hRet
= E_OUTOFMEMORY
;
993 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
995 IUnknown
**src_unk
= lpvSrc
;
996 IUnknown
**dest_unk
= pvData
;
999 IUnknown_AddRef(*src_unk
);
1000 *dest_unk
= *src_unk
;
1002 else if (psa
->fFeatures
& FADF_RECORD
)
1004 IRecordInfo
*record
;
1006 SafeArrayGetRecordInfo(psa
, &record
);
1007 hRet
= IRecordInfo_RecordCopy(record
, lpvSrc
, pvData
);
1008 IRecordInfo_Release(record
);
1011 /* Copy the data over */
1012 memcpy(pvData
, lpvSrc
, psa
->cbElements
);
1014 SafeArrayUnlock(psa
);
1019 /*************************************************************************
1020 * SafeArrayGetUBound (OLEAUT32.19)
1022 * Get the upper bound for a given SafeArray dimension
1025 * psa [I] Array to get dimension upper bound from
1026 * nDim [I] The dimension number to get the upper bound of
1027 * plUbound [O] Destination for the upper bound
1030 * Success: S_OK. plUbound contains the dimensions upper bound.
1031 * Failure: An HRESULT error code indicating the error.
1036 HRESULT WINAPI
SafeArrayGetUBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plUbound
)
1038 TRACE("(%p,%d,%p)\n", psa
, nDim
, plUbound
);
1040 if (!psa
|| !plUbound
)
1041 return E_INVALIDARG
;
1043 if(!nDim
|| nDim
> psa
->cDims
)
1044 return DISP_E_BADINDEX
;
1046 *plUbound
= psa
->rgsabound
[psa
->cDims
- nDim
].lLbound
+
1047 psa
->rgsabound
[psa
->cDims
- nDim
].cElements
- 1;
1052 /*************************************************************************
1053 * SafeArrayGetLBound (OLEAUT32.20)
1055 * Get the lower bound for a given SafeArray dimension
1058 * psa [I] Array to get dimension lower bound from
1059 * nDim [I] The dimension number to get the lower bound of
1060 * plLbound [O] Destination for the lower bound
1063 * Success: S_OK. plUbound contains the dimensions lower bound.
1064 * Failure: An HRESULT error code indicating the error.
1069 HRESULT WINAPI
SafeArrayGetLBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plLbound
)
1071 TRACE("(%p,%d,%p)\n", psa
, nDim
, plLbound
);
1073 if (!psa
|| !plLbound
)
1074 return E_INVALIDARG
;
1076 if(!nDim
|| nDim
> psa
->cDims
)
1077 return DISP_E_BADINDEX
;
1079 *plLbound
= psa
->rgsabound
[psa
->cDims
- nDim
].lLbound
;
1083 /*************************************************************************
1084 * SafeArrayGetDim (OLEAUT32.17)
1086 * Get the number of dimensions in a SafeArray.
1089 * psa [I] Array to get the dimensions of
1092 * The number of array dimensions in psa, or 0 if psa is NULL.
1097 UINT WINAPI
SafeArrayGetDim(SAFEARRAY
*psa
)
1099 TRACE("(%p) returning %d\n", psa
, psa
? psa
->cDims
: 0u);
1100 return psa
? psa
->cDims
: 0;
1103 /*************************************************************************
1104 * SafeArrayGetElemsize (OLEAUT32.18)
1106 * Get the size of an element in a SafeArray.
1109 * psa [I] Array to get the element size from
1112 * The size of a single element in psa, or 0 if psa is NULL.
1117 UINT WINAPI
SafeArrayGetElemsize(SAFEARRAY
*psa
)
1119 TRACE("(%p) returning %d\n", psa
, psa
? psa
->cbElements
: 0u);
1120 return psa
? psa
->cbElements
: 0;
1123 /*************************************************************************
1124 * SafeArrayAccessData (OLEAUT32.23)
1126 * Lock a SafeArray and return a pointer to its data.
1129 * psa [I] Array to get the data pointer from
1130 * ppvData [O] Destination for the arrays data pointer
1133 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1135 * Failure: An HRESULT error code indicating the error.
1140 HRESULT WINAPI
SafeArrayAccessData(SAFEARRAY
*psa
, void **ppvData
)
1144 TRACE("(%p,%p)\n", psa
, ppvData
);
1146 if(!psa
|| !ppvData
)
1147 return E_INVALIDARG
;
1149 hr
= SafeArrayLock(psa
);
1150 *ppvData
= SUCCEEDED(hr
) ? psa
->pvData
: NULL
;
1156 /*************************************************************************
1157 * SafeArrayUnaccessData (OLEAUT32.24)
1159 * Unlock a SafeArray after accessing its data.
1162 * psa [I] Array to unlock
1165 * Success: S_OK. The array is unlocked.
1166 * Failure: An HRESULT error code indicating the error.
1171 HRESULT WINAPI
SafeArrayUnaccessData(SAFEARRAY
*psa
)
1173 TRACE("(%p)\n", psa
);
1174 return SafeArrayUnlock(psa
);
1177 /************************************************************************
1178 * SafeArrayPtrOfIndex (OLEAUT32.148)
1180 * Get the address of an item in a SafeArray.
1183 * psa [I] Array to get the items address from
1184 * rgIndices [I] Index of the item in the array
1185 * ppvData [O] Destination for item address
1188 * Success: S_OK. ppvData contains a pointer to the item.
1189 * Failure: An HRESULT error code indicating the error.
1192 * This function does not lock the array.
1197 HRESULT WINAPI
SafeArrayPtrOfIndex(SAFEARRAY
*psa
, LONG
*rgIndices
, void **ppvData
)
1200 ULONG cell
= 0, dimensionSize
= 1;
1201 SAFEARRAYBOUND
* psab
;
1204 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, ppvData
);
1206 /* The general formula for locating the cell number of an entry in an n
1207 * dimensional array (where cn = coordinate in dimension dn) is:
1209 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1211 * We calculate the size of the last dimension at each step through the
1212 * dimensions to avoid recursing to calculate the last dimensions size.
1214 if (!psa
|| !rgIndices
|| !ppvData
)
1215 return E_INVALIDARG
;
1217 psab
= psa
->rgsabound
+ psa
->cDims
- 1;
1220 if (c1
< psab
->lLbound
|| c1
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1221 return DISP_E_BADINDEX
; /* Initial index out of bounds */
1223 for (dim
= 1; dim
< psa
->cDims
; dim
++)
1225 dimensionSize
*= psab
->cElements
;
1229 if (!psab
->cElements
||
1230 *rgIndices
< psab
->lLbound
||
1231 *rgIndices
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1232 return DISP_E_BADINDEX
; /* Index out of bounds */
1234 cell
+= (*rgIndices
- psab
->lLbound
) * dimensionSize
;
1238 cell
+= (c1
- psa
->rgsabound
[psa
->cDims
- 1].lLbound
);
1240 *ppvData
= (char*)psa
->pvData
+ cell
* psa
->cbElements
;
1244 /************************************************************************
1245 * SafeArrayDestroyData (OLEAUT32.39)
1247 * Destroy the data associated with a SafeArray.
1250 * psa [I] Array to delete the data from
1253 * Success: S_OK. All items and the item data are freed.
1254 * Failure: An HRESULT error code indicating the error.
1259 HRESULT WINAPI
SafeArrayDestroyData(SAFEARRAY
*psa
)
1263 TRACE("(%p)\n", psa
);
1266 return E_INVALIDARG
;
1269 return DISP_E_ARRAYISLOCKED
; /* Can't delete a locked array */
1271 /* Delete the actual item data */
1272 hr
= SAFEARRAY_DestroyData(psa
, 0);
1278 if (psa
->fFeatures
& FADF_STATIC
)
1280 ZeroMemory(psa
->pvData
, SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1283 /* If this is not a vector, free the data memory block */
1284 if (!(psa
->fFeatures
& FADF_CREATEVECTOR
))
1286 if (!SAFEARRAY_Free(psa
->pvData
))
1287 return E_UNEXPECTED
;
1291 psa
->fFeatures
|= FADF_DATADELETED
; /* Mark the data deleted */
1297 /************************************************************************
1298 * SafeArrayCopyData (OLEAUT32.412)
1300 * Copy all data from one SafeArray to another.
1303 * psaSource [I] Source for copy
1304 * psaTarget [O] Destination for copy
1307 * Success: S_OK. psaTarget contains a copy of psaSource.
1308 * Failure: An HRESULT error code indicating the error.
1311 * The two arrays must have the same number of dimensions and elements.
1316 HRESULT WINAPI
SafeArrayCopyData(SAFEARRAY
*psaSource
, SAFEARRAY
*psaTarget
)
1320 TRACE("(%p,%p)\n", psaSource
, psaTarget
);
1322 if (!psaSource
|| !psaTarget
||
1323 psaSource
->cDims
!= psaTarget
->cDims
||
1324 psaSource
->cbElements
!= psaTarget
->cbElements
)
1325 return E_INVALIDARG
;
1327 /* Each dimension must be the same size */
1328 for (dim
= psaSource
->cDims
- 1; dim
>= 0 ; dim
--)
1329 if (psaSource
->rgsabound
[dim
].cElements
!=
1330 psaTarget
->rgsabound
[dim
].cElements
)
1331 return E_INVALIDARG
;
1333 return SAFEARRAY_CopyData(psaSource
, psaTarget
);
1336 /************************************************************************
1337 * SafeArrayDestroy (OLEAUT32.16)
1339 * Destroy a SafeArray.
1342 * psa [I] Array to destroy
1345 * Success: S_OK. All resources used by the array are freed.
1346 * Failure: An HRESULT error code indicating the error.
1351 HRESULT WINAPI
SafeArrayDestroy(SAFEARRAY
*psa
)
1353 TRACE("(%p)\n", psa
);
1359 return DISP_E_ARRAYISLOCKED
;
1361 /* Native doesn't check to see if the free succeeds */
1362 SafeArrayDestroyData(psa
);
1363 SafeArrayDestroyDescriptor(psa
);
1367 /************************************************************************
1368 * SafeArrayCopy (OLEAUT32.27)
1370 * Make a duplicate of a SafeArray.
1373 * psa [I] Source for copy
1374 * ppsaOut [O] Destination for new copy
1377 * Success: S_OK. ppsaOut contains a copy of the array.
1378 * Failure: An HRESULT error code indicating the error.
1383 HRESULT WINAPI
SafeArrayCopy(SAFEARRAY
*psa
, SAFEARRAY
**ppsaOut
)
1387 TRACE("(%p,%p)\n", psa
, ppsaOut
);
1390 return E_INVALIDARG
;
1395 return S_OK
; /* Handles copying of NULL arrays */
1397 if (!psa
->cbElements
)
1398 return E_INVALIDARG
;
1400 if (psa
->fFeatures
& (FADF_RECORD
|FADF_HAVEIID
|FADF_HAVEVARTYPE
))
1404 hRet
= SafeArrayGetVartype(psa
, &vt
);
1405 if (SUCCEEDED(hRet
))
1406 hRet
= SafeArrayAllocDescriptorEx(vt
, psa
->cDims
, ppsaOut
);
1410 hRet
= SafeArrayAllocDescriptor(psa
->cDims
, ppsaOut
);
1411 if (SUCCEEDED(hRet
))
1413 (*ppsaOut
)->fFeatures
= psa
->fFeatures
& ~ignored_copy_features
;
1414 (*ppsaOut
)->cbElements
= psa
->cbElements
;
1418 if (SUCCEEDED(hRet
))
1420 /* Copy dimension bounds */
1421 memcpy((*ppsaOut
)->rgsabound
, psa
->rgsabound
, psa
->cDims
* sizeof(SAFEARRAYBOUND
));
1423 (*ppsaOut
)->pvData
= SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1424 if (!(*ppsaOut
)->pvData
)
1426 SafeArrayDestroyDescriptor(*ppsaOut
);
1428 return E_OUTOFMEMORY
;
1431 hRet
= SAFEARRAY_CopyData(psa
, *ppsaOut
);
1434 SAFEARRAY_Free((*ppsaOut
)->pvData
);
1435 SafeArrayDestroyDescriptor(*ppsaOut
);
1444 /************************************************************************
1445 * SafeArrayRedim (OLEAUT32.40)
1447 * Changes the characteristics of the last dimension of a SafeArray
1450 * psa [I] Array to change
1451 * psabound [I] New bound details for the last dimension
1454 * Success: S_OK. psa is updated to reflect the new bounds.
1455 * Failure: An HRESULT error code indicating the error.
1460 HRESULT WINAPI
SafeArrayRedim(SAFEARRAY
*psa
, SAFEARRAYBOUND
*psabound
)
1462 SAFEARRAYBOUND
*oldBounds
;
1465 TRACE("(%p,%p)\n", psa
, psabound
);
1467 if (!psa
|| psa
->fFeatures
& FADF_FIXEDSIZE
|| !psabound
)
1468 return E_INVALIDARG
;
1470 if (psa
->cLocks
> 0)
1471 return DISP_E_ARRAYISLOCKED
;
1473 hr
= SafeArrayLock(psa
);
1477 oldBounds
= psa
->rgsabound
;
1478 oldBounds
->lLbound
= psabound
->lLbound
;
1480 if (psabound
->cElements
!= oldBounds
->cElements
)
1482 if (psabound
->cElements
< oldBounds
->cElements
)
1484 /* Shorten the final dimension. */
1485 ULONG ulStartCell
= psabound
->cElements
*
1486 (SAFEARRAY_GetCellCount(psa
) / oldBounds
->cElements
);
1487 SAFEARRAY_DestroyData(psa
, ulStartCell
);
1491 /* Lengthen the final dimension */
1492 ULONG ulOldSize
, ulNewSize
;
1495 ulOldSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1497 ulNewSize
= (ulOldSize
/ oldBounds
->cElements
) * psabound
->cElements
;
1499 int oldelems
= oldBounds
->cElements
;
1500 oldBounds
->cElements
= psabound
->cElements
;
1501 ulNewSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1502 oldBounds
->cElements
= oldelems
;
1505 if (!(pvNewData
= SAFEARRAY_Malloc(ulNewSize
)))
1507 SafeArrayUnlock(psa
);
1508 return E_OUTOFMEMORY
;
1511 memcpy(pvNewData
, psa
->pvData
, ulOldSize
);
1512 SAFEARRAY_Free(psa
->pvData
);
1513 psa
->pvData
= pvNewData
;
1515 oldBounds
->cElements
= psabound
->cElements
;
1518 SafeArrayUnlock(psa
);
1522 /************************************************************************
1523 * SafeArrayGetVartype (OLEAUT32.77)
1525 * Get the type of the items in a SafeArray.
1528 * psa [I] Array to get the type from
1529 * pvt [O] Destination for the type
1532 * Success: S_OK. pvt contains the type of the items.
1533 * Failure: An HRESULT error code indicating the error.
1538 HRESULT WINAPI
SafeArrayGetVartype(SAFEARRAY
* psa
, VARTYPE
* pvt
)
1540 TRACE("(%p,%p)\n", psa
, pvt
);
1543 return E_INVALIDARG
;
1545 if (psa
->fFeatures
& FADF_RECORD
)
1547 else if ((psa
->fFeatures
& (FADF_HAVEIID
|FADF_DISPATCH
)) == (FADF_HAVEIID
|FADF_DISPATCH
))
1549 else if (psa
->fFeatures
& FADF_HAVEIID
)
1551 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
1553 VARTYPE vt
= SAFEARRAY_GetHiddenDWORD(psa
);
1557 return E_INVALIDARG
;
1562 /************************************************************************
1563 * SafeArraySetRecordInfo (OLEAUT32.@)
1565 * Set the record info for a SafeArray.
1568 * psa [I] Array to set the record info for
1569 * pRinfo [I] Record info
1572 * Success: S_OK. The record info is stored with the array.
1573 * Failure: An HRESULT error code indicating the error.
1578 HRESULT WINAPI
SafeArraySetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
*pRinfo
)
1580 IRecordInfo
** dest
= (IRecordInfo
**)psa
;
1582 TRACE("(%p,%p)\n", psa
, pRinfo
);
1584 if (!psa
|| !(psa
->fFeatures
& FADF_RECORD
))
1585 return E_INVALIDARG
;
1588 IRecordInfo_AddRef(pRinfo
);
1591 IRecordInfo_Release(dest
[-1]);
1597 /************************************************************************
1598 * SafeArrayGetRecordInfo (OLEAUT32.@)
1600 * Get the record info from a SafeArray.
1603 * psa [I] Array to get the record info from
1604 * pRinfo [O] Destination for the record info
1607 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1608 * Failure: An HRESULT error code indicating the error.
1613 HRESULT WINAPI
SafeArrayGetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
**pRinfo
)
1615 IRecordInfo
** src
= (IRecordInfo
**)psa
;
1617 TRACE("(%p,%p)\n", psa
, pRinfo
);
1619 if (!psa
|| !pRinfo
|| !(psa
->fFeatures
& FADF_RECORD
))
1620 return E_INVALIDARG
;
1625 IRecordInfo_AddRef(*pRinfo
);
1629 /************************************************************************
1630 * SafeArraySetIID (OLEAUT32.@)
1632 * Set the IID for a SafeArray.
1635 * psa [I] Array to set the IID from
1639 * Success: S_OK. The IID is stored with the array
1640 * Failure: An HRESULT error code indicating the error.
1645 HRESULT WINAPI
SafeArraySetIID(SAFEARRAY
*psa
, REFGUID guid
)
1647 GUID
* dest
= (GUID
*)psa
;
1649 TRACE("(%p,%s)\n", psa
, debugstr_guid(guid
));
1651 if (!psa
|| !guid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1652 return E_INVALIDARG
;
1658 /************************************************************************
1659 * SafeArrayGetIID (OLEAUT32.@)
1661 * Get the IID from a SafeArray.
1664 * psa [I] Array to get the ID from
1665 * pGuid [O] Destination for the IID
1668 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1669 * Failure: An HRESULT error code indicating the error.
1674 HRESULT WINAPI
SafeArrayGetIID(SAFEARRAY
*psa
, GUID
*pGuid
)
1676 GUID
* src
= (GUID
*)psa
;
1678 TRACE("(%p,%p)\n", psa
, pGuid
);
1680 if (!psa
|| !pGuid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1681 return E_INVALIDARG
;
1687 /************************************************************************
1688 * VectorFromBstr (OLEAUT32.@)
1690 * Create a SafeArray Vector from the bytes of a BSTR.
1693 * bstr [I] String to get bytes from
1694 * ppsa [O] Destination for the array
1697 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1698 * Failure: An HRESULT error code indicating the error.
1703 HRESULT WINAPI
VectorFromBstr(BSTR bstr
, SAFEARRAY
**ppsa
)
1707 TRACE("(%p,%p)\n", bstr
, ppsa
);
1710 return E_INVALIDARG
;
1713 sab
.cElements
= SysStringByteLen(bstr
);
1715 *ppsa
= SAFEARRAY_Create(VT_UI1
, 1, &sab
, 0);
1719 memcpy((*ppsa
)->pvData
, bstr
, sab
.cElements
);
1722 return E_OUTOFMEMORY
;
1725 /************************************************************************
1726 * BstrFromVector (OLEAUT32.@)
1728 * Create a BSTR from a SafeArray.
1731 * psa [I] Source array
1732 * pbstr [O] Destination for output BSTR
1735 * Success: S_OK. pbstr contains the arrays data.
1736 * Failure: An HRESULT error code indicating the error.
1739 * psa must be a 1 dimensional array of a 1 byte type.
1744 HRESULT WINAPI
BstrFromVector(SAFEARRAY
*psa
, BSTR
*pbstr
)
1746 TRACE("(%p,%p)\n", psa
, pbstr
);
1749 return E_INVALIDARG
;
1753 if (!psa
|| psa
->cbElements
!= 1 || psa
->cDims
!= 1)
1754 return E_INVALIDARG
;
1756 *pbstr
= SysAllocStringByteLen(psa
->pvData
, psa
->rgsabound
[0].cElements
);
1758 return E_OUTOFMEMORY
;