1 /*************************************************************************
2 * OLE Automation - SafeArray
4 * This file contains the implementation of the SafeArray functions.
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 2002-2003 Marcus Meissner
8 * Copyright 2003 Jon Griffiths
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 /* Memory Layout of a SafeArray:
26 * -0x10: start of memory.
27 * -0x10: GUID for VT_DISPATCH and VT_UNKNOWN safearrays (if FADF_HAVEIID)
28 * -0x04: DWORD varianttype; (for all others, except VT_RECORD) (if FADF_HAVEVARTYPE)
29 * -0x4: IRecordInfo* iface; (if FADF_RECORD, for VT_RECORD (can be NULL))
31 * 0x10: SAFEARRAYBOUNDS[0...]
43 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
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)
87 static inline LPVOID
SAFEARRAY_Malloc(ULONG ulSize
)
89 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
90 * instance returned from CoGetMalloc().
92 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulSize
);
96 static inline BOOL
SAFEARRAY_Free(LPVOID lpData
)
98 return HeapFree(GetProcessHeap(), 0, lpData
);
101 /* Get the size of a supported VT type (0 means unsupported) */
102 static DWORD
SAFEARRAY_GetVTSize(VARTYPE vt
)
107 case VT_UI1
: return sizeof(BYTE
);
110 case VT_UI2
: return sizeof(SHORT
);
114 case VT_ERROR
: return sizeof(LONG
);
117 case VT_UI8
: return sizeof(LONG64
);
119 case VT_UINT
: return sizeof(INT
);
121 case VT_UINT_PTR
: return sizeof(UINT_PTR
);
122 case VT_CY
: return sizeof(CY
);
123 case VT_DATE
: return sizeof(DATE
);
124 case VT_BSTR
: return sizeof(BSTR
);
125 case VT_DISPATCH
: return sizeof(LPDISPATCH
);
126 case VT_VARIANT
: return sizeof(VARIANT
);
127 case VT_UNKNOWN
: return sizeof(LPUNKNOWN
);
128 case VT_DECIMAL
: return sizeof(DECIMAL
);
129 /* Note: Return a non-zero size to indicate vt is valid. The actual size
130 * of a UDT is taken from the result of IRecordInfo_GetSize().
132 case VT_RECORD
: return 32;
137 /* Set the hidden data for an array */
138 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY
* psa
, DWORD dw
)
140 /* Implementation data is stored in the 4 bytes before the header */
141 LPDWORD lpDw
= (LPDWORD
)psa
;
145 /* Get the hidden data from an array */
146 static inline DWORD
SAFEARRAY_GetHiddenDWORD(SAFEARRAY
* psa
)
148 LPDWORD lpDw
= (LPDWORD
)psa
;
152 /* Get the number of cells in a SafeArray */
153 static ULONG
SAFEARRAY_GetCellCount(const SAFEARRAY
*psa
)
155 SAFEARRAYBOUND
* psab
= psa
->rgsabound
;
156 USHORT cCount
= psa
->cDims
;
157 ULONG ulNumCells
= 1;
161 /* This is a valid bordercase. See testcases. -Marcus */
162 if (!psab
->cElements
)
164 ulNumCells
*= psab
->cElements
;
170 /* Get the 0 based index of an index into a dimension */
171 static inline ULONG
SAFEARRAY_GetDimensionIndex(SAFEARRAYBOUND
*psab
, ULONG ulIndex
)
173 return ulIndex
- psab
->lLbound
;
176 /* Get the size of a dimension in cells */
177 static inline ULONG
SAFEARRAY_GetDimensionCells(SAFEARRAY
*psa
, ULONG ulDim
)
179 ULONG size
= psa
->rgsabound
[0].cElements
;
183 size
*= psa
->rgsabound
[ulDim
].cElements
;
189 /* Allocate a descriptor for an array */
190 static HRESULT
SAFEARRAY_AllocDescriptor(ULONG ulSize
, SAFEARRAY
**ppsaOut
)
192 *ppsaOut
= (SAFEARRAY
*)((char*)SAFEARRAY_Malloc(ulSize
+ SAFEARRAY_HIDDEN_SIZE
) + SAFEARRAY_HIDDEN_SIZE
);
200 /* Set the features of an array */
201 static void SAFEARRAY_SetFeatures(VARTYPE vt
, SAFEARRAY
*psa
)
203 /* Set the IID if we have one, otherwise set the type */
204 if (vt
== VT_DISPATCH
)
206 psa
->fFeatures
= FADF_HAVEIID
;
207 SafeArraySetIID(psa
, &IID_IDispatch
);
209 else if (vt
== VT_UNKNOWN
)
211 psa
->fFeatures
= FADF_HAVEIID
;
212 SafeArraySetIID(psa
, &IID_IUnknown
);
214 else if (vt
== VT_RECORD
)
215 psa
->fFeatures
= FADF_RECORD
;
218 psa
->fFeatures
= FADF_HAVEVARTYPE
;
219 SAFEARRAY_SetHiddenDWORD(psa
, vt
);
223 /* Create an array */
224 static SAFEARRAY
* SAFEARRAY_Create(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
, ULONG ulSize
)
226 SAFEARRAY
*psa
= NULL
;
231 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt
, cDims
, &psa
)))
235 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
236 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
237 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
238 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
241 memcpy(psa
->rgsabound
, rgsabound
, cDims
* sizeof(SAFEARRAYBOUND
));
244 psa
->cbElements
= ulSize
;
246 if (FAILED(SafeArrayAllocData(psa
)))
248 SafeArrayDestroyDescriptor(psa
);
255 /* Create an array as a vector */
256 static SAFEARRAY
* SAFEARRAY_CreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
, ULONG ulSize
)
258 SAFEARRAY
*psa
= NULL
;
260 if (ulSize
|| (vt
== VT_RECORD
))
262 /* Allocate the header and data together */
263 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY
) + ulSize
* cElements
, &psa
)))
265 SAFEARRAY_SetFeatures(vt
, psa
);
268 psa
->fFeatures
|= FADF_CREATEVECTOR
;
269 psa
->pvData
= &psa
[1]; /* Data follows the header */
270 psa
->cbElements
= ulSize
;
271 psa
->rgsabound
[0].cElements
= cElements
;
272 psa
->rgsabound
[0].lLbound
= lLbound
;
276 case VT_BSTR
: psa
->fFeatures
|= FADF_BSTR
; break;
277 case VT_UNKNOWN
: psa
->fFeatures
|= FADF_UNKNOWN
; break;
278 case VT_DISPATCH
: psa
->fFeatures
|= FADF_DISPATCH
; break;
279 case VT_VARIANT
: psa
->fFeatures
|= FADF_VARIANT
; break;
286 /* Free data items in an array */
287 static HRESULT
SAFEARRAY_DestroyData(SAFEARRAY
*psa
, ULONG ulStartCell
)
289 if (psa
->pvData
&& !(psa
->fFeatures
& FADF_DATADELETED
))
291 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
293 if (ulStartCell
> ulCellCount
) {
294 FIXME("unexpted ulcellcount %ld, start %ld\n",ulCellCount
,ulStartCell
);
298 ulCellCount
-= ulStartCell
;
300 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
302 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
307 IUnknown_Release(*lpUnknown
);
311 else if (psa
->fFeatures
& (FADF_RECORD
))
313 IRecordInfo
*lpRecInfo
;
315 if (SUCCEEDED(SafeArrayGetRecordInfo(psa
, &lpRecInfo
)))
317 PBYTE pRecordData
= (PBYTE
)psa
->pvData
;
320 IRecordInfo_RecordClear(lpRecInfo
, pRecordData
);
321 pRecordData
+= psa
->cbElements
;
323 IRecordInfo_Release(lpRecInfo
);
326 else if (psa
->fFeatures
& FADF_BSTR
)
328 BSTR
* lpBstr
= (BSTR
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
333 SysFreeString(*lpBstr
);
337 else if (psa
->fFeatures
& FADF_VARIANT
)
339 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
+ ulStartCell
* psa
->cbElements
;
343 VariantClear(lpVariant
);
351 /* Copy data items from one array to another */
352 static HRESULT
SAFEARRAY_CopyData(SAFEARRAY
*psa
, SAFEARRAY
*dest
)
354 if (!psa
->pvData
|| !dest
->pvData
|| psa
->fFeatures
& FADF_DATADELETED
)
358 ULONG ulCellCount
= SAFEARRAY_GetCellCount(psa
);
360 dest
->fFeatures
= (dest
->fFeatures
& FADF_CREATEVECTOR
) |
361 (psa
->fFeatures
& ~(FADF_CREATEVECTOR
|FADF_DATADELETED
));
363 if (psa
->fFeatures
& FADF_VARIANT
)
365 VARIANT
* lpVariant
= (VARIANT
*)psa
->pvData
;
366 VARIANT
* lpDest
= (VARIANT
*)dest
->pvData
;
370 VariantCopy(lpDest
, lpVariant
);
375 else if (psa
->fFeatures
& FADF_BSTR
)
377 BSTR
* lpBstr
= (BSTR
*)psa
->pvData
;
378 BSTR
* lpDest
= (BSTR
*)dest
->pvData
;
384 *lpDest
= SysAllocStringByteLen((char*)*lpBstr
, SysStringByteLen(*lpBstr
));
386 return E_OUTOFMEMORY
;
396 /* Copy the data over */
397 memcpy(dest
->pvData
, psa
->pvData
, ulCellCount
* psa
->cbElements
);
399 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
401 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)dest
->pvData
;
406 IUnknown_AddRef(*lpUnknown
);
412 if (psa
->fFeatures
& FADF_RECORD
)
414 IRecordInfo
* pRecInfo
= NULL
;
416 SafeArrayGetRecordInfo(psa
, &pRecInfo
);
417 SafeArraySetRecordInfo(dest
, pRecInfo
);
421 /* Release because Get() adds a reference */
422 IRecordInfo_Release(pRecInfo
);
425 else if (psa
->fFeatures
& FADF_HAVEIID
)
428 SafeArrayGetIID(psa
, &guid
);
429 SafeArraySetIID(dest
, &guid
);
431 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
433 SAFEARRAY_SetHiddenDWORD(dest
, SAFEARRAY_GetHiddenDWORD(psa
));
439 /*************************************************************************
440 * SafeArrayAllocDescriptor (OLEAUT32.36)
442 * Allocate and initialise a descriptor for a SafeArray.
445 * cDims [I] Number of dimensions of the array
446 * ppsaOut [O] Destination for new descriptor
449 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
450 * Failure: An HRESULT error code indicating the error.
455 HRESULT WINAPI
SafeArrayAllocDescriptor(UINT cDims
, SAFEARRAY
**ppsaOut
)
459 TRACE("(%d,%p)\n", cDims
, ppsaOut
);
461 if (!cDims
|| cDims
>= 0x10000) /* Maximum 65535 dimensions */
467 /* We need enough space for the header and its bounds */
468 allocSize
= sizeof(SAFEARRAY
) + sizeof(SAFEARRAYBOUND
) * (cDims
- 1);
470 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize
, ppsaOut
)))
473 (*ppsaOut
)->cDims
= cDims
;
475 TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims
, allocSize
);
479 /*************************************************************************
480 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
482 * Allocate and initialise a descriptor for a SafeArray of a given type.
485 * vt [I] The type of items to store in the array
486 * cDims [I] Number of dimensions of the array
487 * ppsaOut [O] Destination for new descriptor
490 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
491 * Failure: An HRESULT error code indicating the error.
494 * - This function does not chack that vt is an allowed VARTYPE.
495 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
498 HRESULT WINAPI
SafeArrayAllocDescriptorEx(VARTYPE vt
, UINT cDims
, SAFEARRAY
**ppsaOut
)
501 HRESULT hRet
= E_UNEXPECTED
;
503 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, ppsaOut
);
505 cbElements
= SAFEARRAY_GetVTSize(vt
);
507 WARN("Creating a descriptor with an invalid VARTYPE!\n");
509 hRet
= SafeArrayAllocDescriptor(cDims
, ppsaOut
);
513 SAFEARRAY_SetFeatures(vt
, *ppsaOut
);
514 (*ppsaOut
)->cbElements
= cbElements
;
519 /*************************************************************************
520 * SafeArrayAllocData (OLEAUT32.37)
522 * Allocate the data area of a SafeArray.
525 * psa [I] SafeArray to allocate the data area of.
528 * Success: S_OK. The data area is allocated and initialised.
529 * Failure: An HRESULT error code indicating the error.
534 HRESULT WINAPI
SafeArrayAllocData(SAFEARRAY
*psa
)
536 HRESULT hRet
= E_INVALIDARG
;
538 TRACE("(%p)\n", psa
);
542 ULONG ulSize
= SAFEARRAY_GetCellCount(psa
);
544 hRet
= E_OUTOFMEMORY
;
548 psa
->pvData
= SAFEARRAY_Malloc(ulSize
* psa
->cbElements
);
553 TRACE("%lu bytes allocated for data at %p (%lu objects).\n",
554 ulSize
* psa
->cbElements
, psa
->pvData
, ulSize
);
561 /*************************************************************************
562 * SafeArrayCreate (OLEAUT32.15)
564 * Create a new SafeArray.
567 * vt [I] Type to store in the safe array
568 * cDims [I] Number of array dimensions
569 * rgsabound [I] Bounds of the array dimensions
572 * Success: A pointer to a new array object.
573 * Failure: NULL, if any parameter is invalid or memory allocation fails.
576 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
577 * in the Wine implementation.
580 SAFEARRAY
* WINAPI
SafeArrayCreate(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
)
582 TRACE("(%d->%s,%d,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
);
587 return SAFEARRAY_Create(vt
, cDims
, rgsabound
, 0);
590 /*************************************************************************
591 * SafeArrayCreateEx (OLEAUT32.15)
593 * Create a new SafeArray.
596 * vt [I] Type to store in the safe array
597 * cDims [I] Number of array dimensions
598 * rgsabound [I] Bounds of the array dimensions
599 * pvExtra [I] Extra data
602 * Success: A pointer to a new array object.
603 * Failure: NULL, if any parameter is invalid or memory allocation fails.
608 SAFEARRAY
* WINAPI
SafeArrayCreateEx(VARTYPE vt
, UINT cDims
, SAFEARRAYBOUND
*rgsabound
, LPVOID pvExtra
)
611 IRecordInfo
* iRecInfo
= (IRecordInfo
*)pvExtra
;
614 TRACE("(%d->%s,%d,%p,%p)\n", vt
, debugstr_vt(vt
), cDims
, rgsabound
, pvExtra
);
620 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
622 psa
= SAFEARRAY_Create(vt
, cDims
, rgsabound
, ulSize
);
629 SafeArraySetRecordInfo(psa
, pvExtra
);
633 SafeArraySetIID(psa
, pvExtra
);
640 /************************************************************************
641 * SafeArrayCreateVector (OLEAUT32.411)
643 * Create a one dimensional, contigous SafeArray.
646 * vt [I] Type to store in the safe array
647 * lLbound [I] Lower bound of the array
648 * cElements [I] Number of elements in the array
651 * Success: A pointer to a new array object.
652 * Failure: NULL, if any parameter is invalid or memory allocation fails.
657 SAFEARRAY
* WINAPI
SafeArrayCreateVector(VARTYPE vt
, LONG lLbound
, ULONG cElements
)
659 TRACE("(%d->%s,%ld,%ld\n", vt
, debugstr_vt(vt
), lLbound
, cElements
);
664 return SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, SAFEARRAY_GetVTSize(vt
));
667 /************************************************************************
668 * SafeArrayCreateVectorEx (OLEAUT32.411)
670 * Create a one dimensional, contigous SafeArray.
673 * vt [I] Type to store in the safe array
674 * lLbound [I] Lower bound of the array
675 * cElements [I] Number of elements in the array
676 * pvExtra [I] Extra data
679 * Success: A pointer to a new array object.
680 * Failure: NULL, if any parameter is invalid or memory allocation fails.
685 SAFEARRAY
* WINAPI
SafeArrayCreateVectorEx(VARTYPE vt
, LONG lLbound
, ULONG cElements
, LPVOID pvExtra
)
688 IRecordInfo
* iRecInfo
= (IRecordInfo
*)pvExtra
;
691 TRACE("(%d->%s,%ld,%ld,%p\n", vt
, debugstr_vt(vt
), lLbound
, cElements
, pvExtra
);
697 IRecordInfo_GetSize(iRecInfo
, &ulSize
);
700 ulSize
= SAFEARRAY_GetVTSize(vt
);
702 psa
= SAFEARRAY_CreateVector(vt
, lLbound
, cElements
, ulSize
);
709 SafeArraySetRecordInfo(psa
, iRecInfo
);
713 SafeArraySetIID(psa
, pvExtra
);
720 /*************************************************************************
721 * SafeArrayDestroyDescriptor (OLEAUT32.38)
723 * Destroy a SafeArray.
726 * psa [I] SafeArray to destroy.
729 * Success: S_OK. The resources used by the array are freed.
730 * Failure: An HRESULT error code indicating the error.
735 HRESULT WINAPI
SafeArrayDestroyDescriptor(SAFEARRAY
*psa
)
737 TRACE("(%p)\n", psa
);
741 LPVOID lpv
= (char*)psa
- SAFEARRAY_HIDDEN_SIZE
;
744 return DISP_E_ARRAYISLOCKED
; /* Can't destroy a locked array */
746 if (psa
->fFeatures
& FADF_RECORD
)
747 SafeArraySetRecordInfo(psa
, NULL
);
749 if (psa
->fFeatures
& FADF_CREATEVECTOR
&&
750 !(psa
->fFeatures
& FADF_DATADELETED
))
751 SAFEARRAY_DestroyData(psa
, 0); /* Data not previously deleted */
753 if (!SAFEARRAY_Free(lpv
))
759 /*************************************************************************
760 * SafeArrayLock (OLEAUT32.21)
762 * Increment the lock counter of a SafeArray.
765 * psa [O] SafeArray to lock
768 * Success: S_OK. The array lock is incremented.
769 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
770 * are held on the array at once.
773 * In Win32 these locks are not thread safe.
776 HRESULT WINAPI
SafeArrayLock(SAFEARRAY
*psa
)
780 TRACE("(%p)\n", psa
);
785 ulLocks
= InterlockedIncrement(&psa
->cLocks
);
787 if (ulLocks
> 0xffff) /* Maximum of 16384 locks at a time */
789 WARN("Out of locks!\n");
790 InterlockedDecrement(&psa
->cLocks
);
796 /*************************************************************************
797 * SafeArrayUnlock (OLEAUT32.22)
799 * Decrement the lock counter of a SafeArray.
802 * psa [O] SafeArray to unlock
805 * Success: S_OK. The array lock is decremented.
806 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
812 HRESULT WINAPI
SafeArrayUnlock(SAFEARRAY
*psa
)
814 TRACE("(%p)\n", psa
);
819 if ((LONG
)InterlockedDecrement(&psa
->cLocks
) < 0)
821 WARN("Unlocked but no lock held!\n");
822 InterlockedIncrement(&psa
->cLocks
);
828 /*************************************************************************
829 * SafeArrayPutElement (OLEAUT32.26)
831 * Put an item into a SafeArray.
834 * psa [I] SafeArray to insert into
835 * rgIndices [I] Indices to insert at
836 * pvData [I] Data to insert
839 * Success: S_OK. The item is inserted
840 * Failure: An HRESULT error code indicating the error.
845 HRESULT WINAPI
SafeArrayPutElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
849 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
851 if (!psa
|| !rgIndices
)
856 ERR("Invalid pvData would crash under Win32!\n");
860 hRet
= SafeArrayLock(psa
);
866 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvDest
);
870 if (psa
->fFeatures
& FADF_VARIANT
)
872 VARIANT
* lpVariant
= (VARIANT
*)pvData
;
873 VARIANT
* lpDest
= (VARIANT
*)lpvDest
;
875 VariantClear(lpDest
);
876 VariantCopy(lpDest
, lpVariant
);
878 else if (psa
->fFeatures
& FADF_BSTR
)
880 BSTR lpBstr
= (BSTR
)pvData
;
881 BSTR
* lpDest
= (BSTR
*)lpvDest
;
884 SysFreeString(*lpDest
);
888 *lpDest
= SysAllocStringByteLen((char*)lpBstr
, SysStringByteLen(lpBstr
));
890 hRet
= E_OUTOFMEMORY
;
897 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
899 LPUNKNOWN lpUnknown
= (LPUNKNOWN
)pvData
;
900 LPUNKNOWN
*lpDest
= (LPUNKNOWN
*)lpvDest
;
903 IUnknown_AddRef(lpUnknown
);
905 IUnknown_Release(*lpDest
);
908 /* Copy the data over */
909 memcpy(lpvDest
, pvData
, psa
->cbElements
);
913 SafeArrayUnlock(psa
);
919 /*************************************************************************
920 * SafeArrayGetElement (OLEAUT32.25)
922 * Get an item from a SafeArray.
925 * psa [I] SafeArray to get from
926 * rgIndices [I] Indices to get from
927 * pvData [O] Destination for data
930 * Success: S_OK. The item data is returned in pvData.
931 * Failure: An HRESULT error code indicating the error.
936 HRESULT WINAPI
SafeArrayGetElement(SAFEARRAY
*psa
, LONG
*rgIndices
, void *pvData
)
940 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, pvData
);
942 if (!psa
|| !rgIndices
|| !pvData
)
945 hRet
= SafeArrayLock(psa
);
951 hRet
= SafeArrayPtrOfIndex(psa
, rgIndices
, &lpvSrc
);
955 if (psa
->fFeatures
& FADF_VARIANT
)
957 VARIANT
* lpVariant
= (VARIANT
*)lpvSrc
;
958 VARIANT
* lpDest
= (VARIANT
*)pvData
;
960 VariantCopy(lpDest
, lpVariant
);
962 else if (psa
->fFeatures
& FADF_BSTR
)
964 BSTR
* lpBstr
= (BSTR
*)lpvSrc
;
965 BSTR
* lpDest
= (BSTR
*)pvData
;
969 *lpDest
= SysAllocStringByteLen((char*)*lpBstr
, SysStringByteLen(*lpBstr
));
971 hRet
= E_OUTOFMEMORY
;
978 if (psa
->fFeatures
& (FADF_UNKNOWN
|FADF_DISPATCH
))
980 LPUNKNOWN
*lpUnknown
= (LPUNKNOWN
*)lpvSrc
;
983 IUnknown_AddRef(*lpUnknown
);
985 /* Copy the data over */
986 memcpy(pvData
, lpvSrc
, psa
->cbElements
);
989 SafeArrayUnlock(psa
);
994 /*************************************************************************
995 * SafeArrayGetUBound (OLEAUT32.19)
997 * Get the upper bound for a given SafeArray dimension
1000 * psa [I] Array to get dimension upper bound from
1001 * nDim [I] The dimension number to get the upper bound of
1002 * plUbound [O] Destination for the upper bound
1005 * Success: S_OK. plUbound contains the dimensions upper bound.
1006 * Failure: An HRESULT error code indicating the error.
1011 HRESULT WINAPI
SafeArrayGetUBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plUbound
)
1013 TRACE("(%p,%d,%p)\n", psa
, nDim
, plUbound
);
1015 if (!psa
|| !plUbound
)
1016 return E_INVALIDARG
;
1018 if(!nDim
|| nDim
> psa
->cDims
)
1019 return DISP_E_BADINDEX
;
1021 *plUbound
= psa
->rgsabound
[nDim
- 1].lLbound
+
1022 psa
->rgsabound
[nDim
- 1].cElements
- 1;
1027 /*************************************************************************
1028 * SafeArrayGetLBound (OLEAUT32.20)
1030 * Get the lower bound for a given SafeArray dimension
1033 * psa [I] Array to get dimension lower bound from
1034 * nDim [I] The dimension number to get the lowe bound of
1035 * plLbound [O] Destination for the lower bound
1038 * Success: S_OK. plUbound contains the dimensions lower bound.
1039 * Failure: An HRESULT error code indicating the error.
1044 HRESULT WINAPI
SafeArrayGetLBound(SAFEARRAY
*psa
, UINT nDim
, LONG
*plLbound
)
1046 TRACE("(%p,%d,%p)\n", psa
, nDim
, plLbound
);
1048 if (!psa
|| !plLbound
)
1049 return E_INVALIDARG
;
1051 if(!nDim
|| nDim
> psa
->cDims
)
1052 return DISP_E_BADINDEX
;
1054 *plLbound
= psa
->rgsabound
[nDim
- 1].lLbound
;
1058 /*************************************************************************
1059 * SafeArrayGetDim (OLEAUT32.17)
1061 * Get the number of dimensions in a SafeArray.
1064 * psa [I] Array to get the dimensions of
1067 * The number of array dimensions in psa, or 0 if psa is NULL.
1072 UINT WINAPI
SafeArrayGetDim(SAFEARRAY
*psa
)
1074 TRACE("(%p) returning %ld\n", psa
, psa
? psa
->cDims
: 0ul);
1075 return psa
? psa
->cDims
: 0;
1078 /*************************************************************************
1079 * SafeArrayGetElemsize (OLEAUT32.18)
1081 * Get the size of an element in a SafeArray.
1084 * psa [I] Array to get the element size from
1087 * The size of a single element in psa, or 0 if psa is NULL.
1092 UINT WINAPI
SafeArrayGetElemsize(SAFEARRAY
*psa
)
1094 TRACE("(%p) returning %ld\n", psa
, psa
? psa
->cbElements
: 0ul);
1095 return psa
? psa
->cbElements
: 0;
1098 /*************************************************************************
1099 * SafeArrayAccessData (OLEAUT32.23)
1101 * Lock a SafeArray and return a pointer to its data.
1104 * psa [I] Array to get the data pointer from
1105 * ppvData [O] Destination for the arrays data pointer
1108 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1110 * Failure: An HRESULT error code indicating the error.
1115 HRESULT WINAPI
SafeArrayAccessData(SAFEARRAY
*psa
, void **ppvData
)
1117 TRACE("(%p,%p)\n", psa
, ppvData
);
1119 if(!psa
|| !ppvData
)
1120 return E_INVALIDARG
;
1122 if (SUCCEEDED(SafeArrayLock(psa
)))
1124 *ppvData
= psa
->pvData
;
1128 return E_UNEXPECTED
;
1132 /*************************************************************************
1133 * SafeArrayUnaccessData (OLEAUT32.24)
1135 * Unlock a SafeArray after accessing its data.
1138 * psa [I] Array to unlock
1141 * Success: S_OK. The array is unlocked.
1142 * Failure: An HRESULT error code indicating the error.
1147 HRESULT WINAPI
SafeArrayUnaccessData(SAFEARRAY
*psa
)
1149 TRACE("(%p)\n", psa
);
1150 return SafeArrayUnlock(psa
);
1153 /************************************************************************
1154 * SafeArrayPtrOfIndex (OLEAUT32.148)
1156 * Get the address of an item in a SafeArray.
1159 * psa [I] Array to get the items address from
1160 * rgIndices [I] Index of the item in the array
1161 * ppvData [O] Destination for item address
1164 * Success: S_OK. ppvData contains a pointer to the item.
1165 * Failure: An HRESULT error code indicating the error.
1168 * This function does not lock the array.
1173 HRESULT WINAPI
SafeArrayPtrOfIndex(SAFEARRAY
*psa
, LONG
*rgIndices
, void **ppvData
)
1176 ULONG cell
= 0, dimensionSize
= 1;
1177 SAFEARRAYBOUND
* psab
;
1180 TRACE("(%p,%p,%p)\n", psa
, rgIndices
, ppvData
);
1182 /* The general formula for locating the cell number of an entry in an n
1183 * dimensional array (where cn = coordinate in dimension dn) is:
1185 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1187 * We calculate the size of the last dimension at each step through the
1188 * dimensions to avoid recursing to calculate the last dimensions size.
1190 if (!psa
|| !rgIndices
|| !ppvData
)
1191 return E_INVALIDARG
;
1193 psab
= psa
->rgsabound
;
1196 if (c1
< psab
->lLbound
|| c1
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1197 return DISP_E_BADINDEX
; /* Initial index out of bounds */
1199 for (dim
= 1; dim
< psa
->cDims
; dim
++)
1201 dimensionSize
*= psab
->cElements
;
1205 if (!psab
->cElements
||
1206 *rgIndices
< psab
->lLbound
||
1207 *rgIndices
>= psab
->lLbound
+ (LONG
)psab
->cElements
)
1208 return DISP_E_BADINDEX
; /* Index out of bounds */
1210 cell
+= (*rgIndices
- psab
->lLbound
) * dimensionSize
;
1214 cell
+= (c1
- psa
->rgsabound
[0].lLbound
);
1216 *ppvData
= (char*)psa
->pvData
+ cell
* psa
->cbElements
;
1220 /************************************************************************
1221 * SafeArrayDestroyData (OLEAUT32.39)
1223 * Destroy the data associated with a SafeArray.
1226 * psa [I] Array to delete the data from
1229 * Success: S_OK. All items and the item data are freed.
1230 * Failure: An HRESULT error code indicating the error.
1235 HRESULT WINAPI
SafeArrayDestroyData(SAFEARRAY
*psa
)
1237 TRACE("(%p)\n", psa
);
1240 return E_INVALIDARG
;
1243 return DISP_E_ARRAYISLOCKED
; /* Can't delete a locked array */
1247 /* Delete the actual item data */
1248 if (FAILED(SAFEARRAY_DestroyData(psa
, 0)))
1249 return E_UNEXPECTED
;
1251 /* If this is not a vector, free the data memory block */
1252 if (!(psa
->fFeatures
& FADF_CREATEVECTOR
))
1254 if (!SAFEARRAY_Free(psa
->pvData
))
1255 return E_UNEXPECTED
;
1259 psa
->fFeatures
|= FADF_DATADELETED
; /* Mark the data deleted */
1265 /************************************************************************
1266 * SafeArrayCopyData (OLEAUT32.412)
1268 * Copy all data from one SafeArray to another.
1271 * psaSource [I] Source for copy
1272 * psaTarget [O] Destination for copy
1275 * Success: S_OK. psaTarget contains a copy of psaSource.
1276 * Failure: An HRESULT error code indicating the error.
1279 * The two arrays must have the same number of dimensions and elements.
1284 HRESULT WINAPI
SafeArrayCopyData(SAFEARRAY
*psaSource
, SAFEARRAY
*psaTarget
)
1288 TRACE("(%p,%p)\n", psaSource
, psaTarget
);
1290 if (!psaSource
|| !psaTarget
||
1291 psaSource
->cDims
!= psaTarget
->cDims
||
1292 psaSource
->cbElements
!= psaTarget
->cbElements
)
1293 return E_INVALIDARG
;
1295 /* Each dimension must be the same size */
1296 for (dim
= psaSource
->cDims
- 1; dim
>= 0 ; dim
--)
1297 if (psaSource
->rgsabound
[dim
].cElements
!=
1298 psaTarget
->rgsabound
[dim
].cElements
)
1299 return E_INVALIDARG
;
1301 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget
, 0)) &&
1302 SUCCEEDED(SAFEARRAY_CopyData(psaSource
, psaTarget
)))
1304 return E_UNEXPECTED
;
1307 /************************************************************************
1308 * SafeArrayDestroy (OLEAUT32.16)
1310 * Destroy a SafeArray.
1313 * psa [I] Array to destroy
1316 * Success: S_OK. All resources used by the array are freed.
1317 * Failure: An HRESULT error code indicating the error.
1322 HRESULT WINAPI
SafeArrayDestroy(SAFEARRAY
*psa
)
1324 TRACE("(%p)\n", psa
);
1327 return E_INVALIDARG
;
1330 return DISP_E_ARRAYISLOCKED
;
1332 /* Native doesn't check to see if the free succeeds */
1333 SafeArrayDestroyData(psa
);
1334 SafeArrayDestroyDescriptor(psa
);
1338 /************************************************************************
1339 * SafeArrayCopy (OLEAUT32.27)
1341 * Make a duplicate of a SafeArray.
1344 * psa [I] Source for copy
1345 * ppsaOut [O] Destination for new copy
1348 * Success: S_OK. ppsaOut contains a copy of the array.
1349 * Failure: An HRESULT error code indicating the error.
1354 HRESULT WINAPI
SafeArrayCopy(SAFEARRAY
*psa
, SAFEARRAY
**ppsaOut
)
1358 TRACE("(%p,%p)\n", psa
, ppsaOut
);
1361 return E_INVALIDARG
;
1366 return S_OK
; /* Handles copying of NULL arrays */
1368 if (psa
->fFeatures
& (FADF_RECORD
|FADF_HAVEIID
|FADF_HAVEVARTYPE
))
1371 if (FAILED(SafeArrayGetVartype(psa
, &vt
)))
1372 hRet
= E_UNEXPECTED
;
1374 hRet
= SafeArrayAllocDescriptorEx(vt
, psa
->cDims
, ppsaOut
);
1378 hRet
= SafeArrayAllocDescriptor(psa
->cDims
, ppsaOut
);
1379 if (SUCCEEDED(hRet
))
1381 (*ppsaOut
)->fFeatures
= psa
->fFeatures
& ~FADF_CREATEVECTOR
;
1382 (*ppsaOut
)->cbElements
= psa
->cbElements
;
1386 if (SUCCEEDED(hRet
))
1388 /* Copy dimension bounds */
1389 memcpy((*ppsaOut
)->rgsabound
, psa
->rgsabound
, psa
->cDims
* sizeof(SAFEARRAYBOUND
));
1391 (*ppsaOut
)->pvData
= SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
);
1393 if ((*ppsaOut
)->pvData
)
1395 hRet
= SAFEARRAY_CopyData(psa
, *ppsaOut
);
1397 if (SUCCEEDED(hRet
))
1400 SAFEARRAY_Free((*ppsaOut
)->pvData
);
1402 SafeArrayDestroyDescriptor(*ppsaOut
);
1408 /************************************************************************
1409 * SafeArrayRedim (OLEAUT32.40)
1411 * Changes the characteristics of the last dimension of a SafeArray
1414 * psa [I] Array to change
1415 * psabound [I] New bound details for the last dimension
1418 * Success: S_OK. psa is updated to reflect the new bounds.
1419 * Failure: An HRESULT error code indicating the error.
1424 HRESULT WINAPI
SafeArrayRedim(SAFEARRAY
*psa
, SAFEARRAYBOUND
*psabound
)
1426 SAFEARRAYBOUND
*oldBounds
;
1428 TRACE("(%p,%p)\n", psa
, psabound
);
1430 if (!psa
|| psa
->fFeatures
& FADF_FIXEDSIZE
|| !psabound
)
1431 return E_INVALIDARG
;
1433 if (psa
->cLocks
> 0)
1434 return DISP_E_ARRAYISLOCKED
;
1436 if (FAILED(SafeArrayLock(psa
)))
1437 return E_UNEXPECTED
;
1439 oldBounds
= &psa
->rgsabound
[psa
->cDims
- 1];
1440 oldBounds
->lLbound
= psabound
->lLbound
;
1442 if (psabound
->cElements
!= oldBounds
->cElements
)
1444 if (psabound
->cElements
< oldBounds
->cElements
)
1446 /* Shorten the final dimension. */
1447 ULONG ulStartCell
= psa
->cDims
== 1 ? 0 : SAFEARRAY_GetDimensionCells(psa
, psa
->cDims
- 1);
1449 ulStartCell
+= psabound
->cElements
;
1450 SAFEARRAY_DestroyData(psa
, ulStartCell
);
1454 /* Lengthen the final dimension */
1455 ULONG ulOldSize
, ulNewSize
;
1458 ulOldSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1460 ulNewSize
= (ulOldSize
/ oldBounds
->cElements
) * psabound
->cElements
;
1462 int oldelems
= oldBounds
->cElements
;
1463 oldBounds
->cElements
= psabound
->cElements
;
1464 ulNewSize
= SAFEARRAY_GetCellCount(psa
) * psa
->cbElements
;
1465 oldBounds
->cElements
= oldelems
;
1468 if (!(pvNewData
= SAFEARRAY_Malloc(ulNewSize
)))
1470 SafeArrayUnlock(psa
);
1471 return E_UNEXPECTED
;
1474 memcpy(pvNewData
, psa
->pvData
, ulOldSize
);
1475 SAFEARRAY_Free(psa
->pvData
);
1476 psa
->pvData
= pvNewData
;
1478 oldBounds
->cElements
= psabound
->cElements
;
1481 SafeArrayUnlock(psa
);
1485 /************************************************************************
1486 * SafeArrayGetVartype (OLEAUT32.77)
1488 * Get the type of the items in a SafeArray.
1491 * psa [I] Array to get the type from
1492 * pvt [O] Destination for the type
1495 * Success: S_OK. pvt contains the type of the items.
1496 * Failure: An HRESULT error code indicating the error.
1501 HRESULT WINAPI
SafeArrayGetVartype(SAFEARRAY
* psa
, VARTYPE
* pvt
)
1503 TRACE("(%p,%p)\n", psa
, pvt
);
1506 return E_INVALIDARG
;
1508 if (psa
->fFeatures
& FADF_RECORD
)
1510 else if (psa
->fFeatures
& FADF_HAVEIID
)
1512 else if (psa
->fFeatures
& FADF_HAVEVARTYPE
)
1514 VARTYPE vt
= SAFEARRAY_GetHiddenDWORD(psa
);
1518 return E_INVALIDARG
;
1523 /************************************************************************
1524 * SafeArraySetRecordInfo (OLEAUT32.@)
1526 * Set the record info for a SafeArray.
1529 * psa [I] Array to set the record info for
1530 * pRinfo [I] Record info
1533 * Success: S_OK. The record info is stored with the array.
1534 * Failure: An HRESULT error code indicating the error.
1539 HRESULT WINAPI
SafeArraySetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
*pRinfo
)
1541 IRecordInfo
** dest
= (IRecordInfo
**)psa
;
1543 TRACE("(%p,%p)\n", psa
, pRinfo
);
1545 if (!psa
|| !(psa
->fFeatures
& FADF_RECORD
))
1546 return E_INVALIDARG
;
1549 IRecordInfo_AddRef(pRinfo
);
1552 IRecordInfo_Release(dest
[-1]);
1558 /************************************************************************
1559 * SafeArrayGetRecordInfo (OLEAUT32.@)
1561 * Get the record info from a SafeArray.
1564 * psa [I] Array to get the record info from
1565 * pRinfo [O] Destination for the record info
1568 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1569 * Failure: An HRESULT error code indicating the error.
1574 HRESULT WINAPI
SafeArrayGetRecordInfo(SAFEARRAY
*psa
, IRecordInfo
**pRinfo
)
1576 IRecordInfo
** src
= (IRecordInfo
**)psa
;
1578 TRACE("(%p,%p)\n", psa
, pRinfo
);
1580 if (!psa
|| !pRinfo
|| !(psa
->fFeatures
& FADF_RECORD
))
1581 return E_INVALIDARG
;
1586 IRecordInfo_AddRef(*pRinfo
);
1590 /************************************************************************
1591 * SafeArraySetIID (OLEAUT32.@)
1593 * Set the IID for a SafeArray.
1596 * psa [I] Array to set the IID from
1600 * Success: S_OK. The IID is stored with the array
1601 * Failure: An HRESULT error code indicating the error.
1606 HRESULT WINAPI
SafeArraySetIID(SAFEARRAY
*psa
, REFGUID guid
)
1608 GUID
* dest
= (GUID
*)psa
;
1610 TRACE("(%p,%s)\n", psa
, debugstr_guid(guid
));
1612 if (!psa
|| !guid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1613 return E_INVALIDARG
;
1619 /************************************************************************
1620 * SafeArrayGetIID (OLEAUT32.@)
1622 * Get the IID from a SafeArray.
1625 * psa [I] Array to get the ID from
1626 * pGuid [O] Destination for the IID
1629 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1630 * Failure: An HRESULT error code indicating the error.
1635 HRESULT WINAPI
SafeArrayGetIID(SAFEARRAY
*psa
, GUID
*pGuid
)
1637 GUID
* src
= (GUID
*)psa
;
1639 TRACE("(%p,%p)\n", psa
, pGuid
);
1641 if (!psa
|| !pGuid
|| !(psa
->fFeatures
& FADF_HAVEIID
))
1642 return E_INVALIDARG
;
1648 /************************************************************************
1649 * VectorFromBstr (OLEAUT32.@)
1651 * Create a SafeArray Vector from the bytes of a BSTR.
1654 * bstr [I] String to get bytes from
1655 * ppsa [O] Destination for the array
1658 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1659 * Failure: An HRESULT error code indicating the error.
1664 HRESULT WINAPI
VectorFromBstr(BSTR bstr
, SAFEARRAY
**ppsa
)
1668 TRACE("(%p,%p)\n", bstr
, ppsa
);
1671 return E_INVALIDARG
;
1674 sab
.cElements
= SysStringByteLen(bstr
);
1676 *ppsa
= SAFEARRAY_Create(VT_UI1
, 1, &sab
, 0);
1680 memcpy((*ppsa
)->pvData
, bstr
, sab
.cElements
);
1683 return E_OUTOFMEMORY
;
1686 /************************************************************************
1687 * BstrFromVector (OLEAUT32.@)
1689 * Create a BSTR from a SafeArray.
1692 * psa [I] Source array
1693 * pbstr [O] Destination for output BSTR
1696 * Success: S_OK. pbstr contains the arrays data.
1697 * Failure: An HRESULT error code indicating the error.
1700 * psa must be a 1 dimensional array of a 1 byte type.
1705 HRESULT WINAPI
BstrFromVector(SAFEARRAY
*psa
, BSTR
*pbstr
)
1707 TRACE("(%p,%p)\n", psa
, pbstr
);
1710 return E_INVALIDARG
;
1714 if (!psa
|| psa
->cbElements
!= 1 || psa
->cDims
!= 1)
1715 return E_INVALIDARG
;
1717 *pbstr
= SysAllocStringByteLen(psa
->pvData
, psa
->rgsabound
[0].cElements
);
1719 return E_OUTOFMEMORY
;