1 /*************************************************************************
2 * OLE Automation - SafeArray
4 * This file contains the implementation of the SafeArray functions.
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 2003 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* Memory Layout of a SafeArray:
25 * -0x10: start of memory.
26 * -0x10: GUID for VT_DISPATCH and VT_UNKNOWN safearrays (if FADF_HAVEIID)
27 * -0x04: DWORD varianttype; (for all others, except VT_RECORD) (if FADF_HAVEVARTYPE)
28 * -0x4: IRecordInfo* iface; (if FADF_RECORD, for VT_RECORD (can be NULL))
30 * 0x10: SAFEARRAYBOUNDS[0...]
44 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
49 /************************************************************************
50 * SafeArray {OLEAUT32}
53 * The SafeArray data type provides the underlying interface for Ole
54 * Automations arrays, used for example to represent array types in
55 * Visual Basic(tm) and to gather user defined parameters for invocation through
56 * an IDispatch interface.
58 * Safe arrays provide bounds checking and automatically manage the data
59 * types they contain, for example handing reference counting and copying
60 * of interface pointers. User defined types can be stored in arrays
61 * using the IRecordInfo interface.
63 * There are two types of SafeArray, normal and vectors. Normal arrays can have
64 * multiple dimensions and the data for the array is allocated seperately from
65 * the array header. This is the most flexable type of array. Vectors, on the
66 * other hand, are fixed in size and consist of a single allocated block, and a
70 * The following types of data can be stored within a SafeArray.
72 *| VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8, VT_UI8, VT_INT, VT_UINT,
73 *| VT_R4, VT_R8, VT_CY, VT_DECIMAL
75 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
77 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
84 /* Undocumented hidden space before the start of a SafeArray descriptor */
85 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
87 /* Value returned by SAFEARRAY_GetCellCount if a dimension is invalid */
88 #define SAFEARRAY_INVALID_CELLS ~0UL
91 static inline LPVOID
SAFEARRAY_Malloc(ULONG ulSize
)
93 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
94 * instance returned from CoGetMalloc().
96 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulSize
);
100 static inline BOOL
SAFEARRAY_Free(LPVOID lpData
)
102 return HeapFree(GetProcessHeap(), 0, lpData
);
105 /* Get the size of a supported VT type (0 means unsupported) */
106 static DWORD
SAFEARRAY_GetVTSize(VARTYPE vt
)
111 case VT_UI1
: return sizeof(BYTE
);
114 case VT_UI2
: return sizeof(SHORT
);
118 case VT_ERROR
: return sizeof(LONG
);
121 case VT_UI8
: return sizeof(LONG64
);
123 case VT_UINT
: return sizeof(INT
);
125 case VT_UINT_PTR
: return sizeof(UINT_PTR
);
126 case VT_CY
: return sizeof(CY
);
127 case VT_DATE
: return sizeof(DATE
);
128 case VT_BSTR
: return sizeof(BSTR
);
129 case VT_DISPATCH
: return sizeof(LPDISPATCH
);
130 case VT_VARIANT
: return sizeof(VARIANT
);
131 case VT_UNKNOWN
: return sizeof(LPUNKNOWN
);
132 case VT_DECIMAL
: return sizeof(DECIMAL
);
133 /* Note: Return a non-zero size to indicate vt is valid. The actual size
134 * of a UDT is taken from the result of IRecordInfo_GetSize().
136 case VT_RECORD
: return 32;
141 /* Set the hidden data for an array */
142 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY
* psa
, DWORD dw
)
144 /* Implementation data is stored in the 4 bytes before the header */
145 LPDWORD lpDw
= (LPDWORD
)psa
;
149 /* Get the hidden data from an array */
150 static inline DWORD
SAFEARRAY_GetHiddenDWORD(SAFEARRAY
* psa
)
152 LPDWORD lpDw
= (LPDWORD
)psa
;
156 /* Get the number of cells in a SafeArray */
157 static ULONG
SAFEARRAY_GetCellCount(const SAFEARRAY
*psa
)
159 SAFEARRAYBOUND
* psab
= psa
->rgsabound
;
160 USHORT cCount
= psa
->cDims
;
161 ULONG ulNumCells
= 1;
165 if (!psab
->cElements
)
167 ERR("Dimension has size=0! Please report.\n");
168 return SAFEARRAY_INVALID_CELLS
;
170 ulNumCells
*= psab
->cElements
;
176 /* Get the 0 based index of an index into a dimension */
177 static inline ULONG
SAFEARRAY_GetDimensionIndex(SAFEARRAYBOUND
*psab
, ULONG ulIndex
)
179 return ulIndex
- psab
->lLbound
;
182 /* Get the size of a dimension in cells */
183 static inline ULONG
SAFEARRAY_GetDimensionCells(SAFEARRAY
*psa
, ULONG ulDim
)
185 ULONG size
= psa
->rgsabound
[0].cElements
;
189 size
*= psa
->rgsabound
[ulDim
].cElements
;
195 /* Allocate a descriptor for an array */
196 static HRESULT
SAFEARRAY_AllocDescriptor(ULONG ulSize
, SAFEARRAY
**ppsaOut
)
198 *ppsaOut
= (SAFEARRAY
*)((char*)SAFEARRAY_Malloc(ulSize
+ SAFEARRAY_HIDDEN_SIZE
) + SAFEARRAY_HIDDEN_SIZE
);
206 /* Set the features of an array */
207 static void SAFEARRAY_SetFeatures(VARTYPE vt
, SAFEARRAY
*psa
)
209 /* Set the IID if we have one, otherwise set the type */
210 if (vt
== VT_DISPATCH
)
212 psa
->fFeatures
= FADF_HAVEIID
;
213 SafeArraySetIID(psa
, &IID_IDispatch
);
215 else if (vt
== VT_UNKNOWN
)
217 psa
->fFeatures
= FADF_HAVEIID
;
218 SafeArraySetIID(psa
, &IID_IUnknown
);
220 else if (vt
== VT_RECORD
)
221 psa
->fFeatures
= FADF_RECORD
;
224 psa
->fFeatures
= FADF_HAVEVARTYPE
;
225 SAFEARRAY_SetHiddenDWORD(psa
, vt
);
229 /* Create an array */
230 static SAFEARRAY
* SAFEARRAY_Create(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
, ULONG ulSize
)
232 SAFEARRAY
*psa
= NULL
;
237 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt
, cDims
, &psa
)))
241 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
242 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
243 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
244 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
247 memcpy(psa
->rgsabound
, rgsabound
, cDims
* sizeof(SAFEARRAYBOUND
));
250 psa
->cbElements
= ulSize
;
252 if (FAILED(SafeArrayAllocData(psa
)))
254 SafeArrayDestroyDescriptor(psa
);
261 /* Create an array as a vector */
262 static SAFEARRAY
* SAFEARRAY_CreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
, ULONG ulSize
)
264 SAFEARRAY
*psa
= NULL
;
266 if (cElements
&& (vt
== VT_RECORD
|| ulSize
))
268 /* Allocate the header and data together */
269 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY
) + ulSize
* cElements
, &psa
)))
271 SAFEARRAY_SetFeatures(vt
, psa
);
274 psa
->fFeatures
|= FADF_CREATEVECTOR
;
275 psa
->pvData
= &psa
[1]; /* Data follows the header */
276 psa
->cbElements
= ulSize
;
277 psa
->rgsabound
[0].cElements
= cElements
;
278 psa
->rgsabound
[0].lLbound
= lLbound
;
282 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
283 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
284 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
285 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
291 ERR("Creating vector of size 0! Please report.\n");
296 /* Free data items in an array */
297 static HRESULT
SAFEARRAY_DestroyData(SAFEARRAY
*psa
, ULONG ulStartCell
)
299 if (psa
->pvData
&& !(psa
->fFeatures
& FADF_DATADELETED
))
301 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
303 if (ulCellCount
== SAFEARRAY_INVALID_CELLS
|| ulStartCell
> ulCellCount
)
306 ulCellCount
-= ulStartCell
;
308 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
310 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
315 IUnknown_Release(*lpUnknown
);
319 else if (psa
->fFeatures
& (FADF_RECORD
))
321 IRecordInfo
*lpRecInfo
;
323 if (SUCCEEDED(SafeArrayGetRecordInfo(psa
, &lpRecInfo
)))
325 PBYTE pRecordData
= (PBYTE
)psa
->pvData
;
328 IRecordInfo_RecordClear(lpRecInfo
, pRecordData
);
329 pRecordData
+= psa
->cbElements
;
331 IRecordInfo_Release(lpRecInfo
);
334 else if (psa
->fFeatures
& FADF_BSTR
)
336 BSTR
* lpBstr
= (BSTR
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
341 SysFreeString(*lpBstr
);
345 else if (psa
->fFeatures
& FADF_VARIANT
)
347 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
351 VariantClear(lpVariant
);
359 /* Copy data items from one array to another */
360 static HRESULT
SAFEARRAY_CopyData(SAFEARRAY
*psa
, SAFEARRAY
*dest
)
362 if (!psa
->pvData
|| !dest
->pvData
|| psa
->fFeatures
& FADF_DATADELETED
)
366 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
368 if (ulCellCount
== SAFEARRAY_INVALID_CELLS
)
371 dest
->fFeatures
= (dest
->fFeatures
& FADF_CREATEVECTOR
) |
372 (psa
->fFeatures
& ~(FADF_CREATEVECTOR
|FADF_DATADELETED
));
374 if (psa
->fFeatures
& FADF_VARIANT
)
376 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
;
377 VARIANT
* lpDest
= (VARIANT
*)dest
->pvData
;
381 VariantCopy(lpDest
, lpVariant
);
386 else if (psa
->fFeatures
& FADF_BSTR
)
388 BSTR
* lpBstr
= (BSTR
*)psa
->pvData
;
389 BSTR
* lpDest
= (BSTR
*)dest
->pvData
;
395 *lpDest
= SysAllocStringLen(*lpBstr
, SysStringLen(*lpBstr
));
397 return E_OUTOFMEMORY
;
407 /* Copy the data over */
408 memcpy(dest
->pvData
, psa
->pvData
, ulCellCount
* psa
->cbElements
);
410 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
412 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)dest
->pvData
;
417 IUnknown_AddRef(*lpUnknown
);
423 if (psa
->fFeatures
& FADF_RECORD
)
425 IRecordInfo
* pRecInfo
= NULL
;
427 SafeArrayGetRecordInfo(psa
, &pRecInfo
);
428 SafeArraySetRecordInfo(dest
, pRecInfo
);
432 /* Release because Get() adds a reference */
433 IRecordInfo_Release(pRecInfo
);
436 else if (psa
->fFeatures
& FADF_HAVEIID
)
439 SafeArrayGetIID(psa
, &guid
);
440 SafeArraySetIID(dest
, &guid
);
442 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
444 SAFEARRAY_SetHiddenDWORD(dest
, SAFEARRAY_GetHiddenDWORD(psa
));
450 /*************************************************************************
451 * SafeArrayAllocDescriptor (OLEAUT32.36)
453 * Allocate and initialise a descriptor for a SafeArray.
456 * cDims [I] Number of dimensions of the array
457 * ppsaOut [O] Destination for new descriptor
460 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
461 * Failure: An HRESULT error code indicating the error.
466 HRESULT WINAPI
SafeArrayAllocDescriptor(UINT cDims
, SAFEARRAY
**ppsaOut
)
470 TRACE("(%d,%p)\n", cDims
, ppsaOut
);
472 if (!cDims
|| cDims
>= 0x10000) /* Maximum 65535 dimensions */
478 /* We need enough space for the header and its bounds */
479 allocSize
= sizeof(SAFEARRAY
) + sizeof(SAFEARRAYBOUND
) * (cDims
- 1);
481 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize
, ppsaOut
)))
484 (*ppsaOut
)->cDims
= cDims
;
486 TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims
, allocSize
);
490 /*************************************************************************
491 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
493 * Allocate and initialise a descriptor for a SafeArray of a given type.
496 * vt [I] The type of items to store in the array
497 * cDims [I] Number of dimensions of the array
498 * ppsaOut [O] Destination for new descriptor
501 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
502 * Failure: An HRESULT error code indicating the error.
505 * - This function does not chack that vt is an allowed VARTYPE.
506 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
509 HRESULT WINAPI
SafeArrayAllocDescriptorEx(VARTYPE vt
, UINT cDims
, SAFEARRAY
**ppsaOut
)
512 HRESULT hRet
= E_UNEXPECTED
;
514 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, ppsaOut
);
516 cbElements
= SAFEARRAY_GetVTSize(vt
);
518 WARN("Creating a descriptor with an invalid VARTYPE!\n");
520 hRet
= SafeArrayAllocDescriptor(cDims
, ppsaOut
);
524 SAFEARRAY_SetFeatures(vt
, *ppsaOut
);
525 (*ppsaOut
)->cbElements
= cbElements
;
530 /*************************************************************************
531 * SafeArrayAllocData (OLEAUT32.37)
533 * Allocate the data area of a SafeArray.
536 * psa [I] SafeArray to allocate the data area of.
539 * Success: S_OK. The data area is allocated and initialised.
540 * Failure: An HRESULT error code indicating the error.
545 HRESULT WINAPI
SafeArrayAllocData(SAFEARRAY
*psa
)
547 HRESULT hRet
= E_INVALIDARG
;
549 TRACE("(%p)\n", psa
);
553 ULONG ulSize
= SAFEARRAY_GetCellCount(psa
);
555 hRet
= E_OUTOFMEMORY
;
557 if (ulSize
!= SAFEARRAY_INVALID_CELLS
&& psa
->cbElements
)
559 psa
->pvData
= SAFEARRAY_Malloc(ulSize
* psa
->cbElements
);
564 TRACE("%lu bytes allocated for data at %p (%lu objects).\n",
565 ulSize
* psa
->cbElements
, psa
->pvData
, ulSize
);
572 /*************************************************************************
573 * SafeArrayCreate (OLEAUT32.15)
575 * Create a new SafeArray.
578 * vt [I] Type to store in the safe array
579 * cDims [I] Number of array dimensions
580 * rgsabound [I] Bounds of the array dimensions
583 * Success: A pointer to a new array object.
584 * Failure: NULL, if any parameter is invalid or memory allocation fails.
587 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
588 * in the Wine implementation.
591 SAFEARRAY
* WINAPI
SafeArrayCreate(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
)
593 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
);
598 return SAFEARRAY_Create(vt
, cDims
, rgsabound
, 0);
601 /*************************************************************************
602 * SafeArrayCreateEx (OLEAUT32.15)
604 * Create a new SafeArray.
607 * vt [I] Type to store in the safe array
608 * cDims [I] Number of array dimensions
609 * rgsabound [I] Bounds of the array dimensions
610 * pvExtra [I] Extra data
613 * Success: A pointer to a new array object.
614 * Failure: NULL, if any parameter is invalid or memory allocation fails.
619 SAFEARRAY
* WINAPI
SafeArrayCreateEx(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
, LPVOID pvExtra
)
622 IRecordInfo
* iRecInfo
= (IRecordInfo
*)pvExtra
;
625 TRACE("(%d->%s,%d,%p,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
, pvExtra
);
631 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
633 psa
= SAFEARRAY_Create(vt
, cDims
, rgsabound
, ulSize
);
640 SafeArraySetRecordInfo(psa
, pvExtra
);
644 SafeArraySetIID(psa
, pvExtra
);
651 /************************************************************************
652 * SafeArrayCreateVector (OLEAUT32.411)
654 * Create a one dimensional, contigous SafeArray.
657 * vt [I] Type to store in the safe array
658 * lLbound [I] Lower bound of the array
659 * cElements [I] Number of elements in the array
662 * Success: A pointer to a new array object.
663 * Failure: NULL, if any parameter is invalid or memory allocation fails.
668 SAFEARRAY
* WINAPI
SafeArrayCreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
)
670 TRACE("(%d->%s,%ld,%ld\n", vt
, debugstr_vt(vt
), lLbound
, cElements
);
675 return SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, SAFEARRAY_GetVTSize(vt
));
678 /************************************************************************
679 * SafeArrayCreateVectorEx (OLEAUT32.411)
681 * Create a one dimensional, contigous SafeArray.
684 * vt [I] Type to store in the safe array
685 * lLbound [I] Lower bound of the array
686 * cElements [I] Number of elements in the array
687 * pvExtra [I] Extra data
690 * Success: A pointer to a new array object.
691 * Failure: NULL, if any parameter is invalid or memory allocation fails.
696 SAFEARRAY
* WINAPI
SafeArrayCreateVectorEx(VARTYPE vt
, LONG lLbound
, ULONG cElements
, LPVOID pvExtra
)
699 IRecordInfo
* iRecInfo
= (IRecordInfo
*)pvExtra
;
702 TRACE("(%d->%s,%ld,%ld,%p\n", vt
, debugstr_vt(vt
), lLbound
, cElements
, pvExtra
);
708 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
711 ulSize
= SAFEARRAY_GetVTSize(vt
);
713 psa
= SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, ulSize
);
720 SafeArraySetRecordInfo(psa
, iRecInfo
);
724 SafeArraySetIID(psa
, pvExtra
);
731 /*************************************************************************
732 * SafeArrayDestroyDescriptor (OLEAUT32.38)
734 * Destroy a SafeArray.
737 * psa [I] SafeArray to destroy.
740 * Success: S_OK. The resources used by the array are freed.
741 * Failure: An HRESULT error code indicating the error.
746 HRESULT WINAPI
SafeArrayDestroyDescriptor(SAFEARRAY
*psa
)
748 TRACE("(%p)\n", psa
);
752 LPVOID lpv
= (char*)psa
- SAFEARRAY_HIDDEN_SIZE
;
755 return DISP_E_ARRAYISLOCKED
; /* Can't destroy a locked array */
757 if (psa
->fFeatures
& FADF_RECORD
)
758 SafeArraySetRecordInfo(psa
, NULL
);
760 if (psa
->fFeatures
& FADF_CREATEVECTOR
&&
761 !(psa
->fFeatures
& FADF_DATADELETED
))
762 SAFEARRAY_DestroyData(psa
, 0); /* Data not previously deleted */
764 if (!SAFEARRAY_Free(lpv
))
770 /*************************************************************************
771 * SafeArrayLock (OLEAUT32.21)
773 * Increment the lock counter of a SafeArray.
776 * psa [O] SafeArray to lock
779 * Success: S_OK. The array lock is incremented.
780 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
781 * are held on the array at once.
784 * In Win32 these locks are not thread safe.
787 HRESULT WINAPI
SafeArrayLock(SAFEARRAY
*psa
)
791 TRACE("(%p)\n", psa
);
796 ulLocks
= InterlockedIncrement(&psa
->cLocks
);
798 if (ulLocks
> 0xffff) /* Maximum of 16384 locks at a time */
800 WARN("Out of locks!\n");
801 InterlockedDecrement(&psa
->cLocks
);
807 /*************************************************************************
808 * SafeArrayUnlock (OLEAUT32.22)
810 * Decrement the lock counter of a SafeArray.
813 * psa [O] SafeArray to unlock
816 * Success: S_OK. The array lock is decremented.
817 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
823 HRESULT WINAPI
SafeArrayUnlock(SAFEARRAY
*psa
)
825 TRACE("(%p)\n", psa
);
830 if ((LONG
)InterlockedDecrement(&psa
->cLocks
) < 0)
832 WARN("Unlocked but no lock held!\n");
833 InterlockedIncrement(&psa
->cLocks
);
839 /*************************************************************************
840 * SafeArrayPutElement (OLEAUT32.26)
842 * Put an item into a SafeArray.
845 * psa [I] SafeArray to insert into
846 * rgIndices [I] Indices to insert at
847 * pvData [I] Data to insert
850 * Success: S_OK. The item is inserted
851 * Failure: An HRESULT error code indicating the error.
856 HRESULT WINAPI
SafeArrayPutElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
860 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
862 if (!psa
|| !rgIndices
)
867 ERR("Invalid pvData would crash under Win32!\n");
871 hRet
= SafeArrayLock(psa
);
877 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvDest
);
881 if (psa
->fFeatures
& FADF_VARIANT
)
883 VARIANT
* lpVariant
= (VARIANT
*)pvData
;
884 VARIANT
* lpDest
= (VARIANT
*)lpvDest
;
886 VariantClear(lpDest
);
887 VariantCopy(lpDest
, lpVariant
);
889 else if (psa
->fFeatures
& FADF_BSTR
)
891 BSTR
* lpBstr
= (BSTR
*)pvData
;
892 BSTR
* lpDest
= (BSTR
*)lpvDest
;
895 SysFreeString(*lpDest
);
899 *lpDest
= SysAllocStringLen(*lpBstr
, SysStringLen(*lpBstr
));
901 hRet
= E_OUTOFMEMORY
;
908 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
910 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)pvData
;
911 LPUNKNOWN
*lpDest
= (LPUNKNOWN
*)lpvDest
;
914 IUnknown_AddRef(*lpUnknown
);
916 IUnknown_Release(*lpDest
);
918 /* Copy the data over */
919 memcpy(lpvDest
, pvData
, psa
->cbElements
);
922 SafeArrayUnlock(psa
);
928 /*************************************************************************
929 * SafeArrayGetElement (OLEAUT32.25)
931 * Get an item from a SafeArray.
934 * psa [I] SafeArray to get from
935 * rgIndices [I] Indices to get from
936 * pvData [O] Destination for data
939 * Success: S_OK. The item data is returned in pvData.
940 * Failure: An HRESULT error code indicating the error.
945 HRESULT WINAPI
SafeArrayGetElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
949 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
951 if (!psa
|| !rgIndices
|| !pvData
)
954 hRet
= SafeArrayLock(psa
);
960 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvSrc
);
964 if (psa
->fFeatures
& FADF_VARIANT
)
966 VARIANT
* lpVariant
= (VARIANT
*)lpvSrc
;
967 VARIANT
* lpDest
= (VARIANT
*)pvData
;
969 VariantCopy(lpDest
, lpVariant
);
971 else if (psa
->fFeatures
& FADF_BSTR
)
973 BSTR
* lpBstr
= (BSTR
*)lpvSrc
;
974 BSTR
* lpDest
= (BSTR
*)pvData
;
978 *lpDest
= SysAllocStringLen(*lpBstr
, SysStringLen(*lpBstr
));
980 hRet
= E_OUTOFMEMORY
;
987 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
989 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)lpvSrc
;
992 IUnknown_AddRef(*lpUnknown
);
994 /* Copy the data over */
995 memcpy(pvData
, lpvSrc
, psa
->cbElements
);
998 SafeArrayUnlock(psa
);
1003 /*************************************************************************
1004 * SafeArrayGetUBound (OLEAUT32.19)
1006 * Get the upper bound for a given SafeArray dimension
1009 * psa [I] Array to get dimension upper bound from
1010 * nDim [I] The dimension number to get the upper bound of
1011 * plUbound [O] Destination for the upper bound
1014 * Success: S_OK. plUbound contains the dimensions upper bound.
1015 * Failure: An HRESULT error code indicating the error.
1020 HRESULT WINAPI
SafeArrayGetUBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plUbound
)
1022 TRACE("(%p,%d,%p)\n", psa
, nDim
, plUbound
);
1024 if (!psa
|| !plUbound
)
1025 return E_INVALIDARG
;
1027 if(!nDim
|| nDim
> psa
->cDims
|| !psa
->rgsabound
[nDim
- 1].cElements
)
1028 return DISP_E_BADINDEX
;
1030 *plUbound
= psa
->rgsabound
[nDim
- 1].lLbound
+
1031 psa
->rgsabound
[nDim
- 1].cElements
- 1;
1036 /*************************************************************************
1037 * SafeArrayGetLBound (OLEAUT32.20)
1039 * Get the lower bound for a given SafeArray dimension
1042 * psa [I] Array to get dimension lower bound from
1043 * nDim [I] The dimension number to get the lowe bound of
1044 * plLbound [O] Destination for the lower bound
1047 * Success: S_OK. plUbound contains the dimensions lower bound.
1048 * Failure: An HRESULT error code indicating the error.
1053 HRESULT WINAPI
SafeArrayGetLBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plLbound
)
1055 TRACE("(%p,%d,%p)\n", psa
, nDim
, plLbound
);
1057 if (!psa
|| !plLbound
)
1058 return E_INVALIDARG
;
1060 if(!nDim
|| nDim
> psa
->cDims
|| !psa
->rgsabound
[nDim
- 1].cElements
)
1061 return DISP_E_BADINDEX
;
1063 *plLbound
= psa
->rgsabound
[nDim
- 1].lLbound
;
1067 /*************************************************************************
1068 * SafeArrayGetDim (OLEAUT32.17)
1070 * Get the number of dimensions in a SafeArray.
1073 * psa [I] Array to get the dimensions of
1076 * The number of array dimensions in psa, or 0 if psa is NULL.
1081 UINT WINAPI
SafeArrayGetDim(SAFEARRAY
*psa
)
1083 TRACE("(%p) returning %ld\n", psa
, psa
? psa
->cDims
: 0ul);
1084 return psa
? psa
->cDims
: 0;
1087 /*************************************************************************
1088 * SafeArrayGetElemsize (OLEAUT32.18)
1090 * Get the size of an element in a SafeArray.
1093 * psa [I] Array to get the element size from
1096 * The size of a single element in psa, or 0 if psa is NULL.
1101 UINT WINAPI
SafeArrayGetElemsize(SAFEARRAY
*psa
)
1103 TRACE("(%p) returning %ld\n", psa
, psa
? psa
->cbElements
: 0ul);
1104 return psa
? psa
->cbElements
: 0;
1107 /*************************************************************************
1108 * SafeArrayAccessData (OLEAUT32.23)
1110 * Lock a SafeArray and return a pointer to its data.
1113 * psa [I] Array to get the data pointer from
1114 * ppvData [O] Destination for the arrays data pointer
1117 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1119 * Failure: An HRESULT error code indicating the error.
1124 HRESULT WINAPI
SafeArrayAccessData(SAFEARRAY
*psa
, void **ppvData
)
1126 TRACE("(%p,%p)\n", psa
, ppvData
);
1128 if(!psa
|| !ppvData
)
1129 return E_INVALIDARG
;
1131 if (SUCCEEDED(SafeArrayLock(psa
)))
1133 *ppvData
= psa
->pvData
;
1137 return E_UNEXPECTED
;
1141 /*************************************************************************
1142 * SafeArrayUnaccessData (OLEAUT32.24)
1144 * Unlock a SafeArray after accessing its data.
1147 * psa [I] Array to unlock
1150 * Success: S_OK. The array is unlocked.
1151 * Failure: An HRESULT error code indicating the error.
1156 HRESULT WINAPI
SafeArrayUnaccessData(SAFEARRAY
*psa
)
1158 TRACE("(%p)\n", psa
);
1159 return SafeArrayUnlock(psa
);
1162 /************************************************************************
1163 * SafeArrayPtrOfIndex (OLEAUT32.148)
1165 * Get the address of an item in a SafeArray.
1168 * psa [I] Array to get the items address from
1169 * rgIndices [I] Index of the item in the array
1170 * ppvData [O] Destination for item address
1173 * Success: S_OK. ppvData contains a pointer to the item.
1174 * Failure: An HRESULT error code indicating the error.
1177 * This function does not lock the array.
1182 HRESULT WINAPI
SafeArrayPtrOfIndex(SAFEARRAY
*psa
, LONG
*rgIndices
, void **ppvData
)
1185 ULONG cell
= 0, dimensionSize
= 1;
1186 SAFEARRAYBOUND
* psab
;
1189 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, ppvData
);
1191 /* The general formula for locating the cell number of an entry in an n
1192 * dimensional array (where cn = coordinate in dimension dn) is:
1194 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1196 * We calculate the size of the last dimension at each step through the
1197 * dimensions to avoid recursing to calculate the last dimensions size.
1199 if (!psa
|| !rgIndices
|| !ppvData
)
1200 return E_INVALIDARG
;
1202 psab
= psa
->rgsabound
;
1205 if (c1
< psab
->lLbound
|| c1
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1206 return DISP_E_BADINDEX
; /* Initial index out of bounds */
1208 for (dim
= 1; dim
< psa
->cDims
; dim
++)
1210 dimensionSize
*= psab
->cElements
;
1214 if (!psab
->cElements
||
1215 *rgIndices
< psab
->lLbound
||
1216 *rgIndices
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1217 return DISP_E_BADINDEX
; /* Index out of bounds */
1219 cell
+= (*rgIndices
- psab
->lLbound
) * dimensionSize
;
1223 cell
+= (c1
- psa
->rgsabound
[0].lLbound
);
1225 *ppvData
= (char*)psa
->pvData
+ cell
* psa
->cbElements
;
1229 /************************************************************************
1230 * SafeArrayDestroyData (OLEAUT32.39)
1232 * Destroy the data associated with a SafeArray.
1235 * psa [I] Array to delete the data from
1238 * Success: S_OK. All items and the item data are freed.
1239 * Failure: An HRESULT error code indicating the error.
1244 HRESULT WINAPI
SafeArrayDestroyData(SAFEARRAY
*psa
)
1246 TRACE("(%p)\n", psa
);
1249 return E_INVALIDARG
;
1252 return DISP_E_ARRAYISLOCKED
; /* Cant delete a locked array */
1256 /* Delete the actual item data */
1257 if (FAILED(SAFEARRAY_DestroyData(psa
, 0)))
1258 return E_UNEXPECTED
;
1260 /* If this is not a vector, free the data memory block */
1261 if (!(psa
->fFeatures
& FADF_CREATEVECTOR
))
1263 if (!SAFEARRAY_Free(psa
->pvData
))
1264 return E_UNEXPECTED
;
1268 psa
->fFeatures
|= FADF_DATADELETED
; /* Mark the data deleted */
1274 /************************************************************************
1275 * SafeArrayCopyData (OLEAUT32.412)
1277 * Copy all data from one SafeArray to another.
1280 * psaSource [I] Source for copy
1281 * psaTarget [O] Destination for copy
1284 * Success: S_OK. psaTarget contains a copy of psaSource.
1285 * Failure: An HRESULT error code indicating the error.
1288 * The two arrays must have the same number of dimensions and elements.
1293 HRESULT WINAPI
SafeArrayCopyData(SAFEARRAY
*psaSource
, SAFEARRAY
*psaTarget
)
1297 TRACE("(%p,%p)\n", psaSource
, psaTarget
);
1299 if (!psaSource
|| !psaTarget
||
1300 psaSource
->cDims
!= psaTarget
->cDims
||
1301 psaSource
->cbElements
!= psaTarget
->cbElements
)
1302 return E_INVALIDARG
;
1304 /* Each dimension must be the same size */
1305 for (dim
= psaSource
->cDims
- 1; dim
>= 0 ; dim
--)
1306 if (psaSource
->rgsabound
[dim
].cElements
!=
1307 psaTarget
->rgsabound
[dim
].cElements
)
1308 return E_INVALIDARG
;
1310 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget
, 0)) &&
1311 SUCCEEDED(SAFEARRAY_CopyData(psaSource
, psaTarget
)))
1313 return E_UNEXPECTED
;
1316 /************************************************************************
1317 * SafeArrayDestroy (OLEAUT32.16)
1319 * Destroy a SafeArray.
1322 * psa [I] Array to destroy
1325 * Success: S_OK. All resources used by the array are freed.
1326 * Failure: An HRESULT error code indicating the error.
1331 HRESULT WINAPI
SafeArrayDestroy(SAFEARRAY
*psa
)
1333 TRACE("(%p)\n", psa
);
1336 return E_INVALIDARG
;
1339 return DISP_E_ARRAYISLOCKED
;
1341 /* Native doesn't check to see if the free succeeds */
1342 SafeArrayDestroyData(psa
);
1343 SafeArrayDestroyDescriptor(psa
);
1347 /************************************************************************
1348 * SafeArrayCopy (OLEAUT32.27)
1350 * Make a duplicate of a SafeArray.
1353 * psa [I] Source for copy
1354 * ppsaOut [O] Destination for new copy
1357 * Success: S_OK. ppsaOut contains a copy of the array.
1358 * Failure: An HRESULT error code indicating the error.
1363 HRESULT WINAPI
SafeArrayCopy(SAFEARRAY
*psa
, SAFEARRAY
**ppsaOut
)
1367 TRACE("(%p,%p)\n", psa
, ppsaOut
);
1370 return E_INVALIDARG
;
1375 return S_OK
; /* Handles copying of NULL arrays */
1377 if (psa
->fFeatures
& (FADF_RECORD
|FADF_HAVEIID
|FADF_HAVEVARTYPE
))
1380 if (FAILED(SafeArrayGetVartype(psa
, &vt
)))
1381 hRet
= E_UNEXPECTED
;
1383 hRet
= SafeArrayAllocDescriptorEx(vt
, psa
->cDims
, ppsaOut
);
1387 hRet
= SafeArrayAllocDescriptor(psa
->cDims
, ppsaOut
);
1388 if (SUCCEEDED(hRet
))
1390 (*ppsaOut
)->fFeatures
= psa
->fFeatures
& ~FADF_CREATEVECTOR
;
1391 (*ppsaOut
)->cbElements
= psa
->cbElements
;
1395 if (SUCCEEDED(hRet
))
1397 /* Copy dimension bounds */
1398 memcpy((*ppsaOut
)->rgsabound
, psa
->rgsabound
, psa
->cDims
* sizeof(SAFEARRAYBOUND
));
1400 (*ppsaOut
)->pvData
= SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1402 if ((*ppsaOut
)->pvData
)
1404 hRet
= SAFEARRAY_CopyData(psa
, *ppsaOut
);
1406 if (SUCCEEDED(hRet
))
1409 SAFEARRAY_Free((*ppsaOut
)->pvData
);
1411 SafeArrayDestroyDescriptor(*ppsaOut
);
1417 /************************************************************************
1418 * SafeArrayRedim (OLEAUT32.40)
1420 * Changes the characteristics of the last dimension of a SafeArray
1423 * psa [I] Array to change
1424 * psabound [I] New bound details for the last dimension
1427 * Success: S_OK. psa is updated to reflect the new bounds.
1428 * Failure: An HRESULT error code indicating the error.
1433 HRESULT WINAPI
SafeArrayRedim(SAFEARRAY
*psa
, SAFEARRAYBOUND
*psabound
)
1435 SAFEARRAYBOUND
*oldBounds
;
1437 TRACE("(%p,%p)\n", psa
, psabound
);
1439 if (!psa
|| psa
->fFeatures
& FADF_FIXEDSIZE
|| !psabound
|| !psabound
->cElements
)
1440 return E_INVALIDARG
;
1442 if (psa
->cLocks
> 0)
1443 return DISP_E_ARRAYISLOCKED
;
1445 if (FAILED(SafeArrayLock(psa
)))
1446 return E_UNEXPECTED
;
1448 oldBounds
= &psa
->rgsabound
[psa
->cDims
- 1];
1449 oldBounds
->lLbound
= psabound
->lLbound
;
1451 if (psabound
->cElements
!= oldBounds
->cElements
)
1453 if (psabound
->cElements
< oldBounds
->cElements
)
1455 /* Shorten the final dimension. */
1456 ULONG ulStartCell
= psa
->cDims
== 1 ? 0 : SAFEARRAY_GetDimensionCells(psa
, psa
->cDims
- 1);
1458 ulStartCell
+= psabound
->cElements
;
1459 SAFEARRAY_DestroyData(psa
, ulStartCell
);
1463 /* Lengthen the final dimension */
1464 ULONG ulOldSize
, ulNewSize
;
1467 ulOldSize
= SAFEARRAY_GetCellCount(psa
);
1468 ulNewSize
= (ulOldSize
/ oldBounds
->cElements
) * psabound
->cElements
;
1470 if (ulOldSize
== SAFEARRAY_INVALID_CELLS
||
1471 !(pvNewData
= SAFEARRAY_Malloc(ulNewSize
)))
1473 SafeArrayUnlock(psa
);
1474 return E_UNEXPECTED
;
1477 memcpy(pvNewData
, psa
->pvData
, ulOldSize
);
1478 SAFEARRAY_Free(psa
->pvData
);
1479 psa
->pvData
= pvNewData
;
1481 oldBounds
->cElements
= psabound
->cElements
;
1484 SafeArrayUnlock(psa
);
1488 /************************************************************************
1489 * SafeArrayGetVartype (OLEAUT32.77)
1491 * Get the type of the items in a SafeArray.
1494 * psa [I] Array to get the type from
1495 * pvt [O] Destination for the type
1498 * Success: S_OK. pvt contains the type of the items.
1499 * Failure: An HRESULT error code indicating the error.
1504 HRESULT WINAPI
SafeArrayGetVartype(SAFEARRAY
* psa
, VARTYPE
* pvt
)
1506 TRACE("(%p,%p)\n", psa
, pvt
);
1509 return E_INVALIDARG
;
1511 if (psa
->fFeatures
& FADF_RECORD
)
1513 else if (psa
->fFeatures
& FADF_HAVEIID
)
1515 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
1517 VARTYPE vt
= SAFEARRAY_GetHiddenDWORD(psa
);
1521 return E_INVALIDARG
;
1526 /************************************************************************
1527 * SafeArraySetRecordInfo (OLEAUT32.@)
1529 * Set the record info for a SafeArray.
1532 * psa [I] Array to set the record info for
1533 * pRinfo [I] Record info
1536 * Success: S_OK. The record info is stored with the array.
1537 * Failure: An HRESULT error code indicating the error.
1542 HRESULT WINAPI
SafeArraySetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
*pRinfo
)
1544 IRecordInfo
** dest
= (IRecordInfo
**)psa
;
1546 TRACE("(%p,%p)\n", psa
, pRinfo
);
1548 if (!psa
|| !(psa
->fFeatures
& FADF_RECORD
))
1549 return E_INVALIDARG
;
1552 IRecordInfo_AddRef(pRinfo
);
1555 IRecordInfo_Release(dest
[-1]);
1561 /************************************************************************
1562 * SafeArrayGetRecordInfo (OLEAUT32.@)
1564 * Get the record info from a SafeArray.
1567 * psa [I] Array to get the record info from
1568 * pRinfo [O] Destination for the record info
1571 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1572 * Failure: An HRESULT error code indicating the error.
1577 HRESULT WINAPI
SafeArrayGetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
**pRinfo
)
1579 IRecordInfo
** src
= (IRecordInfo
**)psa
;
1581 TRACE("(%p,%p)\n", psa
, pRinfo
);
1583 if (!psa
|| !pRinfo
|| !(psa
->fFeatures
& FADF_RECORD
))
1584 return E_INVALIDARG
;
1589 IRecordInfo_AddRef(*pRinfo
);
1593 /************************************************************************
1594 * SafeArraySetIID (OLEAUT32.@)
1596 * Set the IID for a SafeArray.
1599 * psa [I] Array to set the IID from
1603 * Success: S_OK. The IID is stored with the array
1604 * Failure: An HRESULT error code indicating the error.
1609 HRESULT WINAPI
SafeArraySetIID(SAFEARRAY
*psa
, REFGUID guid
)
1611 GUID
* dest
= (GUID
*)psa
;
1613 TRACE("(%p,%s)\n", psa
, debugstr_guid(guid
));
1615 if (!psa
|| !guid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1616 return E_INVALIDARG
;
1622 /************************************************************************
1623 * SafeArrayGetIID (OLEAUT32.@)
1625 * Get the IID from a SafeArray.
1628 * psa [I] Array to get the ID from
1629 * pGuid [O] Destination for the IID
1632 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1633 * Failure: An HRESULT error code indicating the error.
1638 HRESULT WINAPI
SafeArrayGetIID(SAFEARRAY
*psa
, GUID
*pGuid
)
1640 GUID
* src
= (GUID
*)psa
;
1642 TRACE("(%p,%p)\n", psa
, pGuid
);
1644 if (!psa
|| !pGuid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1645 return E_INVALIDARG
;
1651 /************************************************************************
1652 * VectorFromBstr (OLEAUT32.@)
1654 * Create a SafeArray Vector from the bytes of a BSTR.
1657 * bstr [I] String to get bytes from
1658 * ppsa [O] Destination for the array
1661 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1662 * Failure: An HRESULT error code indicating the error.
1667 HRESULT WINAPI
VectorFromBstr(BSTR bstr
, SAFEARRAY
**ppsa
)
1671 TRACE("(%p,%p)\n", bstr
, ppsa
);
1674 return E_INVALIDARG
;
1677 sab
.cElements
= SysStringByteLen(bstr
);
1679 *ppsa
= SAFEARRAY_Create(VT_UI1
, 1, &sab
, 0);
1683 memcpy((*ppsa
)->pvData
, bstr
, sab
.cElements
);
1686 return E_OUTOFMEMORY
;
1689 /************************************************************************
1690 * BstrFromVector (OLEAUT32.@)
1692 * Create a BSTR from a SafeArray.
1695 * psa [I] Source array
1696 * pbstr [O] Destination for output BSTR
1699 * Success: S_OK. pbstr contains the arrays data.
1700 * Failure: An HRESULT error code indicating the error.
1703 * psa must be a 1 dimensional array of a 1 byte type.
1708 HRESULT WINAPI
BstrFromVector(SAFEARRAY
*psa
, BSTR
*pbstr
)
1710 TRACE("(%p,%p)\n", psa
, pbstr
);
1713 return E_INVALIDARG
;
1717 if (!psa
|| psa
->cbElements
!= 1 || psa
->cDims
!= 1)
1718 return E_INVALIDARG
;
1720 *pbstr
= SysAllocStringByteLen(psa
->pvData
, psa
->rgsabound
[0].cElements
);
1722 return E_OUTOFMEMORY
;