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...]
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(variant
);
48 /************************************************************************
49 * SafeArray {OLEAUT32}
52 * The SafeArray data type provides the underlying interface for Ole
53 * Automations arrays, used for example to represent array types in
54 * Visual Basic(tm) and to gather user defined parameters for invocation through
55 * an IDispatch interface.
57 * Safe arrays provide bounds checking and automatically manage the data
58 * types they contain, for example handing reference counting and copying
59 * of interface pointers. User defined types can be stored in arrays
60 * using the IRecordInfo interface.
62 * There are two types of SafeArray, normal and vectors. Normal arrays can have
63 * multiple dimensions and the data for the array is allocated separately from
64 * the array header. This is the most flexible type of array. Vectors, on the
65 * other hand, are fixed in size and consist of a single allocated block, and a
69 * The following types of data can be stored within a SafeArray.
71 *| VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8, VT_UI8, VT_INT, VT_UINT,
72 *| VT_R4, VT_R8, VT_CY, VT_DECIMAL
74 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
76 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
83 /* Undocumented hidden space before the start of a SafeArray descriptor */
84 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
86 /* features listed here are not propagated to newly created array or data copy
87 created with SafeArrayCopy()/SafeArrayCopyData() */
88 static const USHORT ignored_copy_features
=
96 static inline void* SAFEARRAY_Malloc(ULONG size
)
98 void *ret
= CoTaskMemAlloc(size
);
100 memset(ret
, 0, size
);
105 static inline void SAFEARRAY_Free(void *ptr
)
110 /* Get the size of a supported VT type (0 means unsupported) */
111 static DWORD
SAFEARRAY_GetVTSize(VARTYPE vt
)
116 case VT_UI1
: return sizeof(BYTE
);
119 case VT_UI2
: return sizeof(SHORT
);
123 case VT_ERROR
: return sizeof(LONG
);
126 case VT_UI8
: return sizeof(LONG64
);
128 case VT_UINT
: return sizeof(INT
);
130 case VT_UINT_PTR
: return sizeof(UINT_PTR
);
131 case VT_CY
: return sizeof(CY
);
132 case VT_DATE
: return sizeof(DATE
);
133 case VT_BSTR
: return sizeof(BSTR
);
134 case VT_DISPATCH
: return sizeof(LPDISPATCH
);
135 case VT_VARIANT
: return sizeof(VARIANT
);
136 case VT_UNKNOWN
: return sizeof(LPUNKNOWN
);
137 case VT_DECIMAL
: return sizeof(DECIMAL
);
138 /* Note: Return a non-zero size to indicate vt is valid. The actual size
139 * of a UDT is taken from the result of IRecordInfo_GetSize().
141 case VT_RECORD
: return 32;
146 /* Set the hidden data for an array */
147 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY
* psa
, DWORD dw
)
149 /* Implementation data is stored in the 4 bytes before the header */
150 LPDWORD lpDw
= (LPDWORD
)psa
;
154 /* Get the hidden data from an array */
155 static inline DWORD
SAFEARRAY_GetHiddenDWORD(SAFEARRAY
* psa
)
157 LPDWORD lpDw
= (LPDWORD
)psa
;
161 /* Get the number of cells in a SafeArray */
162 static ULONG
SAFEARRAY_GetCellCount(const SAFEARRAY
*psa
)
164 const SAFEARRAYBOUND
* psab
= psa
->rgsabound
;
165 USHORT cCount
= psa
->cDims
;
166 ULONG ulNumCells
= 1;
170 /* This is a valid bordercase. See testcases. -Marcus */
171 if (!psab
->cElements
)
173 ulNumCells
*= psab
->cElements
;
179 /* Allocate a descriptor for an array */
180 static HRESULT
SAFEARRAY_AllocDescriptor(ULONG ulSize
, SAFEARRAY
**ppsaOut
)
182 char *ptr
= SAFEARRAY_Malloc(ulSize
+ SAFEARRAY_HIDDEN_SIZE
);
187 return E_OUTOFMEMORY
;
190 *ppsaOut
= (SAFEARRAY
*)(ptr
+ SAFEARRAY_HIDDEN_SIZE
);
194 /* Set the features of an array */
195 static void SAFEARRAY_SetFeatures(VARTYPE vt
, SAFEARRAY
*psa
)
197 /* Set the IID if we have one, otherwise set the type */
198 if (vt
== VT_DISPATCH
)
200 psa
->fFeatures
= FADF_HAVEIID
;
201 SafeArraySetIID(psa
, &IID_IDispatch
);
203 else if (vt
== VT_UNKNOWN
)
205 psa
->fFeatures
= FADF_HAVEIID
;
206 SafeArraySetIID(psa
, &IID_IUnknown
);
208 else if (vt
== VT_RECORD
)
209 psa
->fFeatures
= FADF_RECORD
;
212 psa
->fFeatures
= FADF_HAVEVARTYPE
;
213 SAFEARRAY_SetHiddenDWORD(psa
, vt
);
217 /* Create an array */
218 static SAFEARRAY
* SAFEARRAY_Create(VARTYPE vt
, UINT cDims
, const SAFEARRAYBOUND
*rgsabound
, ULONG ulSize
)
220 SAFEARRAY
*psa
= NULL
;
226 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt
, cDims
, &psa
)))
230 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
231 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
232 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
233 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
236 for (i
= 0; i
< cDims
; i
++)
237 memcpy(psa
->rgsabound
+ i
, rgsabound
+ cDims
- 1 - i
, sizeof(SAFEARRAYBOUND
));
240 psa
->cbElements
= ulSize
;
242 if (!psa
->cbElements
|| FAILED(SafeArrayAllocData(psa
)))
244 SafeArrayDestroyDescriptor(psa
);
251 /* Create an array as a vector */
252 static SAFEARRAY
* SAFEARRAY_CreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
, ULONG ulSize
)
254 SAFEARRAY
*psa
= NULL
;
256 if (ulSize
|| (vt
== VT_RECORD
))
258 /* Allocate the header and data together */
259 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY
) + ulSize
* cElements
, &psa
)))
261 SAFEARRAY_SetFeatures(vt
, psa
);
264 psa
->fFeatures
|= FADF_CREATEVECTOR
;
265 psa
->pvData
= &psa
[1]; /* Data follows the header */
266 psa
->cbElements
= ulSize
;
267 psa
->rgsabound
[0].cElements
= cElements
;
268 psa
->rgsabound
[0].lLbound
= lLbound
;
272 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
273 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
274 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
275 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
282 /* Free data items in an array */
283 static HRESULT
SAFEARRAY_DestroyData(SAFEARRAY
*psa
, ULONG ulStartCell
)
285 if (psa
->pvData
&& !(psa
->fFeatures
& FADF_DATADELETED
))
287 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
289 if (ulStartCell
> ulCellCount
) {
290 FIXME("unexpected ulCellCount %d, start %d\n",ulCellCount
,ulStartCell
);
294 ulCellCount
-= ulStartCell
;
296 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
298 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)psa
->pvData
+ ulStartCell
;
303 IUnknown_Release(*lpUnknown
);
307 else if (psa
->fFeatures
& FADF_RECORD
)
309 IRecordInfo
*lpRecInfo
;
311 if (SUCCEEDED(SafeArrayGetRecordInfo(psa
, &lpRecInfo
)))
313 PBYTE pRecordData
= psa
->pvData
;
316 IRecordInfo_RecordClear(lpRecInfo
, pRecordData
);
317 pRecordData
+= psa
->cbElements
;
319 IRecordInfo_Release(lpRecInfo
);
322 else if (psa
->fFeatures
& FADF_BSTR
)
324 BSTR
* lpBstr
= (BSTR
*)psa
->pvData
+ ulStartCell
;
328 SysFreeString(*lpBstr
);
332 else if (psa
->fFeatures
& FADF_VARIANT
)
334 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
+ ulStartCell
;
338 HRESULT hRet
= VariantClear(lpVariant
);
340 if (FAILED(hRet
)) FIXME("VariantClear of element failed!\n");
348 /* Copy data items from one array to another. Destination data is freed before copy. */
349 static HRESULT
SAFEARRAY_CopyData(SAFEARRAY
*psa
, SAFEARRAY
*dest
)
356 if (!dest
->pvData
|| psa
->fFeatures
& FADF_DATADELETED
)
360 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
362 dest
->fFeatures
= (dest
->fFeatures
& FADF_CREATEVECTOR
) | (psa
->fFeatures
& ~ignored_copy_features
);
364 if (psa
->fFeatures
& FADF_VARIANT
)
366 VARIANT
*src_var
= psa
->pvData
;
367 VARIANT
*dest_var
= dest
->pvData
;
373 /* destination is cleared automatically */
374 hRet
= VariantCopy(dest_var
, src_var
);
375 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%08x, element %u\n", hRet
, ulCellCount
);
380 else if (psa
->fFeatures
& FADF_BSTR
)
382 BSTR
*src_bstr
= psa
->pvData
;
383 BSTR
*dest_bstr
= dest
->pvData
;
387 SysFreeString(*dest_bstr
);
390 *dest_bstr
= SysAllocStringByteLen((char*)*src_bstr
, SysStringByteLen(*src_bstr
));
392 return E_OUTOFMEMORY
;
400 else if (psa
->fFeatures
& FADF_RECORD
)
402 BYTE
*dest_data
= dest
->pvData
;
403 BYTE
*src_data
= psa
->pvData
;
406 SafeArrayGetRecordInfo(psa
, &record
);
407 while (ulCellCount
--)
409 /* RecordCopy() clears destination record */
410 hr
= IRecordInfo_RecordCopy(record
, src_data
, dest_data
);
411 if (FAILED(hr
)) break;
412 src_data
+= psa
->cbElements
;
413 dest_data
+= psa
->cbElements
;
416 SafeArraySetRecordInfo(dest
, record
);
417 /* This value is set to 32 bytes by default on descriptor creation,
418 update with actual structure size. */
419 dest
->cbElements
= psa
->cbElements
;
420 IRecordInfo_Release(record
);
422 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
424 IUnknown
**dest_unk
= dest
->pvData
;
425 IUnknown
**src_unk
= psa
->pvData
;
427 /* release old iface, addref new one */
428 while (ulCellCount
--)
431 IUnknown_Release(*dest_unk
);
432 *dest_unk
= *src_unk
;
434 IUnknown_AddRef(*dest_unk
);
441 /* Copy the data over */
442 memcpy(dest
->pvData
, psa
->pvData
, ulCellCount
* psa
->cbElements
);
445 if (psa
->fFeatures
& FADF_HAVEIID
)
448 SafeArrayGetIID(psa
, &guid
);
449 SafeArraySetIID(dest
, &guid
);
451 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
453 SAFEARRAY_SetHiddenDWORD(dest
, SAFEARRAY_GetHiddenDWORD(psa
));
460 /*************************************************************************
461 * SafeArrayAllocDescriptor (OLEAUT32.36)
463 * Allocate and initialise a descriptor for a SafeArray.
466 * cDims [I] Number of dimensions of the array
467 * ppsaOut [O] Destination for new descriptor
470 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
471 * Failure: An HRESULT error code indicating the error.
476 HRESULT WINAPI
SafeArrayAllocDescriptor(UINT cDims
, SAFEARRAY
**ppsaOut
)
481 TRACE("(%d,%p)\n", cDims
, ppsaOut
);
483 if (!cDims
|| cDims
>= 0x10000) /* Maximum 65535 dimensions */
489 /* We need enough space for the header and its bounds */
490 allocSize
= sizeof(SAFEARRAY
) + sizeof(SAFEARRAYBOUND
) * (cDims
- 1);
492 hr
= SAFEARRAY_AllocDescriptor(allocSize
, ppsaOut
);
496 (*ppsaOut
)->cDims
= cDims
;
498 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims
, allocSize
);
502 /*************************************************************************
503 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
505 * Allocate and initialise a descriptor for a SafeArray of a given type.
508 * vt [I] The type of items to store in the array
509 * cDims [I] Number of dimensions of the array
510 * ppsaOut [O] Destination for new descriptor
513 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
514 * Failure: An HRESULT error code indicating the error.
517 * - This function does not check that vt is an allowed VARTYPE.
518 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
521 HRESULT WINAPI
SafeArrayAllocDescriptorEx(VARTYPE vt
, UINT cDims
, SAFEARRAY
**ppsaOut
)
526 TRACE("(%s,%u,%p)\n", debugstr_vt(vt
), cDims
, ppsaOut
);
528 cbElements
= SAFEARRAY_GetVTSize(vt
);
530 WARN("Creating a descriptor with an invalid VARTYPE!\n");
532 hRet
= SafeArrayAllocDescriptor(cDims
, ppsaOut
);
536 SAFEARRAY_SetFeatures(vt
, *ppsaOut
);
537 (*ppsaOut
)->cbElements
= cbElements
;
542 /*************************************************************************
543 * SafeArrayAllocData (OLEAUT32.37)
545 * Allocate the data area of a SafeArray.
548 * psa [I] SafeArray to allocate the data area of.
551 * Success: S_OK. The data area is allocated and initialised.
552 * Failure: An HRESULT error code indicating the error.
557 HRESULT WINAPI
SafeArrayAllocData(SAFEARRAY
*psa
)
559 HRESULT hRet
= E_INVALIDARG
;
561 TRACE("(%p)\n", psa
);
565 ULONG ulSize
= SAFEARRAY_GetCellCount(psa
);
567 psa
->pvData
= SAFEARRAY_Malloc(ulSize
* psa
->cbElements
);
572 TRACE("%u bytes allocated for data at %p (%u objects).\n",
573 ulSize
* psa
->cbElements
, psa
->pvData
, ulSize
);
576 hRet
= E_OUTOFMEMORY
;
581 /*************************************************************************
582 * SafeArrayCreate (OLEAUT32.15)
584 * Create a new SafeArray.
587 * vt [I] Type to store in the safe array
588 * cDims [I] Number of array dimensions
589 * rgsabound [I] Bounds of the array dimensions
592 * Success: A pointer to a new array object.
593 * Failure: NULL, if any parameter is invalid or memory allocation fails.
596 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
597 * in the Wine implementation.
600 SAFEARRAY
* WINAPI
SafeArrayCreate(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
)
602 TRACE("(%s,%u,%p)\n", debugstr_vt(vt
), cDims
, rgsabound
);
607 return SAFEARRAY_Create(vt
, cDims
, rgsabound
, 0);
610 /*************************************************************************
611 * SafeArrayCreateEx (OLEAUT32.15)
613 * Create a new SafeArray.
616 * vt [I] Type to store in the safe array
617 * cDims [I] Number of array dimensions
618 * rgsabound [I] Bounds of the array dimensions
619 * pvExtra [I] Extra data
622 * Success: A pointer to a new array object.
623 * Failure: NULL, if any parameter is invalid or memory allocation fails.
628 SAFEARRAY
* WINAPI
SafeArrayCreateEx(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
, LPVOID pvExtra
)
631 IRecordInfo
* iRecInfo
= pvExtra
;
634 TRACE("(%s,%u,%p,%p)\n", debugstr_vt(vt
), cDims
, rgsabound
, pvExtra
);
640 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
642 psa
= SAFEARRAY_Create(vt
, cDims
, rgsabound
, ulSize
);
649 SafeArraySetRecordInfo(psa
, pvExtra
);
653 SafeArraySetIID(psa
, pvExtra
);
660 /************************************************************************
661 * SafeArrayCreateVector (OLEAUT32.411)
663 * Create a one dimensional, contiguous SafeArray.
666 * vt [I] Type to store in the safe array
667 * lLbound [I] Lower bound of the array
668 * cElements [I] Number of elements in the array
671 * Success: A pointer to a new array object.
672 * Failure: NULL, if any parameter is invalid or memory allocation fails.
677 SAFEARRAY
* WINAPI
SafeArrayCreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
)
679 TRACE("(%s,%d,%u)\n", debugstr_vt(vt
), lLbound
, cElements
);
684 return SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, SAFEARRAY_GetVTSize(vt
));
687 /************************************************************************
688 * SafeArrayCreateVectorEx (OLEAUT32.411)
690 * Create a one dimensional, contiguous SafeArray.
693 * vt [I] Type to store in the safe array
694 * lLbound [I] Lower bound of the array
695 * cElements [I] Number of elements in the array
696 * pvExtra [I] Extra data
699 * Success: A pointer to a new array object.
700 * Failure: NULL, if any parameter is invalid or memory allocation fails.
705 SAFEARRAY
* WINAPI
SafeArrayCreateVectorEx(VARTYPE vt
, LONG lLbound
, ULONG cElements
, LPVOID pvExtra
)
708 IRecordInfo
* iRecInfo
= pvExtra
;
711 TRACE("(%s,%d,%u,%p)\n", debugstr_vt(vt
), lLbound
, cElements
, pvExtra
);
717 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
720 ulSize
= SAFEARRAY_GetVTSize(vt
);
722 psa
= SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, ulSize
);
729 SafeArraySetRecordInfo(psa
, iRecInfo
);
733 SafeArraySetIID(psa
, pvExtra
);
740 /*************************************************************************
741 * SafeArrayDestroyDescriptor (OLEAUT32.38)
743 * Destroy a SafeArray.
746 * psa [I] SafeArray to destroy.
749 * Success: S_OK. The resources used by the array are freed.
750 * Failure: An HRESULT error code indicating the error.
755 HRESULT WINAPI
SafeArrayDestroyDescriptor(SAFEARRAY
*psa
)
757 TRACE("(%p)\n", psa
);
761 LPVOID lpv
= (char*)psa
- SAFEARRAY_HIDDEN_SIZE
;
764 return DISP_E_ARRAYISLOCKED
; /* Can't destroy a locked array */
766 if (psa
->fFeatures
& FADF_RECORD
)
767 SafeArraySetRecordInfo(psa
, NULL
);
769 if (psa
->fFeatures
& FADF_CREATEVECTOR
&&
770 !(psa
->fFeatures
& FADF_DATADELETED
))
771 SAFEARRAY_DestroyData(psa
, 0); /* Data not previously deleted */
778 /*************************************************************************
779 * SafeArrayLock (OLEAUT32.21)
781 * Increment the lock counter of a SafeArray.
784 * psa [O] SafeArray to lock
787 * Success: S_OK. The array lock is incremented.
788 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
789 * are held on the array at once.
792 * In Win32 these locks are not thread safe.
795 HRESULT WINAPI
SafeArrayLock(SAFEARRAY
*psa
)
799 TRACE("(%p)\n", psa
);
804 ulLocks
= InterlockedIncrement( (LONG
*) &psa
->cLocks
);
806 if (ulLocks
> 0xffff) /* Maximum of 16384 locks at a time */
808 WARN("Out of locks!\n");
809 InterlockedDecrement( (LONG
*) &psa
->cLocks
);
815 /*************************************************************************
816 * SafeArrayUnlock (OLEAUT32.22)
818 * Decrement the lock counter of a SafeArray.
821 * psa [O] SafeArray to unlock
824 * Success: S_OK. The array lock is decremented.
825 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
831 HRESULT WINAPI
SafeArrayUnlock(SAFEARRAY
*psa
)
833 TRACE("(%p)\n", psa
);
838 if (InterlockedDecrement( (LONG
*) &psa
->cLocks
) < 0)
840 WARN("Unlocked but no lock held!\n");
841 InterlockedIncrement( (LONG
*) &psa
->cLocks
);
847 /*************************************************************************
848 * SafeArrayPutElement (OLEAUT32.26)
850 * Put an item into a SafeArray.
853 * psa [I] SafeArray to insert into
854 * rgIndices [I] Indices to insert at
855 * pvData [I] Data to insert
858 * Success: S_OK. The item is inserted
859 * Failure: An HRESULT error code indicating the error.
864 HRESULT WINAPI
SafeArrayPutElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
868 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
870 if (!psa
|| !rgIndices
)
873 hRet
= SafeArrayLock(psa
);
879 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvDest
);
883 if (psa
->fFeatures
& FADF_VARIANT
)
885 VARIANT
* lpVariant
= pvData
;
886 VARIANT
* lpDest
= lpvDest
;
888 hRet
= VariantCopy(lpDest
, lpVariant
);
889 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%x\n", hRet
);
891 else if (psa
->fFeatures
& FADF_BSTR
)
893 BSTR lpBstr
= (BSTR
)pvData
;
894 BSTR
* lpDest
= lpvDest
;
896 SysFreeString(*lpDest
);
898 *lpDest
= SysAllocStringByteLen((char*)lpBstr
, SysStringByteLen(lpBstr
));
900 hRet
= E_OUTOFMEMORY
;
902 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
904 IUnknown
*lpUnknown
= pvData
;
905 IUnknown
**lpDest
= lpvDest
;
908 IUnknown_AddRef(lpUnknown
);
910 IUnknown_Release(*lpDest
);
913 else if (psa
->fFeatures
& FADF_RECORD
)
917 SafeArrayGetRecordInfo(psa
, &record
);
918 hRet
= IRecordInfo_RecordCopy(record
, pvData
, lpvDest
);
919 IRecordInfo_Release(record
);
921 /* Copy the data over */
922 memcpy(lpvDest
, pvData
, psa
->cbElements
);
924 SafeArrayUnlock(psa
);
930 /*************************************************************************
931 * SafeArrayGetElement (OLEAUT32.25)
933 * Get an item from a SafeArray.
936 * psa [I] SafeArray to get from
937 * rgIndices [I] Indices to get from
938 * pvData [O] Destination for data
941 * Success: S_OK. The item data is returned in pvData.
942 * Failure: An HRESULT error code indicating the error.
947 HRESULT WINAPI
SafeArrayGetElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
951 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
953 if (!psa
|| !rgIndices
|| !pvData
)
956 hRet
= SafeArrayLock(psa
);
962 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvSrc
);
966 if (psa
->fFeatures
& FADF_VARIANT
)
968 VARIANT
* lpVariant
= lpvSrc
;
969 VARIANT
* lpDest
= pvData
;
971 /* The original content of pvData is ignored. */
972 V_VT(lpDest
) = VT_EMPTY
;
973 hRet
= VariantCopy(lpDest
, lpVariant
);
974 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%x\n", hRet
);
976 else if (psa
->fFeatures
& FADF_BSTR
)
978 BSTR
* lpBstr
= lpvSrc
;
979 BSTR
* lpDest
= pvData
;
983 *lpDest
= SysAllocStringByteLen((char*)*lpBstr
, SysStringByteLen(*lpBstr
));
985 hRet
= E_OUTOFMEMORY
;
990 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
992 IUnknown
**src_unk
= lpvSrc
;
993 IUnknown
**dest_unk
= pvData
;
996 IUnknown_AddRef(*src_unk
);
997 *dest_unk
= *src_unk
;
999 else if (psa
->fFeatures
& FADF_RECORD
)
1001 IRecordInfo
*record
;
1003 SafeArrayGetRecordInfo(psa
, &record
);
1004 hRet
= IRecordInfo_RecordCopy(record
, lpvSrc
, pvData
);
1005 IRecordInfo_Release(record
);
1008 /* Copy the data over */
1009 memcpy(pvData
, lpvSrc
, psa
->cbElements
);
1011 SafeArrayUnlock(psa
);
1016 /*************************************************************************
1017 * SafeArrayGetUBound (OLEAUT32.19)
1019 * Get the upper bound for a given SafeArray dimension
1022 * psa [I] Array to get dimension upper bound from
1023 * nDim [I] The dimension number to get the upper bound of
1024 * plUbound [O] Destination for the upper bound
1027 * Success: S_OK. plUbound contains the dimensions upper bound.
1028 * Failure: An HRESULT error code indicating the error.
1033 HRESULT WINAPI
SafeArrayGetUBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plUbound
)
1035 TRACE("(%p,%d,%p)\n", psa
, nDim
, plUbound
);
1037 if (!psa
|| !plUbound
)
1038 return E_INVALIDARG
;
1040 if(!nDim
|| nDim
> psa
->cDims
)
1041 return DISP_E_BADINDEX
;
1043 *plUbound
= psa
->rgsabound
[psa
->cDims
- nDim
].lLbound
+
1044 psa
->rgsabound
[psa
->cDims
- nDim
].cElements
- 1;
1049 /*************************************************************************
1050 * SafeArrayGetLBound (OLEAUT32.20)
1052 * Get the lower bound for a given SafeArray dimension
1055 * psa [I] Array to get dimension lower bound from
1056 * nDim [I] The dimension number to get the lower bound of
1057 * plLbound [O] Destination for the lower bound
1060 * Success: S_OK. plUbound contains the dimensions lower bound.
1061 * Failure: An HRESULT error code indicating the error.
1066 HRESULT WINAPI
SafeArrayGetLBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plLbound
)
1068 TRACE("(%p,%d,%p)\n", psa
, nDim
, plLbound
);
1070 if (!psa
|| !plLbound
)
1071 return E_INVALIDARG
;
1073 if(!nDim
|| nDim
> psa
->cDims
)
1074 return DISP_E_BADINDEX
;
1076 *plLbound
= psa
->rgsabound
[psa
->cDims
- nDim
].lLbound
;
1080 /*************************************************************************
1081 * SafeArrayGetDim (OLEAUT32.17)
1083 * Get the number of dimensions in a SafeArray.
1086 * psa [I] Array to get the dimensions of
1089 * The number of array dimensions in psa, or 0 if psa is NULL.
1094 UINT WINAPI
SafeArrayGetDim(SAFEARRAY
*psa
)
1096 TRACE("(%p) returning %d\n", psa
, psa
? psa
->cDims
: 0u);
1097 return psa
? psa
->cDims
: 0;
1100 /*************************************************************************
1101 * SafeArrayGetElemsize (OLEAUT32.18)
1103 * Get the size of an element in a SafeArray.
1106 * psa [I] Array to get the element size from
1109 * The size of a single element in psa, or 0 if psa is NULL.
1114 UINT WINAPI
SafeArrayGetElemsize(SAFEARRAY
*psa
)
1116 TRACE("(%p) returning %d\n", psa
, psa
? psa
->cbElements
: 0u);
1117 return psa
? psa
->cbElements
: 0;
1120 /*************************************************************************
1121 * SafeArrayAccessData (OLEAUT32.23)
1123 * Lock a SafeArray and return a pointer to its data.
1126 * psa [I] Array to get the data pointer from
1127 * ppvData [O] Destination for the arrays data pointer
1130 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1132 * Failure: An HRESULT error code indicating the error.
1137 HRESULT WINAPI
SafeArrayAccessData(SAFEARRAY
*psa
, void **ppvData
)
1141 TRACE("(%p,%p)\n", psa
, ppvData
);
1143 if(!psa
|| !ppvData
)
1144 return E_INVALIDARG
;
1146 hr
= SafeArrayLock(psa
);
1147 *ppvData
= SUCCEEDED(hr
) ? psa
->pvData
: NULL
;
1153 /*************************************************************************
1154 * SafeArrayUnaccessData (OLEAUT32.24)
1156 * Unlock a SafeArray after accessing its data.
1159 * psa [I] Array to unlock
1162 * Success: S_OK. The array is unlocked.
1163 * Failure: An HRESULT error code indicating the error.
1168 HRESULT WINAPI
SafeArrayUnaccessData(SAFEARRAY
*psa
)
1170 TRACE("(%p)\n", psa
);
1171 return SafeArrayUnlock(psa
);
1174 /************************************************************************
1175 * SafeArrayPtrOfIndex (OLEAUT32.148)
1177 * Get the address of an item in a SafeArray.
1180 * psa [I] Array to get the items address from
1181 * rgIndices [I] Index of the item in the array
1182 * ppvData [O] Destination for item address
1185 * Success: S_OK. ppvData contains a pointer to the item.
1186 * Failure: An HRESULT error code indicating the error.
1189 * This function does not lock the array.
1194 HRESULT WINAPI
SafeArrayPtrOfIndex(SAFEARRAY
*psa
, LONG
*rgIndices
, void **ppvData
)
1197 ULONG cell
= 0, dimensionSize
= 1;
1198 SAFEARRAYBOUND
* psab
;
1201 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, ppvData
);
1203 /* The general formula for locating the cell number of an entry in an n
1204 * dimensional array (where cn = coordinate in dimension dn) is:
1206 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1208 * We calculate the size of the last dimension at each step through the
1209 * dimensions to avoid recursing to calculate the last dimensions size.
1211 if (!psa
|| !rgIndices
|| !ppvData
)
1212 return E_INVALIDARG
;
1214 psab
= psa
->rgsabound
+ psa
->cDims
- 1;
1217 if (c1
< psab
->lLbound
|| c1
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1218 return DISP_E_BADINDEX
; /* Initial index out of bounds */
1220 for (dim
= 1; dim
< psa
->cDims
; dim
++)
1222 dimensionSize
*= psab
->cElements
;
1226 if (!psab
->cElements
||
1227 *rgIndices
< psab
->lLbound
||
1228 *rgIndices
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1229 return DISP_E_BADINDEX
; /* Index out of bounds */
1231 cell
+= (*rgIndices
- psab
->lLbound
) * dimensionSize
;
1235 cell
+= (c1
- psa
->rgsabound
[psa
->cDims
- 1].lLbound
);
1237 *ppvData
= (char*)psa
->pvData
+ cell
* psa
->cbElements
;
1241 /************************************************************************
1242 * SafeArrayDestroyData (OLEAUT32.39)
1244 * Destroy the data associated with a SafeArray.
1247 * psa [I] Array to delete the data from
1250 * Success: S_OK. All items and the item data are freed.
1251 * Failure: An HRESULT error code indicating the error.
1256 HRESULT WINAPI
SafeArrayDestroyData(SAFEARRAY
*psa
)
1260 TRACE("(%p)\n", psa
);
1263 return E_INVALIDARG
;
1266 return DISP_E_ARRAYISLOCKED
; /* Can't delete a locked array */
1268 /* Delete the actual item data */
1269 hr
= SAFEARRAY_DestroyData(psa
, 0);
1275 if (psa
->fFeatures
& FADF_STATIC
)
1277 ZeroMemory(psa
->pvData
, SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1280 /* If this is not a vector, free the data memory block */
1281 if (!(psa
->fFeatures
& FADF_CREATEVECTOR
))
1283 SAFEARRAY_Free(psa
->pvData
);
1287 psa
->fFeatures
|= FADF_DATADELETED
; /* Mark the data deleted */
1293 /************************************************************************
1294 * SafeArrayCopyData (OLEAUT32.412)
1296 * Copy all data from one SafeArray to another.
1299 * psaSource [I] Source for copy
1300 * psaTarget [O] Destination for copy
1303 * Success: S_OK. psaTarget contains a copy of psaSource.
1304 * Failure: An HRESULT error code indicating the error.
1307 * The two arrays must have the same number of dimensions and elements.
1312 HRESULT WINAPI
SafeArrayCopyData(SAFEARRAY
*psaSource
, SAFEARRAY
*psaTarget
)
1316 TRACE("(%p,%p)\n", psaSource
, psaTarget
);
1318 if (!psaSource
|| !psaTarget
||
1319 psaSource
->cDims
!= psaTarget
->cDims
||
1320 psaSource
->cbElements
!= psaTarget
->cbElements
)
1321 return E_INVALIDARG
;
1323 /* Each dimension must be the same size */
1324 for (dim
= psaSource
->cDims
- 1; dim
>= 0 ; dim
--)
1325 if (psaSource
->rgsabound
[dim
].cElements
!=
1326 psaTarget
->rgsabound
[dim
].cElements
)
1327 return E_INVALIDARG
;
1329 return SAFEARRAY_CopyData(psaSource
, psaTarget
);
1332 /************************************************************************
1333 * SafeArrayDestroy (OLEAUT32.16)
1335 * Destroy a SafeArray.
1338 * psa [I] Array to destroy
1341 * Success: S_OK. All resources used by the array are freed.
1342 * Failure: An HRESULT error code indicating the error.
1347 HRESULT WINAPI
SafeArrayDestroy(SAFEARRAY
*psa
)
1349 TRACE("(%p)\n", psa
);
1355 return DISP_E_ARRAYISLOCKED
;
1357 /* Native doesn't check to see if the free succeeds */
1358 SafeArrayDestroyData(psa
);
1359 SafeArrayDestroyDescriptor(psa
);
1363 /************************************************************************
1364 * SafeArrayCopy (OLEAUT32.27)
1366 * Make a duplicate of a SafeArray.
1369 * psa [I] Source for copy
1370 * ppsaOut [O] Destination for new copy
1373 * Success: S_OK. ppsaOut contains a copy of the array.
1374 * Failure: An HRESULT error code indicating the error.
1379 HRESULT WINAPI
SafeArrayCopy(SAFEARRAY
*psa
, SAFEARRAY
**ppsaOut
)
1383 TRACE("(%p,%p)\n", psa
, ppsaOut
);
1386 return E_INVALIDARG
;
1391 return S_OK
; /* Handles copying of NULL arrays */
1393 if (!psa
->cbElements
)
1394 return E_INVALIDARG
;
1396 if (psa
->fFeatures
& (FADF_RECORD
|FADF_HAVEIID
|FADF_HAVEVARTYPE
))
1400 hRet
= SafeArrayGetVartype(psa
, &vt
);
1401 if (SUCCEEDED(hRet
))
1402 hRet
= SafeArrayAllocDescriptorEx(vt
, psa
->cDims
, ppsaOut
);
1406 hRet
= SafeArrayAllocDescriptor(psa
->cDims
, ppsaOut
);
1407 if (SUCCEEDED(hRet
))
1409 (*ppsaOut
)->fFeatures
= psa
->fFeatures
& ~ignored_copy_features
;
1410 (*ppsaOut
)->cbElements
= psa
->cbElements
;
1414 if (SUCCEEDED(hRet
))
1416 /* Copy dimension bounds */
1417 memcpy((*ppsaOut
)->rgsabound
, psa
->rgsabound
, psa
->cDims
* sizeof(SAFEARRAYBOUND
));
1419 (*ppsaOut
)->pvData
= SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1420 if (!(*ppsaOut
)->pvData
)
1422 SafeArrayDestroyDescriptor(*ppsaOut
);
1424 return E_OUTOFMEMORY
;
1427 hRet
= SAFEARRAY_CopyData(psa
, *ppsaOut
);
1430 SAFEARRAY_Free((*ppsaOut
)->pvData
);
1431 SafeArrayDestroyDescriptor(*ppsaOut
);
1440 /************************************************************************
1441 * SafeArrayRedim (OLEAUT32.40)
1443 * Changes the characteristics of the last dimension of a SafeArray
1446 * psa [I] Array to change
1447 * psabound [I] New bound details for the last dimension
1450 * Success: S_OK. psa is updated to reflect the new bounds.
1451 * Failure: An HRESULT error code indicating the error.
1456 HRESULT WINAPI
SafeArrayRedim(SAFEARRAY
*psa
, SAFEARRAYBOUND
*psabound
)
1458 SAFEARRAYBOUND
*oldBounds
;
1461 TRACE("(%p,%p)\n", psa
, psabound
);
1463 if (!psa
|| psa
->fFeatures
& FADF_FIXEDSIZE
|| !psabound
)
1464 return E_INVALIDARG
;
1466 if (psa
->cLocks
> 0)
1467 return DISP_E_ARRAYISLOCKED
;
1469 hr
= SafeArrayLock(psa
);
1473 oldBounds
= psa
->rgsabound
;
1474 oldBounds
->lLbound
= psabound
->lLbound
;
1476 if (psabound
->cElements
!= oldBounds
->cElements
)
1478 if (psabound
->cElements
< oldBounds
->cElements
)
1480 /* Shorten the final dimension. */
1481 ULONG ulStartCell
= psabound
->cElements
*
1482 (SAFEARRAY_GetCellCount(psa
) / oldBounds
->cElements
);
1483 SAFEARRAY_DestroyData(psa
, ulStartCell
);
1487 /* Lengthen the final dimension */
1488 ULONG ulOldSize
, ulNewSize
;
1491 ulOldSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1493 ulNewSize
= (ulOldSize
/ oldBounds
->cElements
) * psabound
->cElements
;
1495 int oldelems
= oldBounds
->cElements
;
1496 oldBounds
->cElements
= psabound
->cElements
;
1497 ulNewSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1498 oldBounds
->cElements
= oldelems
;
1501 if (!(pvNewData
= SAFEARRAY_Malloc(ulNewSize
)))
1503 SafeArrayUnlock(psa
);
1504 return E_OUTOFMEMORY
;
1507 memcpy(pvNewData
, psa
->pvData
, ulOldSize
);
1508 SAFEARRAY_Free(psa
->pvData
);
1509 psa
->pvData
= pvNewData
;
1511 oldBounds
->cElements
= psabound
->cElements
;
1514 SafeArrayUnlock(psa
);
1518 /************************************************************************
1519 * SafeArrayGetVartype (OLEAUT32.77)
1521 * Get the type of the items in a SafeArray.
1524 * psa [I] Array to get the type from
1525 * pvt [O] Destination for the type
1528 * Success: S_OK. pvt contains the type of the items.
1529 * Failure: An HRESULT error code indicating the error.
1534 HRESULT WINAPI
SafeArrayGetVartype(SAFEARRAY
* psa
, VARTYPE
* pvt
)
1536 TRACE("(%p,%p)\n", psa
, pvt
);
1539 return E_INVALIDARG
;
1541 if (psa
->fFeatures
& FADF_RECORD
)
1543 else if ((psa
->fFeatures
& (FADF_HAVEIID
|FADF_DISPATCH
)) == (FADF_HAVEIID
|FADF_DISPATCH
))
1545 else if (psa
->fFeatures
& FADF_HAVEIID
)
1547 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
1549 VARTYPE vt
= SAFEARRAY_GetHiddenDWORD(psa
);
1553 return E_INVALIDARG
;
1558 /************************************************************************
1559 * SafeArraySetRecordInfo (OLEAUT32.@)
1561 * Set the record info for a SafeArray.
1564 * psa [I] Array to set the record info for
1565 * pRinfo [I] Record info
1568 * Success: S_OK. The record info is stored with the array.
1569 * Failure: An HRESULT error code indicating the error.
1574 HRESULT WINAPI
SafeArraySetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
*pRinfo
)
1576 IRecordInfo
** dest
= (IRecordInfo
**)psa
;
1578 TRACE("(%p,%p)\n", psa
, pRinfo
);
1580 if (!psa
|| !(psa
->fFeatures
& FADF_RECORD
))
1581 return E_INVALIDARG
;
1584 IRecordInfo_AddRef(pRinfo
);
1587 IRecordInfo_Release(dest
[-1]);
1593 /************************************************************************
1594 * SafeArrayGetRecordInfo (OLEAUT32.@)
1596 * Get the record info from a SafeArray.
1599 * psa [I] Array to get the record info from
1600 * pRinfo [O] Destination for the record info
1603 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1604 * Failure: An HRESULT error code indicating the error.
1609 HRESULT WINAPI
SafeArrayGetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
**pRinfo
)
1611 IRecordInfo
** src
= (IRecordInfo
**)psa
;
1613 TRACE("(%p,%p)\n", psa
, pRinfo
);
1615 if (!psa
|| !pRinfo
|| !(psa
->fFeatures
& FADF_RECORD
))
1616 return E_INVALIDARG
;
1621 IRecordInfo_AddRef(*pRinfo
);
1625 /************************************************************************
1626 * SafeArraySetIID (OLEAUT32.@)
1628 * Set the IID for a SafeArray.
1631 * psa [I] Array to set the IID from
1635 * Success: S_OK. The IID is stored with the array
1636 * Failure: An HRESULT error code indicating the error.
1641 HRESULT WINAPI
SafeArraySetIID(SAFEARRAY
*psa
, REFGUID guid
)
1643 GUID
* dest
= (GUID
*)psa
;
1645 TRACE("(%p,%s)\n", psa
, debugstr_guid(guid
));
1647 if (!psa
|| !guid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1648 return E_INVALIDARG
;
1654 /************************************************************************
1655 * SafeArrayGetIID (OLEAUT32.@)
1657 * Get the IID from a SafeArray.
1660 * psa [I] Array to get the ID from
1661 * pGuid [O] Destination for the IID
1664 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1665 * Failure: An HRESULT error code indicating the error.
1670 HRESULT WINAPI
SafeArrayGetIID(SAFEARRAY
*psa
, GUID
*pGuid
)
1672 GUID
* src
= (GUID
*)psa
;
1674 TRACE("(%p,%p)\n", psa
, pGuid
);
1676 if (!psa
|| !pGuid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1677 return E_INVALIDARG
;
1683 /************************************************************************
1684 * VectorFromBstr (OLEAUT32.@)
1686 * Create a SafeArray Vector from the bytes of a BSTR.
1689 * bstr [I] String to get bytes from
1690 * ppsa [O] Destination for the array
1693 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1694 * Failure: An HRESULT error code indicating the error.
1699 HRESULT WINAPI
VectorFromBstr(BSTR bstr
, SAFEARRAY
**ppsa
)
1703 TRACE("(%p,%p)\n", bstr
, ppsa
);
1706 return E_INVALIDARG
;
1709 sab
.cElements
= SysStringByteLen(bstr
);
1711 *ppsa
= SAFEARRAY_Create(VT_UI1
, 1, &sab
, 0);
1715 memcpy((*ppsa
)->pvData
, bstr
, sab
.cElements
);
1718 return E_OUTOFMEMORY
;
1721 /************************************************************************
1722 * BstrFromVector (OLEAUT32.@)
1724 * Create a BSTR from a SafeArray.
1727 * psa [I] Source array
1728 * pbstr [O] Destination for output BSTR
1731 * Success: S_OK. pbstr contains the arrays data.
1732 * Failure: An HRESULT error code indicating the error.
1735 * psa must be a 1 dimensional array of a 1 byte type.
1740 HRESULT WINAPI
BstrFromVector(SAFEARRAY
*psa
, BSTR
*pbstr
)
1742 TRACE("(%p,%p)\n", psa
, pbstr
);
1745 return E_INVALIDARG
;
1749 if (!psa
|| psa
->cbElements
!= 1 || psa
->cDims
!= 1)
1750 return E_INVALIDARG
;
1752 *pbstr
= SysAllocStringByteLen(psa
->pvData
, psa
->rgsabound
[0].cElements
);
1754 return E_OUTOFMEMORY
;