winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / oleaut32 / safearray.c
blob0b769358f65211e86134fd92f26a68ee76abb996
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))
30 * 0x00: SAFEARRAY,
31 * 0x10: SAFEARRAYBOUNDS[0...]
34 #include <string.h>
35 #include <stdarg.h>
36 #include <stdio.h>
38 #define COBJMACROS
40 #include "windef.h"
41 #include "winerror.h"
42 #include "winbase.h"
43 #include "variant.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(variant);
48 /************************************************************************
49 * SafeArray {OLEAUT32}
51 * NOTES
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
66 * single dimension.
68 * DATATYPES
69 * The following types of data can be stored within a SafeArray.
70 * Numeric:
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
73 * Interfaces:
74 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
75 * Other:
76 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
78 * FUNCTIONS
79 * BstrFromVector()
80 * VectorFromBstr()
83 /* Undocumented hidden space before the start of a SafeArray descriptor */
84 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
86 /* features listed here are not propagated to newly created array or data copy
87 created with SafeArrayCopy()/SafeArrayCopyData() */
88 static const USHORT ignored_copy_features =
89 FADF_AUTO |
90 FADF_STATIC |
91 FADF_EMBEDDED |
92 FADF_FIXEDSIZE |
93 FADF_CREATEVECTOR;
95 /* Allocate memory */
96 static inline void* SAFEARRAY_Malloc(ULONG size)
98 void *ret = CoTaskMemAlloc(size);
99 if (ret)
100 memset(ret, 0, size);
101 return ret;
104 /* Free memory */
105 static inline void SAFEARRAY_Free(void *ptr)
107 CoTaskMemFree(ptr);
110 /* Get the size of a supported VT type (0 means unsupported) */
111 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
113 switch (vt)
115 case VT_I1:
116 case VT_UI1: return sizeof(BYTE);
117 case VT_BOOL:
118 case VT_I2:
119 case VT_UI2: return sizeof(SHORT);
120 case VT_I4:
121 case VT_UI4:
122 case VT_R4:
123 case VT_ERROR: return sizeof(LONG);
124 case VT_R8:
125 case VT_I8:
126 case VT_UI8: return sizeof(LONG64);
127 case VT_INT:
128 case VT_UINT: return sizeof(INT);
129 case VT_INT_PTR:
130 case VT_UINT_PTR: return sizeof(UINT_PTR);
131 case VT_CY: return sizeof(CY);
132 case VT_DATE: return sizeof(DATE);
133 case VT_BSTR: return sizeof(BSTR);
134 case VT_DISPATCH: return sizeof(LPDISPATCH);
135 case VT_VARIANT: return sizeof(VARIANT);
136 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
137 case VT_DECIMAL: return sizeof(DECIMAL);
138 /* Note: Return a non-zero size to indicate vt is valid. The actual size
139 * of a UDT is taken from the result of IRecordInfo_GetSize().
141 case VT_RECORD: return 32;
143 return 0;
146 /* Set the hidden data for an array */
147 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
149 /* Implementation data is stored in the 4 bytes before the header */
150 LPDWORD lpDw = (LPDWORD)psa;
151 lpDw[-1] = dw;
154 /* Get the hidden data from an array */
155 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
157 LPDWORD lpDw = (LPDWORD)psa;
158 return lpDw[-1];
161 /* Get the number of cells in a SafeArray */
162 static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
164 const SAFEARRAYBOUND* psab = psa->rgsabound;
165 USHORT cCount = psa->cDims;
166 ULONG ulNumCells = 1;
168 while (cCount--)
170 /* This is a valid bordercase. See testcases. -Marcus */
171 if (!psab->cElements)
172 return 0;
173 ulNumCells *= psab->cElements;
174 psab++;
176 return ulNumCells;
179 /* Allocate a descriptor for an array */
180 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
182 char *ptr = SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE);
184 if (!ptr)
186 *ppsaOut = NULL;
187 return E_OUTOFMEMORY;
190 *ppsaOut = (SAFEARRAY*)(ptr + SAFEARRAY_HIDDEN_SIZE);
191 return S_OK;
194 /* Set the features of an array */
195 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
197 /* Set the IID if we have one, otherwise set the type */
198 if (vt == VT_DISPATCH)
200 psa->fFeatures = FADF_HAVEIID;
201 SafeArraySetIID(psa, &IID_IDispatch);
203 else if (vt == VT_UNKNOWN)
205 psa->fFeatures = FADF_HAVEIID;
206 SafeArraySetIID(psa, &IID_IUnknown);
208 else if (vt == VT_RECORD)
209 psa->fFeatures = FADF_RECORD;
210 else
212 psa->fFeatures = FADF_HAVEVARTYPE;
213 SAFEARRAY_SetHiddenDWORD(psa, vt);
217 /* Create an array */
218 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, const SAFEARRAYBOUND *rgsabound, ULONG ulSize)
220 SAFEARRAY *psa = NULL;
221 unsigned int i;
223 if (!rgsabound)
224 return NULL;
226 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
228 switch (vt)
230 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
231 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
232 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
233 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
236 for (i = 0; i < cDims; i++)
237 memcpy(psa->rgsabound + i, rgsabound + cDims - 1 - i, sizeof(SAFEARRAYBOUND));
239 if (ulSize)
240 psa->cbElements = ulSize;
242 if (!psa->cbElements || FAILED(SafeArrayAllocData(psa)))
244 SafeArrayDestroyDescriptor(psa);
245 psa = NULL;
248 return psa;
251 /* Create an array as a vector */
252 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
254 SAFEARRAY *psa = NULL;
256 if (ulSize || (vt == VT_RECORD))
258 /* Allocate the header and data together */
259 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
261 SAFEARRAY_SetFeatures(vt, psa);
263 psa->cDims = 1;
264 psa->fFeatures |= FADF_CREATEVECTOR;
265 psa->pvData = &psa[1]; /* Data follows the header */
266 psa->cbElements = ulSize;
267 psa->rgsabound[0].cElements = cElements;
268 psa->rgsabound[0].lLbound = lLbound;
270 switch (vt)
272 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
273 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
274 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
275 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
279 return psa;
282 /* Free data items in an array */
283 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
285 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
287 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
289 if (ulStartCell > ulCellCount) {
290 FIXME("unexpected ulCellCount %d, start %d\n",ulCellCount,ulStartCell);
291 return E_UNEXPECTED;
294 ulCellCount -= ulStartCell;
296 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
298 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell;
300 while(ulCellCount--)
302 if (*lpUnknown)
303 IUnknown_Release(*lpUnknown);
304 lpUnknown++;
307 else if (psa->fFeatures & FADF_RECORD)
309 IRecordInfo *lpRecInfo;
311 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
313 PBYTE pRecordData = psa->pvData;
314 while(ulCellCount--)
316 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
317 pRecordData += psa->cbElements;
319 IRecordInfo_Release(lpRecInfo);
322 else if (psa->fFeatures & FADF_BSTR)
324 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell;
326 while(ulCellCount--)
328 SysFreeString(*lpBstr);
329 lpBstr++;
332 else if (psa->fFeatures & FADF_VARIANT)
334 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell;
336 while(ulCellCount--)
338 HRESULT hRet = VariantClear(lpVariant);
340 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
341 lpVariant++;
345 return S_OK;
348 /* Copy data items from one array to another. Destination data is freed before copy. */
349 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
351 HRESULT hr = S_OK;
353 if (!psa->pvData)
354 return S_OK;
356 if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
357 return E_INVALIDARG;
358 else
360 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
362 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) | (psa->fFeatures & ~ignored_copy_features);
364 if (psa->fFeatures & FADF_VARIANT)
366 VARIANT *src_var = psa->pvData;
367 VARIANT *dest_var = dest->pvData;
369 while(ulCellCount--)
371 HRESULT hRet;
373 /* destination is cleared automatically */
374 hRet = VariantCopy(dest_var, src_var);
375 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%08x, element %u\n", hRet, ulCellCount);
376 src_var++;
377 dest_var++;
380 else if (psa->fFeatures & FADF_BSTR)
382 BSTR *src_bstr = psa->pvData;
383 BSTR *dest_bstr = dest->pvData;
385 while(ulCellCount--)
387 SysFreeString(*dest_bstr);
388 if (*src_bstr)
390 *dest_bstr = SysAllocStringByteLen((char*)*src_bstr, SysStringByteLen(*src_bstr));
391 if (!*dest_bstr)
392 return E_OUTOFMEMORY;
394 else
395 *dest_bstr = NULL;
396 src_bstr++;
397 dest_bstr++;
400 else if (psa->fFeatures & FADF_RECORD)
402 BYTE *dest_data = dest->pvData;
403 BYTE *src_data = psa->pvData;
404 IRecordInfo *record;
406 SafeArrayGetRecordInfo(psa, &record);
407 while (ulCellCount--)
409 /* RecordCopy() clears destination record */
410 hr = IRecordInfo_RecordCopy(record, src_data, dest_data);
411 if (FAILED(hr)) break;
412 src_data += psa->cbElements;
413 dest_data += psa->cbElements;
416 SafeArraySetRecordInfo(dest, record);
417 /* This value is set to 32 bytes by default on descriptor creation,
418 update with actual structure size. */
419 dest->cbElements = psa->cbElements;
420 IRecordInfo_Release(record);
422 else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
424 IUnknown **dest_unk = dest->pvData;
425 IUnknown **src_unk = psa->pvData;
427 /* release old iface, addref new one */
428 while (ulCellCount--)
430 if (*dest_unk)
431 IUnknown_Release(*dest_unk);
432 *dest_unk = *src_unk;
433 if (*dest_unk)
434 IUnknown_AddRef(*dest_unk);
435 src_unk++;
436 dest_unk++;
439 else
441 /* Copy the data over */
442 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
445 if (psa->fFeatures & FADF_HAVEIID)
447 GUID guid;
448 SafeArrayGetIID(psa, &guid);
449 SafeArraySetIID(dest, &guid);
451 else if (psa->fFeatures & FADF_HAVEVARTYPE)
453 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
457 return hr;
460 /*************************************************************************
461 * SafeArrayAllocDescriptor (OLEAUT32.36)
463 * Allocate and initialise a descriptor for a SafeArray.
465 * PARAMS
466 * cDims [I] Number of dimensions of the array
467 * ppsaOut [O] Destination for new descriptor
469 * RETURNS
470 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
471 * Failure: An HRESULT error code indicating the error.
473 * NOTES
474 * See SafeArray.
476 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
478 LONG allocSize;
479 HRESULT hr;
481 TRACE("(%d,%p)\n", cDims, ppsaOut);
483 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
484 return E_INVALIDARG;
486 if (!ppsaOut)
487 return E_POINTER;
489 /* We need enough space for the header and its bounds */
490 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
492 hr = SAFEARRAY_AllocDescriptor(allocSize, ppsaOut);
493 if (FAILED(hr))
494 return hr;
496 (*ppsaOut)->cDims = cDims;
498 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims, allocSize);
499 return S_OK;
502 /*************************************************************************
503 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
505 * Allocate and initialise a descriptor for a SafeArray of a given type.
507 * PARAMS
508 * vt [I] The type of items to store in the array
509 * cDims [I] Number of dimensions of the array
510 * ppsaOut [O] Destination for new descriptor
512 * RETURNS
513 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
514 * Failure: An HRESULT error code indicating the error.
516 * NOTES
517 * - This function does not check that vt is an allowed VARTYPE.
518 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
519 * See SafeArray.
521 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
523 ULONG cbElements;
524 HRESULT hRet;
526 TRACE("(%s,%u,%p)\n", debugstr_vt(vt), cDims, ppsaOut);
528 cbElements = SAFEARRAY_GetVTSize(vt);
529 if (!cbElements)
530 WARN("Creating a descriptor with an invalid VARTYPE!\n");
532 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
534 if (SUCCEEDED(hRet))
536 SAFEARRAY_SetFeatures(vt, *ppsaOut);
537 (*ppsaOut)->cbElements = cbElements;
539 return hRet;
542 /*************************************************************************
543 * SafeArrayAllocData (OLEAUT32.37)
545 * Allocate the data area of a SafeArray.
547 * PARAMS
548 * psa [I] SafeArray to allocate the data area of.
550 * RETURNS
551 * Success: S_OK. The data area is allocated and initialised.
552 * Failure: An HRESULT error code indicating the error.
554 * NOTES
555 * See SafeArray.
557 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
559 HRESULT hRet = E_INVALIDARG;
561 TRACE("(%p)\n", psa);
563 if (psa)
565 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
567 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
569 if (psa->pvData)
571 hRet = S_OK;
572 TRACE("%u bytes allocated for data at %p (%u objects).\n",
573 ulSize * psa->cbElements, psa->pvData, ulSize);
575 else
576 hRet = E_OUTOFMEMORY;
578 return hRet;
581 /*************************************************************************
582 * SafeArrayCreate (OLEAUT32.15)
584 * Create a new SafeArray.
586 * PARAMS
587 * vt [I] Type to store in the safe array
588 * cDims [I] Number of array dimensions
589 * rgsabound [I] Bounds of the array dimensions
591 * RETURNS
592 * Success: A pointer to a new array object.
593 * Failure: NULL, if any parameter is invalid or memory allocation fails.
595 * NOTES
596 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
597 * in the Wine implementation.
598 * See SafeArray.
600 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
602 TRACE("(%s,%u,%p)\n", debugstr_vt(vt), cDims, rgsabound);
604 if (vt == VT_RECORD)
605 return NULL;
607 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
610 /*************************************************************************
611 * SafeArrayCreateEx (OLEAUT32.15)
613 * Create a new SafeArray.
615 * PARAMS
616 * vt [I] Type to store in the safe array
617 * cDims [I] Number of array dimensions
618 * rgsabound [I] Bounds of the array dimensions
619 * pvExtra [I] Extra data
621 * RETURNS
622 * Success: A pointer to a new array object.
623 * Failure: NULL, if any parameter is invalid or memory allocation fails.
625 * NOTES
626 * See SafeArray.
628 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
630 ULONG ulSize = 0;
631 IRecordInfo* iRecInfo = pvExtra;
632 SAFEARRAY* psa;
634 TRACE("(%s,%u,%p,%p)\n", debugstr_vt(vt), cDims, rgsabound, pvExtra);
636 if (vt == VT_RECORD)
638 if (!iRecInfo)
639 return NULL;
640 IRecordInfo_GetSize(iRecInfo, &ulSize);
642 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
644 if (pvExtra)
646 switch(vt)
648 case VT_RECORD:
649 SafeArraySetRecordInfo(psa, pvExtra);
650 break;
651 case VT_UNKNOWN:
652 case VT_DISPATCH:
653 SafeArraySetIID(psa, pvExtra);
654 break;
657 return psa;
660 /************************************************************************
661 * SafeArrayCreateVector (OLEAUT32.411)
663 * Create a one dimensional, contiguous SafeArray.
665 * PARAMS
666 * vt [I] Type to store in the safe array
667 * lLbound [I] Lower bound of the array
668 * cElements [I] Number of elements in the array
670 * RETURNS
671 * Success: A pointer to a new array object.
672 * Failure: NULL, if any parameter is invalid or memory allocation fails.
674 * NOTES
675 * See SafeArray.
677 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
679 TRACE("(%s,%d,%u)\n", debugstr_vt(vt), lLbound, cElements);
681 if (vt == VT_RECORD)
682 return NULL;
684 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
687 /************************************************************************
688 * SafeArrayCreateVectorEx (OLEAUT32.411)
690 * Create a one dimensional, contiguous SafeArray.
692 * PARAMS
693 * vt [I] Type to store in the safe array
694 * lLbound [I] Lower bound of the array
695 * cElements [I] Number of elements in the array
696 * pvExtra [I] Extra data
698 * RETURNS
699 * Success: A pointer to a new array object.
700 * Failure: NULL, if any parameter is invalid or memory allocation fails.
702 * NOTES
703 * See SafeArray.
705 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
707 ULONG ulSize;
708 IRecordInfo* iRecInfo = pvExtra;
709 SAFEARRAY* psa;
711 TRACE("(%s,%d,%u,%p)\n", debugstr_vt(vt), lLbound, cElements, pvExtra);
713 if (vt == VT_RECORD)
715 if (!iRecInfo)
716 return NULL;
717 IRecordInfo_GetSize(iRecInfo, &ulSize);
719 else
720 ulSize = SAFEARRAY_GetVTSize(vt);
722 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
724 if (pvExtra)
726 switch(vt)
728 case VT_RECORD:
729 SafeArraySetRecordInfo(psa, iRecInfo);
730 break;
731 case VT_UNKNOWN:
732 case VT_DISPATCH:
733 SafeArraySetIID(psa, pvExtra);
734 break;
737 return psa;
740 /*************************************************************************
741 * SafeArrayDestroyDescriptor (OLEAUT32.38)
743 * Destroy a SafeArray.
745 * PARAMS
746 * psa [I] SafeArray to destroy.
748 * RETURNS
749 * Success: S_OK. The resources used by the array are freed.
750 * Failure: An HRESULT error code indicating the error.
752 * NOTES
753 * See SafeArray.
755 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
757 TRACE("(%p)\n", psa);
759 if (psa)
761 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
763 if (psa->cLocks)
764 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
766 if (psa->fFeatures & FADF_RECORD)
767 SafeArraySetRecordInfo(psa, NULL);
769 if (psa->fFeatures & FADF_CREATEVECTOR &&
770 !(psa->fFeatures & FADF_DATADELETED))
771 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
773 SAFEARRAY_Free(lpv);
775 return S_OK;
778 /*************************************************************************
779 * SafeArrayLock (OLEAUT32.21)
781 * Increment the lock counter of a SafeArray.
783 * PARAMS
784 * psa [O] SafeArray to lock
786 * RETURNS
787 * Success: S_OK. The array lock is incremented.
788 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
789 * are held on the array at once.
791 * NOTES
792 * In Win32 these locks are not thread safe.
793 * See SafeArray.
795 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
797 ULONG ulLocks;
799 TRACE("(%p)\n", psa);
801 if (!psa)
802 return E_INVALIDARG;
804 ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
806 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
808 WARN("Out of locks!\n");
809 InterlockedDecrement( (LONG*) &psa->cLocks);
810 return E_UNEXPECTED;
812 return S_OK;
815 /*************************************************************************
816 * SafeArrayUnlock (OLEAUT32.22)
818 * Decrement the lock counter of a SafeArray.
820 * PARAMS
821 * psa [O] SafeArray to unlock
823 * RETURNS
824 * Success: S_OK. The array lock is decremented.
825 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
826 * held on the array.
828 * NOTES
829 * See SafeArray.
831 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
833 TRACE("(%p)\n", psa);
835 if (!psa)
836 return E_INVALIDARG;
838 if (InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
840 WARN("Unlocked but no lock held!\n");
841 InterlockedIncrement( (LONG*) &psa->cLocks);
842 return E_UNEXPECTED;
844 return S_OK;
847 /*************************************************************************
848 * SafeArrayPutElement (OLEAUT32.26)
850 * Put an item into a SafeArray.
852 * PARAMS
853 * psa [I] SafeArray to insert into
854 * rgIndices [I] Indices to insert at
855 * pvData [I] Data to insert
857 * RETURNS
858 * Success: S_OK. The item is inserted
859 * Failure: An HRESULT error code indicating the error.
861 * NOTES
862 * See SafeArray.
864 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
866 HRESULT hRet;
868 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
870 if (!psa || !rgIndices)
871 return E_INVALIDARG;
873 hRet = SafeArrayLock(psa);
875 if (SUCCEEDED(hRet))
877 PVOID lpvDest;
879 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
881 if (SUCCEEDED(hRet))
883 if (psa->fFeatures & FADF_VARIANT)
885 VARIANT* lpVariant = pvData;
886 VARIANT* lpDest = lpvDest;
888 hRet = VariantCopy(lpDest, lpVariant);
889 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
891 else if (psa->fFeatures & FADF_BSTR)
893 BSTR lpBstr = (BSTR)pvData;
894 BSTR* lpDest = lpvDest;
896 SysFreeString(*lpDest);
898 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
899 if (!*lpDest)
900 hRet = E_OUTOFMEMORY;
902 else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
904 IUnknown *lpUnknown = pvData;
905 IUnknown **lpDest = lpvDest;
907 if (lpUnknown)
908 IUnknown_AddRef(lpUnknown);
909 if (*lpDest)
910 IUnknown_Release(*lpDest);
911 *lpDest = lpUnknown;
913 else if (psa->fFeatures & FADF_RECORD)
915 IRecordInfo *record;
917 SafeArrayGetRecordInfo(psa, &record);
918 hRet = IRecordInfo_RecordCopy(record, pvData, lpvDest);
919 IRecordInfo_Release(record);
920 } else
921 /* Copy the data over */
922 memcpy(lpvDest, pvData, psa->cbElements);
924 SafeArrayUnlock(psa);
926 return hRet;
930 /*************************************************************************
931 * SafeArrayGetElement (OLEAUT32.25)
933 * Get an item from a SafeArray.
935 * PARAMS
936 * psa [I] SafeArray to get from
937 * rgIndices [I] Indices to get from
938 * pvData [O] Destination for data
940 * RETURNS
941 * Success: S_OK. The item data is returned in pvData.
942 * Failure: An HRESULT error code indicating the error.
944 * NOTES
945 * See SafeArray.
947 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
949 HRESULT hRet;
951 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
953 if (!psa || !rgIndices || !pvData)
954 return E_INVALIDARG;
956 hRet = SafeArrayLock(psa);
958 if (SUCCEEDED(hRet))
960 PVOID lpvSrc;
962 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
964 if (SUCCEEDED(hRet))
966 if (psa->fFeatures & FADF_VARIANT)
968 VARIANT* lpVariant = lpvSrc;
969 VARIANT* lpDest = pvData;
971 /* The original content of pvData is ignored. */
972 V_VT(lpDest) = VT_EMPTY;
973 hRet = VariantCopy(lpDest, lpVariant);
974 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
976 else if (psa->fFeatures & FADF_BSTR)
978 BSTR* lpBstr = lpvSrc;
979 BSTR* lpDest = pvData;
981 if (*lpBstr)
983 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
984 if (!*lpBstr)
985 hRet = E_OUTOFMEMORY;
987 else
988 *lpDest = NULL;
990 else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
992 IUnknown **src_unk = lpvSrc;
993 IUnknown **dest_unk = pvData;
995 if (*src_unk)
996 IUnknown_AddRef(*src_unk);
997 *dest_unk = *src_unk;
999 else if (psa->fFeatures & FADF_RECORD)
1001 IRecordInfo *record;
1003 SafeArrayGetRecordInfo(psa, &record);
1004 hRet = IRecordInfo_RecordCopy(record, lpvSrc, pvData);
1005 IRecordInfo_Release(record);
1007 else
1008 /* Copy the data over */
1009 memcpy(pvData, lpvSrc, psa->cbElements);
1011 SafeArrayUnlock(psa);
1013 return hRet;
1016 /*************************************************************************
1017 * SafeArrayGetUBound (OLEAUT32.19)
1019 * Get the upper bound for a given SafeArray dimension
1021 * PARAMS
1022 * psa [I] Array to get dimension upper bound from
1023 * nDim [I] The dimension number to get the upper bound of
1024 * plUbound [O] Destination for the upper bound
1026 * RETURNS
1027 * Success: S_OK. plUbound contains the dimensions upper bound.
1028 * Failure: An HRESULT error code indicating the error.
1030 * NOTES
1031 * See SafeArray.
1033 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
1035 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1037 if (!psa || !plUbound)
1038 return E_INVALIDARG;
1040 if(!nDim || nDim > psa->cDims)
1041 return DISP_E_BADINDEX;
1043 *plUbound = psa->rgsabound[psa->cDims - nDim].lLbound +
1044 psa->rgsabound[psa->cDims - nDim].cElements - 1;
1046 return S_OK;
1049 /*************************************************************************
1050 * SafeArrayGetLBound (OLEAUT32.20)
1052 * Get the lower bound for a given SafeArray dimension
1054 * PARAMS
1055 * psa [I] Array to get dimension lower bound from
1056 * nDim [I] The dimension number to get the lower bound of
1057 * plLbound [O] Destination for the lower bound
1059 * RETURNS
1060 * Success: S_OK. plUbound contains the dimensions lower bound.
1061 * Failure: An HRESULT error code indicating the error.
1063 * NOTES
1064 * See SafeArray.
1066 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1068 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1070 if (!psa || !plLbound)
1071 return E_INVALIDARG;
1073 if(!nDim || nDim > psa->cDims)
1074 return DISP_E_BADINDEX;
1076 *plLbound = psa->rgsabound[psa->cDims - nDim].lLbound;
1077 return S_OK;
1080 /*************************************************************************
1081 * SafeArrayGetDim (OLEAUT32.17)
1083 * Get the number of dimensions in a SafeArray.
1085 * PARAMS
1086 * psa [I] Array to get the dimensions of
1088 * RETURNS
1089 * The number of array dimensions in psa, or 0 if psa is NULL.
1091 * NOTES
1092 * See SafeArray.
1094 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1096 TRACE("(%p) returning %d\n", psa, psa ? psa->cDims : 0u);
1097 return psa ? psa->cDims : 0;
1100 /*************************************************************************
1101 * SafeArrayGetElemsize (OLEAUT32.18)
1103 * Get the size of an element in a SafeArray.
1105 * PARAMS
1106 * psa [I] Array to get the element size from
1108 * RETURNS
1109 * The size of a single element in psa, or 0 if psa is NULL.
1111 * NOTES
1112 * See SafeArray.
1114 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1116 TRACE("(%p) returning %d\n", psa, psa ? psa->cbElements : 0u);
1117 return psa ? psa->cbElements : 0;
1120 /*************************************************************************
1121 * SafeArrayAccessData (OLEAUT32.23)
1123 * Lock a SafeArray and return a pointer to its data.
1125 * PARAMS
1126 * psa [I] Array to get the data pointer from
1127 * ppvData [O] Destination for the arrays data pointer
1129 * RETURNS
1130 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1131 * is locked.
1132 * Failure: An HRESULT error code indicating the error.
1134 * NOTES
1135 * See SafeArray.
1137 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1139 HRESULT hr;
1141 TRACE("(%p,%p)\n", psa, ppvData);
1143 if(!psa || !ppvData)
1144 return E_INVALIDARG;
1146 hr = SafeArrayLock(psa);
1147 *ppvData = SUCCEEDED(hr) ? psa->pvData : NULL;
1149 return hr;
1153 /*************************************************************************
1154 * SafeArrayUnaccessData (OLEAUT32.24)
1156 * Unlock a SafeArray after accessing its data.
1158 * PARAMS
1159 * psa [I] Array to unlock
1161 * RETURNS
1162 * Success: S_OK. The array is unlocked.
1163 * Failure: An HRESULT error code indicating the error.
1165 * NOTES
1166 * See SafeArray.
1168 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1170 TRACE("(%p)\n", psa);
1171 return SafeArrayUnlock(psa);
1174 /************************************************************************
1175 * SafeArrayPtrOfIndex (OLEAUT32.148)
1177 * Get the address of an item in a SafeArray.
1179 * PARAMS
1180 * psa [I] Array to get the items address from
1181 * rgIndices [I] Index of the item in the array
1182 * ppvData [O] Destination for item address
1184 * RETURNS
1185 * Success: S_OK. ppvData contains a pointer to the item.
1186 * Failure: An HRESULT error code indicating the error.
1188 * NOTES
1189 * This function does not lock the array.
1191 * NOTES
1192 * See SafeArray.
1194 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1196 USHORT dim;
1197 ULONG cell = 0, dimensionSize = 1;
1198 SAFEARRAYBOUND* psab;
1199 LONG c1;
1201 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1203 /* The general formula for locating the cell number of an entry in an n
1204 * dimensional array (where cn = coordinate in dimension dn) is:
1206 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1208 * We calculate the size of the last dimension at each step through the
1209 * dimensions to avoid recursing to calculate the last dimensions size.
1211 if (!psa || !rgIndices || !ppvData)
1212 return E_INVALIDARG;
1214 psab = psa->rgsabound + psa->cDims - 1;
1215 c1 = *rgIndices++;
1217 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1218 return DISP_E_BADINDEX; /* Initial index out of bounds */
1220 for (dim = 1; dim < psa->cDims; dim++)
1222 dimensionSize *= psab->cElements;
1224 psab--;
1226 if (!psab->cElements ||
1227 *rgIndices < psab->lLbound ||
1228 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1229 return DISP_E_BADINDEX; /* Index out of bounds */
1231 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1232 rgIndices++;
1235 cell += (c1 - psa->rgsabound[psa->cDims - 1].lLbound);
1237 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1238 return S_OK;
1241 /************************************************************************
1242 * SafeArrayDestroyData (OLEAUT32.39)
1244 * Destroy the data associated with a SafeArray.
1246 * PARAMS
1247 * psa [I] Array to delete the data from
1249 * RETURNS
1250 * Success: S_OK. All items and the item data are freed.
1251 * Failure: An HRESULT error code indicating the error.
1253 * NOTES
1254 * See SafeArray.
1256 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1258 HRESULT hr;
1260 TRACE("(%p)\n", psa);
1262 if (!psa)
1263 return E_INVALIDARG;
1265 if (psa->cLocks)
1266 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1268 /* Delete the actual item data */
1269 hr = SAFEARRAY_DestroyData(psa, 0);
1270 if (FAILED(hr))
1271 return hr;
1273 if (psa->pvData)
1275 if (psa->fFeatures & FADF_STATIC)
1277 ZeroMemory(psa->pvData, SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1278 return S_OK;
1280 /* If this is not a vector, free the data memory block */
1281 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1283 SAFEARRAY_Free(psa->pvData);
1284 psa->pvData = NULL;
1286 else
1287 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1290 return S_OK;
1293 /************************************************************************
1294 * SafeArrayCopyData (OLEAUT32.412)
1296 * Copy all data from one SafeArray to another.
1298 * PARAMS
1299 * psaSource [I] Source for copy
1300 * psaTarget [O] Destination for copy
1302 * RETURNS
1303 * Success: S_OK. psaTarget contains a copy of psaSource.
1304 * Failure: An HRESULT error code indicating the error.
1306 * NOTES
1307 * The two arrays must have the same number of dimensions and elements.
1309 * NOTES
1310 * See SafeArray.
1312 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1314 int dim;
1316 TRACE("(%p,%p)\n", psaSource, psaTarget);
1318 if (!psaSource || !psaTarget ||
1319 psaSource->cDims != psaTarget->cDims ||
1320 psaSource->cbElements != psaTarget->cbElements)
1321 return E_INVALIDARG;
1323 /* Each dimension must be the same size */
1324 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1325 if (psaSource->rgsabound[dim].cElements !=
1326 psaTarget->rgsabound[dim].cElements)
1327 return E_INVALIDARG;
1329 return SAFEARRAY_CopyData(psaSource, psaTarget);
1332 /************************************************************************
1333 * SafeArrayDestroy (OLEAUT32.16)
1335 * Destroy a SafeArray.
1337 * PARAMS
1338 * psa [I] Array to destroy
1340 * RETURNS
1341 * Success: S_OK. All resources used by the array are freed.
1342 * Failure: An HRESULT error code indicating the error.
1344 * NOTES
1345 * See SafeArray.
1347 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1349 TRACE("(%p)\n", psa);
1351 if(!psa)
1352 return S_OK;
1354 if(psa->cLocks > 0)
1355 return DISP_E_ARRAYISLOCKED;
1357 /* Native doesn't check to see if the free succeeds */
1358 SafeArrayDestroyData(psa);
1359 SafeArrayDestroyDescriptor(psa);
1360 return S_OK;
1363 /************************************************************************
1364 * SafeArrayCopy (OLEAUT32.27)
1366 * Make a duplicate of a SafeArray.
1368 * PARAMS
1369 * psa [I] Source for copy
1370 * ppsaOut [O] Destination for new copy
1372 * RETURNS
1373 * Success: S_OK. ppsaOut contains a copy of the array.
1374 * Failure: An HRESULT error code indicating the error.
1376 * NOTES
1377 * See SafeArray.
1379 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1381 HRESULT hRet;
1383 TRACE("(%p,%p)\n", psa, ppsaOut);
1385 if (!ppsaOut)
1386 return E_INVALIDARG;
1388 *ppsaOut = NULL;
1390 if (!psa)
1391 return S_OK; /* Handles copying of NULL arrays */
1393 if (!psa->cbElements)
1394 return E_INVALIDARG;
1396 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1398 VARTYPE vt;
1400 hRet = SafeArrayGetVartype(psa, &vt);
1401 if (SUCCEEDED(hRet))
1402 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1404 else
1406 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1407 if (SUCCEEDED(hRet))
1409 (*ppsaOut)->fFeatures = psa->fFeatures & ~ignored_copy_features;
1410 (*ppsaOut)->cbElements = psa->cbElements;
1414 if (SUCCEEDED(hRet))
1416 /* Copy dimension bounds */
1417 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1419 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1420 if (!(*ppsaOut)->pvData)
1422 SafeArrayDestroyDescriptor(*ppsaOut);
1423 *ppsaOut = NULL;
1424 return E_OUTOFMEMORY;
1427 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1428 if (FAILED(hRet))
1430 SAFEARRAY_Free((*ppsaOut)->pvData);
1431 SafeArrayDestroyDescriptor(*ppsaOut);
1432 *ppsaOut = NULL;
1433 return hRet;
1437 return hRet;
1440 /************************************************************************
1441 * SafeArrayRedim (OLEAUT32.40)
1443 * Changes the characteristics of the last dimension of a SafeArray
1445 * PARAMS
1446 * psa [I] Array to change
1447 * psabound [I] New bound details for the last dimension
1449 * RETURNS
1450 * Success: S_OK. psa is updated to reflect the new bounds.
1451 * Failure: An HRESULT error code indicating the error.
1453 * NOTES
1454 * See SafeArray.
1456 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1458 SAFEARRAYBOUND *oldBounds;
1459 HRESULT hr;
1461 TRACE("(%p,%p)\n", psa, psabound);
1463 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1464 return E_INVALIDARG;
1466 if (psa->cLocks > 0)
1467 return DISP_E_ARRAYISLOCKED;
1469 hr = SafeArrayLock(psa);
1470 if (FAILED(hr))
1471 return hr;
1473 oldBounds = psa->rgsabound;
1474 oldBounds->lLbound = psabound->lLbound;
1476 if (psabound->cElements != oldBounds->cElements)
1478 if (psabound->cElements < oldBounds->cElements)
1480 /* Shorten the final dimension. */
1481 ULONG ulStartCell = psabound->cElements *
1482 (SAFEARRAY_GetCellCount(psa) / oldBounds->cElements);
1483 SAFEARRAY_DestroyData(psa, ulStartCell);
1485 else
1487 /* Lengthen the final dimension */
1488 ULONG ulOldSize, ulNewSize;
1489 PVOID pvNewData;
1491 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1492 if (ulOldSize)
1493 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1494 else {
1495 int oldelems = oldBounds->cElements;
1496 oldBounds->cElements = psabound->cElements;
1497 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1498 oldBounds->cElements = oldelems;
1501 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1503 SafeArrayUnlock(psa);
1504 return E_OUTOFMEMORY;
1507 memcpy(pvNewData, psa->pvData, ulOldSize);
1508 SAFEARRAY_Free(psa->pvData);
1509 psa->pvData = pvNewData;
1511 oldBounds->cElements = psabound->cElements;
1514 SafeArrayUnlock(psa);
1515 return S_OK;
1518 /************************************************************************
1519 * SafeArrayGetVartype (OLEAUT32.77)
1521 * Get the type of the items in a SafeArray.
1523 * PARAMS
1524 * psa [I] Array to get the type from
1525 * pvt [O] Destination for the type
1527 * RETURNS
1528 * Success: S_OK. pvt contains the type of the items.
1529 * Failure: An HRESULT error code indicating the error.
1531 * NOTES
1532 * See SafeArray.
1534 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1536 TRACE("(%p,%p)\n", psa, pvt);
1538 if (!psa || !pvt)
1539 return E_INVALIDARG;
1541 if (psa->fFeatures & FADF_RECORD)
1542 *pvt = VT_RECORD;
1543 else if ((psa->fFeatures & (FADF_HAVEIID|FADF_DISPATCH)) == (FADF_HAVEIID|FADF_DISPATCH))
1544 *pvt = VT_DISPATCH;
1545 else if (psa->fFeatures & FADF_HAVEIID)
1546 *pvt = VT_UNKNOWN;
1547 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1549 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1550 *pvt = vt;
1552 else
1553 return E_INVALIDARG;
1555 return S_OK;
1558 /************************************************************************
1559 * SafeArraySetRecordInfo (OLEAUT32.@)
1561 * Set the record info for a SafeArray.
1563 * PARAMS
1564 * psa [I] Array to set the record info for
1565 * pRinfo [I] Record info
1567 * RETURNS
1568 * Success: S_OK. The record info is stored with the array.
1569 * Failure: An HRESULT error code indicating the error.
1571 * NOTES
1572 * See SafeArray.
1574 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1576 IRecordInfo** dest = (IRecordInfo**)psa;
1578 TRACE("(%p,%p)\n", psa, pRinfo);
1580 if (!psa || !(psa->fFeatures & FADF_RECORD))
1581 return E_INVALIDARG;
1583 if (pRinfo)
1584 IRecordInfo_AddRef(pRinfo);
1586 if (dest[-1])
1587 IRecordInfo_Release(dest[-1]);
1589 dest[-1] = pRinfo;
1590 return S_OK;
1593 /************************************************************************
1594 * SafeArrayGetRecordInfo (OLEAUT32.@)
1596 * Get the record info from a SafeArray.
1598 * PARAMS
1599 * psa [I] Array to get the record info from
1600 * pRinfo [O] Destination for the record info
1602 * RETURNS
1603 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1604 * Failure: An HRESULT error code indicating the error.
1606 * NOTES
1607 * See SafeArray.
1609 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1611 IRecordInfo** src = (IRecordInfo**)psa;
1613 TRACE("(%p,%p)\n", psa, pRinfo);
1615 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1616 return E_INVALIDARG;
1618 *pRinfo = src[-1];
1620 if (*pRinfo)
1621 IRecordInfo_AddRef(*pRinfo);
1622 return S_OK;
1625 /************************************************************************
1626 * SafeArraySetIID (OLEAUT32.@)
1628 * Set the IID for a SafeArray.
1630 * PARAMS
1631 * psa [I] Array to set the IID from
1632 * guid [I] IID
1634 * RETURNS
1635 * Success: S_OK. The IID is stored with the array
1636 * Failure: An HRESULT error code indicating the error.
1638 * NOTES
1639 * See SafeArray.
1641 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1643 GUID* dest = (GUID*)psa;
1645 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1647 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1648 return E_INVALIDARG;
1650 dest[-1] = *guid;
1651 return S_OK;
1654 /************************************************************************
1655 * SafeArrayGetIID (OLEAUT32.@)
1657 * Get the IID from a SafeArray.
1659 * PARAMS
1660 * psa [I] Array to get the ID from
1661 * pGuid [O] Destination for the IID
1663 * RETURNS
1664 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1665 * Failure: An HRESULT error code indicating the error.
1667 * NOTES
1668 * See SafeArray.
1670 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1672 GUID* src = (GUID*)psa;
1674 TRACE("(%p,%p)\n", psa, pGuid);
1676 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1677 return E_INVALIDARG;
1679 *pGuid = src[-1];
1680 return S_OK;
1683 /************************************************************************
1684 * VectorFromBstr (OLEAUT32.@)
1686 * Create a SafeArray Vector from the bytes of a BSTR.
1688 * PARAMS
1689 * bstr [I] String to get bytes from
1690 * ppsa [O] Destination for the array
1692 * RETURNS
1693 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1694 * Failure: An HRESULT error code indicating the error.
1696 * NOTES
1697 * See SafeArray.
1699 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1701 SAFEARRAYBOUND sab;
1703 TRACE("(%p,%p)\n", bstr, ppsa);
1705 if (!ppsa)
1706 return E_INVALIDARG;
1708 sab.lLbound = 0;
1709 sab.cElements = SysStringByteLen(bstr);
1711 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1713 if (*ppsa)
1715 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1716 return S_OK;
1718 return E_OUTOFMEMORY;
1721 /************************************************************************
1722 * BstrFromVector (OLEAUT32.@)
1724 * Create a BSTR from a SafeArray.
1726 * PARAMS
1727 * psa [I] Source array
1728 * pbstr [O] Destination for output BSTR
1730 * RETURNS
1731 * Success: S_OK. pbstr contains the arrays data.
1732 * Failure: An HRESULT error code indicating the error.
1734 * NOTES
1735 * psa must be a 1 dimensional array of a 1 byte type.
1737 * NOTES
1738 * See SafeArray.
1740 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1742 TRACE("(%p,%p)\n", psa, pbstr);
1744 if (!pbstr)
1745 return E_INVALIDARG;
1747 *pbstr = NULL;
1749 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1750 return E_INVALIDARG;
1752 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1753 if (!*pbstr)
1754 return E_OUTOFMEMORY;
1755 return S_OK;