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 void* SAFEARRAY_Malloc(ULONG size
)
100 void *ret
= CoTaskMemAlloc(size
);
102 memset(ret
, 0, size
);
107 static inline void SAFEARRAY_Free(void *ptr
)
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 */
780 /*************************************************************************
781 * SafeArrayLock (OLEAUT32.21)
783 * Increment the lock counter of a SafeArray.
786 * psa [O] SafeArray to lock
789 * Success: S_OK. The array lock is incremented.
790 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
791 * are held on the array at once.
794 * In Win32 these locks are not thread safe.
797 HRESULT WINAPI
SafeArrayLock(SAFEARRAY
*psa
)
801 TRACE("(%p)\n", psa
);
806 ulLocks
= InterlockedIncrement( (LONG
*) &psa
->cLocks
);
808 if (ulLocks
> 0xffff) /* Maximum of 16384 locks at a time */
810 WARN("Out of locks!\n");
811 InterlockedDecrement( (LONG
*) &psa
->cLocks
);
817 /*************************************************************************
818 * SafeArrayUnlock (OLEAUT32.22)
820 * Decrement the lock counter of a SafeArray.
823 * psa [O] SafeArray to unlock
826 * Success: S_OK. The array lock is decremented.
827 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
833 HRESULT WINAPI
SafeArrayUnlock(SAFEARRAY
*psa
)
835 TRACE("(%p)\n", psa
);
840 if (InterlockedDecrement( (LONG
*) &psa
->cLocks
) < 0)
842 WARN("Unlocked but no lock held!\n");
843 InterlockedIncrement( (LONG
*) &psa
->cLocks
);
849 /*************************************************************************
850 * SafeArrayPutElement (OLEAUT32.26)
852 * Put an item into a SafeArray.
855 * psa [I] SafeArray to insert into
856 * rgIndices [I] Indices to insert at
857 * pvData [I] Data to insert
860 * Success: S_OK. The item is inserted
861 * Failure: An HRESULT error code indicating the error.
866 HRESULT WINAPI
SafeArrayPutElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
870 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
872 if (!psa
|| !rgIndices
)
875 hRet
= SafeArrayLock(psa
);
881 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvDest
);
885 if (psa
->fFeatures
& FADF_VARIANT
)
887 VARIANT
* lpVariant
= pvData
;
888 VARIANT
* lpDest
= lpvDest
;
890 hRet
= VariantCopy(lpDest
, lpVariant
);
891 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%x\n", hRet
);
893 else if (psa
->fFeatures
& FADF_BSTR
)
895 BSTR lpBstr
= (BSTR
)pvData
;
896 BSTR
* lpDest
= lpvDest
;
898 SysFreeString(*lpDest
);
900 *lpDest
= SysAllocStringByteLen((char*)lpBstr
, SysStringByteLen(lpBstr
));
902 hRet
= E_OUTOFMEMORY
;
904 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
906 IUnknown
*lpUnknown
= pvData
;
907 IUnknown
**lpDest
= lpvDest
;
910 IUnknown_AddRef(lpUnknown
);
912 IUnknown_Release(*lpDest
);
915 else if (psa
->fFeatures
& FADF_RECORD
)
919 SafeArrayGetRecordInfo(psa
, &record
);
920 hRet
= IRecordInfo_RecordCopy(record
, pvData
, lpvDest
);
921 IRecordInfo_Release(record
);
923 /* Copy the data over */
924 memcpy(lpvDest
, pvData
, psa
->cbElements
);
926 SafeArrayUnlock(psa
);
932 /*************************************************************************
933 * SafeArrayGetElement (OLEAUT32.25)
935 * Get an item from a SafeArray.
938 * psa [I] SafeArray to get from
939 * rgIndices [I] Indices to get from
940 * pvData [O] Destination for data
943 * Success: S_OK. The item data is returned in pvData.
944 * Failure: An HRESULT error code indicating the error.
949 HRESULT WINAPI
SafeArrayGetElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
953 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
955 if (!psa
|| !rgIndices
|| !pvData
)
958 hRet
= SafeArrayLock(psa
);
964 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvSrc
);
968 if (psa
->fFeatures
& FADF_VARIANT
)
970 VARIANT
* lpVariant
= lpvSrc
;
971 VARIANT
* lpDest
= pvData
;
973 /* The original content of pvData is ignored. */
974 V_VT(lpDest
) = VT_EMPTY
;
975 hRet
= VariantCopy(lpDest
, lpVariant
);
976 if (FAILED(hRet
)) FIXME("VariantCopy failed with 0x%x\n", hRet
);
978 else if (psa
->fFeatures
& FADF_BSTR
)
980 BSTR
* lpBstr
= lpvSrc
;
981 BSTR
* lpDest
= pvData
;
985 *lpDest
= SysAllocStringByteLen((char*)*lpBstr
, SysStringByteLen(*lpBstr
));
987 hRet
= E_OUTOFMEMORY
;
992 else if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
994 IUnknown
**src_unk
= lpvSrc
;
995 IUnknown
**dest_unk
= pvData
;
998 IUnknown_AddRef(*src_unk
);
999 *dest_unk
= *src_unk
;
1001 else if (psa
->fFeatures
& FADF_RECORD
)
1003 IRecordInfo
*record
;
1005 SafeArrayGetRecordInfo(psa
, &record
);
1006 hRet
= IRecordInfo_RecordCopy(record
, lpvSrc
, pvData
);
1007 IRecordInfo_Release(record
);
1010 /* Copy the data over */
1011 memcpy(pvData
, lpvSrc
, psa
->cbElements
);
1013 SafeArrayUnlock(psa
);
1018 /*************************************************************************
1019 * SafeArrayGetUBound (OLEAUT32.19)
1021 * Get the upper bound for a given SafeArray dimension
1024 * psa [I] Array to get dimension upper bound from
1025 * nDim [I] The dimension number to get the upper bound of
1026 * plUbound [O] Destination for the upper bound
1029 * Success: S_OK. plUbound contains the dimensions upper bound.
1030 * Failure: An HRESULT error code indicating the error.
1035 HRESULT WINAPI
SafeArrayGetUBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plUbound
)
1037 TRACE("(%p,%d,%p)\n", psa
, nDim
, plUbound
);
1039 if (!psa
|| !plUbound
)
1040 return E_INVALIDARG
;
1042 if(!nDim
|| nDim
> psa
->cDims
)
1043 return DISP_E_BADINDEX
;
1045 *plUbound
= psa
->rgsabound
[psa
->cDims
- nDim
].lLbound
+
1046 psa
->rgsabound
[psa
->cDims
- nDim
].cElements
- 1;
1051 /*************************************************************************
1052 * SafeArrayGetLBound (OLEAUT32.20)
1054 * Get the lower bound for a given SafeArray dimension
1057 * psa [I] Array to get dimension lower bound from
1058 * nDim [I] The dimension number to get the lower bound of
1059 * plLbound [O] Destination for the lower bound
1062 * Success: S_OK. plUbound contains the dimensions lower bound.
1063 * Failure: An HRESULT error code indicating the error.
1068 HRESULT WINAPI
SafeArrayGetLBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plLbound
)
1070 TRACE("(%p,%d,%p)\n", psa
, nDim
, plLbound
);
1072 if (!psa
|| !plLbound
)
1073 return E_INVALIDARG
;
1075 if(!nDim
|| nDim
> psa
->cDims
)
1076 return DISP_E_BADINDEX
;
1078 *plLbound
= psa
->rgsabound
[psa
->cDims
- nDim
].lLbound
;
1082 /*************************************************************************
1083 * SafeArrayGetDim (OLEAUT32.17)
1085 * Get the number of dimensions in a SafeArray.
1088 * psa [I] Array to get the dimensions of
1091 * The number of array dimensions in psa, or 0 if psa is NULL.
1096 UINT WINAPI
SafeArrayGetDim(SAFEARRAY
*psa
)
1098 TRACE("(%p) returning %d\n", psa
, psa
? psa
->cDims
: 0u);
1099 return psa
? psa
->cDims
: 0;
1102 /*************************************************************************
1103 * SafeArrayGetElemsize (OLEAUT32.18)
1105 * Get the size of an element in a SafeArray.
1108 * psa [I] Array to get the element size from
1111 * The size of a single element in psa, or 0 if psa is NULL.
1116 UINT WINAPI
SafeArrayGetElemsize(SAFEARRAY
*psa
)
1118 TRACE("(%p) returning %d\n", psa
, psa
? psa
->cbElements
: 0u);
1119 return psa
? psa
->cbElements
: 0;
1122 /*************************************************************************
1123 * SafeArrayAccessData (OLEAUT32.23)
1125 * Lock a SafeArray and return a pointer to its data.
1128 * psa [I] Array to get the data pointer from
1129 * ppvData [O] Destination for the arrays data pointer
1132 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1134 * Failure: An HRESULT error code indicating the error.
1139 HRESULT WINAPI
SafeArrayAccessData(SAFEARRAY
*psa
, void **ppvData
)
1143 TRACE("(%p,%p)\n", psa
, ppvData
);
1145 if(!psa
|| !ppvData
)
1146 return E_INVALIDARG
;
1148 hr
= SafeArrayLock(psa
);
1149 *ppvData
= SUCCEEDED(hr
) ? psa
->pvData
: NULL
;
1155 /*************************************************************************
1156 * SafeArrayUnaccessData (OLEAUT32.24)
1158 * Unlock a SafeArray after accessing its data.
1161 * psa [I] Array to unlock
1164 * Success: S_OK. The array is unlocked.
1165 * Failure: An HRESULT error code indicating the error.
1170 HRESULT WINAPI
SafeArrayUnaccessData(SAFEARRAY
*psa
)
1172 TRACE("(%p)\n", psa
);
1173 return SafeArrayUnlock(psa
);
1176 /************************************************************************
1177 * SafeArrayPtrOfIndex (OLEAUT32.148)
1179 * Get the address of an item in a SafeArray.
1182 * psa [I] Array to get the items address from
1183 * rgIndices [I] Index of the item in the array
1184 * ppvData [O] Destination for item address
1187 * Success: S_OK. ppvData contains a pointer to the item.
1188 * Failure: An HRESULT error code indicating the error.
1191 * This function does not lock the array.
1196 HRESULT WINAPI
SafeArrayPtrOfIndex(SAFEARRAY
*psa
, LONG
*rgIndices
, void **ppvData
)
1199 ULONG cell
= 0, dimensionSize
= 1;
1200 SAFEARRAYBOUND
* psab
;
1203 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, ppvData
);
1205 /* The general formula for locating the cell number of an entry in an n
1206 * dimensional array (where cn = coordinate in dimension dn) is:
1208 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1210 * We calculate the size of the last dimension at each step through the
1211 * dimensions to avoid recursing to calculate the last dimensions size.
1213 if (!psa
|| !rgIndices
|| !ppvData
)
1214 return E_INVALIDARG
;
1216 psab
= psa
->rgsabound
+ psa
->cDims
- 1;
1219 if (c1
< psab
->lLbound
|| c1
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1220 return DISP_E_BADINDEX
; /* Initial index out of bounds */
1222 for (dim
= 1; dim
< psa
->cDims
; dim
++)
1224 dimensionSize
*= psab
->cElements
;
1228 if (!psab
->cElements
||
1229 *rgIndices
< psab
->lLbound
||
1230 *rgIndices
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1231 return DISP_E_BADINDEX
; /* Index out of bounds */
1233 cell
+= (*rgIndices
- psab
->lLbound
) * dimensionSize
;
1237 cell
+= (c1
- psa
->rgsabound
[psa
->cDims
- 1].lLbound
);
1239 *ppvData
= (char*)psa
->pvData
+ cell
* psa
->cbElements
;
1243 /************************************************************************
1244 * SafeArrayDestroyData (OLEAUT32.39)
1246 * Destroy the data associated with a SafeArray.
1249 * psa [I] Array to delete the data from
1252 * Success: S_OK. All items and the item data are freed.
1253 * Failure: An HRESULT error code indicating the error.
1258 HRESULT WINAPI
SafeArrayDestroyData(SAFEARRAY
*psa
)
1262 TRACE("(%p)\n", psa
);
1265 return E_INVALIDARG
;
1268 return DISP_E_ARRAYISLOCKED
; /* Can't delete a locked array */
1270 /* Delete the actual item data */
1271 hr
= SAFEARRAY_DestroyData(psa
, 0);
1277 if (psa
->fFeatures
& FADF_STATIC
)
1279 ZeroMemory(psa
->pvData
, SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1282 /* If this is not a vector, free the data memory block */
1283 if (!(psa
->fFeatures
& FADF_CREATEVECTOR
))
1285 SAFEARRAY_Free(psa
->pvData
);
1289 psa
->fFeatures
|= FADF_DATADELETED
; /* Mark the data deleted */
1295 /************************************************************************
1296 * SafeArrayCopyData (OLEAUT32.412)
1298 * Copy all data from one SafeArray to another.
1301 * psaSource [I] Source for copy
1302 * psaTarget [O] Destination for copy
1305 * Success: S_OK. psaTarget contains a copy of psaSource.
1306 * Failure: An HRESULT error code indicating the error.
1309 * The two arrays must have the same number of dimensions and elements.
1314 HRESULT WINAPI
SafeArrayCopyData(SAFEARRAY
*psaSource
, SAFEARRAY
*psaTarget
)
1318 TRACE("(%p,%p)\n", psaSource
, psaTarget
);
1320 if (!psaSource
|| !psaTarget
||
1321 psaSource
->cDims
!= psaTarget
->cDims
||
1322 psaSource
->cbElements
!= psaTarget
->cbElements
)
1323 return E_INVALIDARG
;
1325 /* Each dimension must be the same size */
1326 for (dim
= psaSource
->cDims
- 1; dim
>= 0 ; dim
--)
1327 if (psaSource
->rgsabound
[dim
].cElements
!=
1328 psaTarget
->rgsabound
[dim
].cElements
)
1329 return E_INVALIDARG
;
1331 return SAFEARRAY_CopyData(psaSource
, psaTarget
);
1334 /************************************************************************
1335 * SafeArrayDestroy (OLEAUT32.16)
1337 * Destroy a SafeArray.
1340 * psa [I] Array to destroy
1343 * Success: S_OK. All resources used by the array are freed.
1344 * Failure: An HRESULT error code indicating the error.
1349 HRESULT WINAPI
SafeArrayDestroy(SAFEARRAY
*psa
)
1351 TRACE("(%p)\n", psa
);
1357 return DISP_E_ARRAYISLOCKED
;
1359 /* Native doesn't check to see if the free succeeds */
1360 SafeArrayDestroyData(psa
);
1361 SafeArrayDestroyDescriptor(psa
);
1365 /************************************************************************
1366 * SafeArrayCopy (OLEAUT32.27)
1368 * Make a duplicate of a SafeArray.
1371 * psa [I] Source for copy
1372 * ppsaOut [O] Destination for new copy
1375 * Success: S_OK. ppsaOut contains a copy of the array.
1376 * Failure: An HRESULT error code indicating the error.
1381 HRESULT WINAPI
SafeArrayCopy(SAFEARRAY
*psa
, SAFEARRAY
**ppsaOut
)
1385 TRACE("(%p,%p)\n", psa
, ppsaOut
);
1388 return E_INVALIDARG
;
1393 return S_OK
; /* Handles copying of NULL arrays */
1395 if (!psa
->cbElements
)
1396 return E_INVALIDARG
;
1398 if (psa
->fFeatures
& (FADF_RECORD
|FADF_HAVEIID
|FADF_HAVEVARTYPE
))
1402 hRet
= SafeArrayGetVartype(psa
, &vt
);
1403 if (SUCCEEDED(hRet
))
1404 hRet
= SafeArrayAllocDescriptorEx(vt
, psa
->cDims
, ppsaOut
);
1408 hRet
= SafeArrayAllocDescriptor(psa
->cDims
, ppsaOut
);
1409 if (SUCCEEDED(hRet
))
1411 (*ppsaOut
)->fFeatures
= psa
->fFeatures
& ~ignored_copy_features
;
1412 (*ppsaOut
)->cbElements
= psa
->cbElements
;
1416 if (SUCCEEDED(hRet
))
1418 /* Copy dimension bounds */
1419 memcpy((*ppsaOut
)->rgsabound
, psa
->rgsabound
, psa
->cDims
* sizeof(SAFEARRAYBOUND
));
1421 (*ppsaOut
)->pvData
= SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1422 if (!(*ppsaOut
)->pvData
)
1424 SafeArrayDestroyDescriptor(*ppsaOut
);
1426 return E_OUTOFMEMORY
;
1429 hRet
= SAFEARRAY_CopyData(psa
, *ppsaOut
);
1432 SAFEARRAY_Free((*ppsaOut
)->pvData
);
1433 SafeArrayDestroyDescriptor(*ppsaOut
);
1442 /************************************************************************
1443 * SafeArrayRedim (OLEAUT32.40)
1445 * Changes the characteristics of the last dimension of a SafeArray
1448 * psa [I] Array to change
1449 * psabound [I] New bound details for the last dimension
1452 * Success: S_OK. psa is updated to reflect the new bounds.
1453 * Failure: An HRESULT error code indicating the error.
1458 HRESULT WINAPI
SafeArrayRedim(SAFEARRAY
*psa
, SAFEARRAYBOUND
*psabound
)
1460 SAFEARRAYBOUND
*oldBounds
;
1463 TRACE("(%p,%p)\n", psa
, psabound
);
1465 if (!psa
|| psa
->fFeatures
& FADF_FIXEDSIZE
|| !psabound
)
1466 return E_INVALIDARG
;
1468 if (psa
->cLocks
> 0)
1469 return DISP_E_ARRAYISLOCKED
;
1471 hr
= SafeArrayLock(psa
);
1475 oldBounds
= psa
->rgsabound
;
1476 oldBounds
->lLbound
= psabound
->lLbound
;
1478 if (psabound
->cElements
!= oldBounds
->cElements
)
1480 if (psabound
->cElements
< oldBounds
->cElements
)
1482 /* Shorten the final dimension. */
1483 ULONG ulStartCell
= psabound
->cElements
*
1484 (SAFEARRAY_GetCellCount(psa
) / oldBounds
->cElements
);
1485 SAFEARRAY_DestroyData(psa
, ulStartCell
);
1489 /* Lengthen the final dimension */
1490 ULONG ulOldSize
, ulNewSize
;
1493 ulOldSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1495 ulNewSize
= (ulOldSize
/ oldBounds
->cElements
) * psabound
->cElements
;
1497 int oldelems
= oldBounds
->cElements
;
1498 oldBounds
->cElements
= psabound
->cElements
;
1499 ulNewSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1500 oldBounds
->cElements
= oldelems
;
1503 if (!(pvNewData
= SAFEARRAY_Malloc(ulNewSize
)))
1505 SafeArrayUnlock(psa
);
1506 return E_OUTOFMEMORY
;
1509 memcpy(pvNewData
, psa
->pvData
, ulOldSize
);
1510 SAFEARRAY_Free(psa
->pvData
);
1511 psa
->pvData
= pvNewData
;
1513 oldBounds
->cElements
= psabound
->cElements
;
1516 SafeArrayUnlock(psa
);
1520 /************************************************************************
1521 * SafeArrayGetVartype (OLEAUT32.77)
1523 * Get the type of the items in a SafeArray.
1526 * psa [I] Array to get the type from
1527 * pvt [O] Destination for the type
1530 * Success: S_OK. pvt contains the type of the items.
1531 * Failure: An HRESULT error code indicating the error.
1536 HRESULT WINAPI
SafeArrayGetVartype(SAFEARRAY
* psa
, VARTYPE
* pvt
)
1538 TRACE("(%p,%p)\n", psa
, pvt
);
1541 return E_INVALIDARG
;
1543 if (psa
->fFeatures
& FADF_RECORD
)
1545 else if ((psa
->fFeatures
& (FADF_HAVEIID
|FADF_DISPATCH
)) == (FADF_HAVEIID
|FADF_DISPATCH
))
1547 else if (psa
->fFeatures
& FADF_HAVEIID
)
1549 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
1551 VARTYPE vt
= SAFEARRAY_GetHiddenDWORD(psa
);
1555 return E_INVALIDARG
;
1560 /************************************************************************
1561 * SafeArraySetRecordInfo (OLEAUT32.@)
1563 * Set the record info for a SafeArray.
1566 * psa [I] Array to set the record info for
1567 * pRinfo [I] Record info
1570 * Success: S_OK. The record info is stored with the array.
1571 * Failure: An HRESULT error code indicating the error.
1576 HRESULT WINAPI
SafeArraySetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
*pRinfo
)
1578 IRecordInfo
** dest
= (IRecordInfo
**)psa
;
1580 TRACE("(%p,%p)\n", psa
, pRinfo
);
1582 if (!psa
|| !(psa
->fFeatures
& FADF_RECORD
))
1583 return E_INVALIDARG
;
1586 IRecordInfo_AddRef(pRinfo
);
1589 IRecordInfo_Release(dest
[-1]);
1595 /************************************************************************
1596 * SafeArrayGetRecordInfo (OLEAUT32.@)
1598 * Get the record info from a SafeArray.
1601 * psa [I] Array to get the record info from
1602 * pRinfo [O] Destination for the record info
1605 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1606 * Failure: An HRESULT error code indicating the error.
1611 HRESULT WINAPI
SafeArrayGetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
**pRinfo
)
1613 IRecordInfo
** src
= (IRecordInfo
**)psa
;
1615 TRACE("(%p,%p)\n", psa
, pRinfo
);
1617 if (!psa
|| !pRinfo
|| !(psa
->fFeatures
& FADF_RECORD
))
1618 return E_INVALIDARG
;
1623 IRecordInfo_AddRef(*pRinfo
);
1627 /************************************************************************
1628 * SafeArraySetIID (OLEAUT32.@)
1630 * Set the IID for a SafeArray.
1633 * psa [I] Array to set the IID from
1637 * Success: S_OK. The IID is stored with the array
1638 * Failure: An HRESULT error code indicating the error.
1643 HRESULT WINAPI
SafeArraySetIID(SAFEARRAY
*psa
, REFGUID guid
)
1645 GUID
* dest
= (GUID
*)psa
;
1647 TRACE("(%p,%s)\n", psa
, debugstr_guid(guid
));
1649 if (!psa
|| !guid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1650 return E_INVALIDARG
;
1656 /************************************************************************
1657 * SafeArrayGetIID (OLEAUT32.@)
1659 * Get the IID from a SafeArray.
1662 * psa [I] Array to get the ID from
1663 * pGuid [O] Destination for the IID
1666 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1667 * Failure: An HRESULT error code indicating the error.
1672 HRESULT WINAPI
SafeArrayGetIID(SAFEARRAY
*psa
, GUID
*pGuid
)
1674 GUID
* src
= (GUID
*)psa
;
1676 TRACE("(%p,%p)\n", psa
, pGuid
);
1678 if (!psa
|| !pGuid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1679 return E_INVALIDARG
;
1685 /************************************************************************
1686 * VectorFromBstr (OLEAUT32.@)
1688 * Create a SafeArray Vector from the bytes of a BSTR.
1691 * bstr [I] String to get bytes from
1692 * ppsa [O] Destination for the array
1695 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1696 * Failure: An HRESULT error code indicating the error.
1701 HRESULT WINAPI
VectorFromBstr(BSTR bstr
, SAFEARRAY
**ppsa
)
1705 TRACE("(%p,%p)\n", bstr
, ppsa
);
1708 return E_INVALIDARG
;
1711 sab
.cElements
= SysStringByteLen(bstr
);
1713 *ppsa
= SAFEARRAY_Create(VT_UI1
, 1, &sab
, 0);
1717 memcpy((*ppsa
)->pvData
, bstr
, sab
.cElements
);
1720 return E_OUTOFMEMORY
;
1723 /************************************************************************
1724 * BstrFromVector (OLEAUT32.@)
1726 * Create a BSTR from a SafeArray.
1729 * psa [I] Source array
1730 * pbstr [O] Destination for output BSTR
1733 * Success: S_OK. pbstr contains the arrays data.
1734 * Failure: An HRESULT error code indicating the error.
1737 * psa must be a 1 dimensional array of a 1 byte type.
1742 HRESULT WINAPI
BstrFromVector(SAFEARRAY
*psa
, BSTR
*pbstr
)
1744 TRACE("(%p,%p)\n", psa
, pbstr
);
1747 return E_INVALIDARG
;
1751 if (!psa
|| psa
->cbElements
!= 1 || psa
->cDims
!= 1)
1752 return E_INVALIDARG
;
1754 *pbstr
= SysAllocStringByteLen(psa
->pvData
, psa
->rgsabound
[0].cElements
);
1756 return E_OUTOFMEMORY
;