Review and fix regular expressions of the form /^foo|bar$/.
[wine/wine-gecko.git] / dlls / oleaut32 / safearray.c
blob1aaedb4704fb232d9e26c89f71338d1f4f3bffe2
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))
30 * 0x00: SAFEARRAY,
31 * 0x10: SAFEARRAYBOUNDS[0...]
34 #include "config.h"
36 #include <string.h>
37 #include <stdarg.h>
38 #include <stdio.h>
40 #define COBJMACROS
42 #include "windef.h"
43 #include "winerror.h"
44 #include "winbase.h"
45 #include "variant.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(variant);
50 /************************************************************************
51 * SafeArray {OLEAUT32}
53 * NOTES
54 * The SafeArray data type provides the underlying interface for Ole
55 * Automations arrays, used for example to represent array types in
56 * Visual Basic(tm) and to gather user defined parameters for invocation through
57 * an IDispatch interface.
59 * Safe arrays provide bounds checking and automatically manage the data
60 * types they contain, for example handing reference counting and copying
61 * of interface pointers. User defined types can be stored in arrays
62 * using the IRecordInfo interface.
64 * There are two types of SafeArray, normal and vectors. Normal arrays can have
65 * multiple dimensions and the data for the array is allocated separately from
66 * the array header. This is the most flexible type of array. Vectors, on the
67 * other hand, are fixed in size and consist of a single allocated block, and a
68 * single dimension.
70 * DATATYPES
71 * The following types of data can be stored within a SafeArray.
72 * Numeric:
73 *| VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8, VT_UI8, VT_INT, VT_UINT,
74 *| VT_R4, VT_R8, VT_CY, VT_DECIMAL
75 * Interfaces:
76 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
77 * Other:
78 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
80 * FUNCTIONS
81 * BstrFromVector()
82 * VectorFromBstr()
85 /* Undocumented hidden space before the start of a SafeArray descriptor */
86 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
88 /* Allocate memory */
89 static inline LPVOID SAFEARRAY_Malloc(ULONG ulSize)
91 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
92 * instance returned from CoGetMalloc().
94 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulSize);
97 /* Free memory */
98 static inline BOOL SAFEARRAY_Free(LPVOID lpData)
100 return HeapFree(GetProcessHeap(), 0, lpData);
103 /* Get the size of a supported VT type (0 means unsupported) */
104 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
106 switch (vt)
108 case VT_I1:
109 case VT_UI1: return sizeof(BYTE);
110 case VT_BOOL:
111 case VT_I2:
112 case VT_UI2: return sizeof(SHORT);
113 case VT_I4:
114 case VT_UI4:
115 case VT_R4:
116 case VT_ERROR: return sizeof(LONG);
117 case VT_R8:
118 case VT_I8:
119 case VT_UI8: return sizeof(LONG64);
120 case VT_INT:
121 case VT_UINT: return sizeof(INT);
122 case VT_INT_PTR:
123 case VT_UINT_PTR: return sizeof(UINT_PTR);
124 case VT_CY: return sizeof(CY);
125 case VT_DATE: return sizeof(DATE);
126 case VT_BSTR: return sizeof(BSTR);
127 case VT_DISPATCH: return sizeof(LPDISPATCH);
128 case VT_VARIANT: return sizeof(VARIANT);
129 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
130 case VT_DECIMAL: return sizeof(DECIMAL);
131 /* Note: Return a non-zero size to indicate vt is valid. The actual size
132 * of a UDT is taken from the result of IRecordInfo_GetSize().
134 case VT_RECORD: return 32;
136 return 0;
139 /* Set the hidden data for an array */
140 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
142 /* Implementation data is stored in the 4 bytes before the header */
143 LPDWORD lpDw = (LPDWORD)psa;
144 lpDw[-1] = dw;
147 /* Get the hidden data from an array */
148 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
150 LPDWORD lpDw = (LPDWORD)psa;
151 return lpDw[-1];
154 /* Get the number of cells in a SafeArray */
155 static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
157 SAFEARRAYBOUND* psab = psa->rgsabound;
158 USHORT cCount = psa->cDims;
159 ULONG ulNumCells = 1;
161 while (cCount--)
163 /* This is a valid bordercase. See testcases. -Marcus */
164 if (!psab->cElements)
165 return 0;
166 ulNumCells *= psab->cElements;
167 psab++;
169 return ulNumCells;
172 /* Get the 0 based index of an index into a dimension */
173 static inline ULONG SAFEARRAY_GetDimensionIndex(SAFEARRAYBOUND *psab, ULONG ulIndex)
175 return ulIndex - psab->lLbound;
178 /* Get the size of a dimension in cells */
179 static inline ULONG SAFEARRAY_GetDimensionCells(SAFEARRAY *psa, ULONG ulDim)
181 ULONG size = psa->rgsabound[0].cElements;
183 while (ulDim)
185 size *= psa->rgsabound[ulDim].cElements;
186 ulDim--;
188 return size;
191 /* Allocate a descriptor for an array */
192 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
194 *ppsaOut = (SAFEARRAY*)((char*)SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE) + SAFEARRAY_HIDDEN_SIZE);
196 if (!*ppsaOut)
197 return E_UNEXPECTED;
199 return S_OK;
202 /* Set the features of an array */
203 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
205 /* Set the IID if we have one, otherwise set the type */
206 if (vt == VT_DISPATCH)
208 psa->fFeatures = FADF_HAVEIID;
209 SafeArraySetIID(psa, &IID_IDispatch);
211 else if (vt == VT_UNKNOWN)
213 psa->fFeatures = FADF_HAVEIID;
214 SafeArraySetIID(psa, &IID_IUnknown);
216 else if (vt == VT_RECORD)
217 psa->fFeatures = FADF_RECORD;
218 else
220 psa->fFeatures = FADF_HAVEVARTYPE;
221 SAFEARRAY_SetHiddenDWORD(psa, vt);
225 /* Create an array */
226 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, ULONG ulSize)
228 SAFEARRAY *psa = NULL;
230 if (!rgsabound)
231 return NULL;
233 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
235 switch (vt)
237 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
238 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
239 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
240 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
243 memcpy(psa->rgsabound, rgsabound, cDims * sizeof(SAFEARRAYBOUND));
245 if (ulSize)
246 psa->cbElements = ulSize;
248 if (FAILED(SafeArrayAllocData(psa)))
250 SafeArrayDestroyDescriptor(psa);
251 psa = NULL;
254 return psa;
257 /* Create an array as a vector */
258 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
260 SAFEARRAY *psa = NULL;
262 if (ulSize || (vt == VT_RECORD))
264 /* Allocate the header and data together */
265 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
267 SAFEARRAY_SetFeatures(vt, psa);
269 psa->cDims = 1;
270 psa->fFeatures |= FADF_CREATEVECTOR;
271 psa->pvData = &psa[1]; /* Data follows the header */
272 psa->cbElements = ulSize;
273 psa->rgsabound[0].cElements = cElements;
274 psa->rgsabound[0].lLbound = lLbound;
276 switch (vt)
278 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
279 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
280 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
281 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
285 return psa;
288 /* Free data items in an array */
289 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
291 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
293 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
295 if (ulStartCell > ulCellCount) {
296 FIXME("unexpted ulcellcount %ld, start %ld\n",ulCellCount,ulStartCell);
297 return E_UNEXPECTED;
300 ulCellCount -= ulStartCell;
302 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
304 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell * psa->cbElements;
306 while(ulCellCount--)
308 if (*lpUnknown)
309 IUnknown_Release(*lpUnknown);
310 lpUnknown++;
313 else if (psa->fFeatures & (FADF_RECORD))
315 IRecordInfo *lpRecInfo;
317 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
319 PBYTE pRecordData = (PBYTE)psa->pvData;
320 while(ulCellCount--)
322 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
323 pRecordData += psa->cbElements;
325 IRecordInfo_Release(lpRecInfo);
328 else if (psa->fFeatures & FADF_BSTR)
330 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell * psa->cbElements;
332 while(ulCellCount--)
334 if (*lpBstr)
335 SysFreeString(*lpBstr);
336 lpBstr++;
339 else if (psa->fFeatures & FADF_VARIANT)
341 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell * psa->cbElements;
343 while(ulCellCount--)
345 HRESULT hRet = VariantClear(lpVariant);
347 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
348 lpVariant++;
352 return S_OK;
355 /* Copy data items from one array to another */
356 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
358 if (!psa->pvData || !dest->pvData || psa->fFeatures & FADF_DATADELETED)
359 return E_INVALIDARG;
360 else
362 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
364 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
365 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
367 if (psa->fFeatures & FADF_VARIANT)
369 VARIANT* lpVariant = (VARIANT*)psa->pvData;
370 VARIANT* lpDest = (VARIANT*)dest->pvData;
372 while(ulCellCount--)
374 HRESULT hRet;
376 hRet = VariantCopy(lpDest, lpVariant);
377 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%lx\n", hRet);
378 lpVariant++;
379 lpDest++;
382 else if (psa->fFeatures & FADF_BSTR)
384 BSTR* lpBstr = (BSTR*)psa->pvData;
385 BSTR* lpDest = (BSTR*)dest->pvData;
387 while(ulCellCount--)
389 if (*lpBstr)
391 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
392 if (!*lpDest)
393 return E_OUTOFMEMORY;
395 else
396 *lpDest = NULL;
397 lpBstr++;
398 lpDest++;
401 else
403 /* Copy the data over */
404 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
406 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
408 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)dest->pvData;
410 while(ulCellCount--)
412 if (*lpUnknown)
413 IUnknown_AddRef(*lpUnknown);
414 lpUnknown++;
419 if (psa->fFeatures & FADF_RECORD)
421 IRecordInfo* pRecInfo = NULL;
423 SafeArrayGetRecordInfo(psa, &pRecInfo);
424 SafeArraySetRecordInfo(dest, pRecInfo);
426 if (pRecInfo)
428 /* Release because Get() adds a reference */
429 IRecordInfo_Release(pRecInfo);
432 else if (psa->fFeatures & FADF_HAVEIID)
434 GUID guid;
435 SafeArrayGetIID(psa, &guid);
436 SafeArraySetIID(dest, &guid);
438 else if (psa->fFeatures & FADF_HAVEVARTYPE)
440 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
443 return S_OK;
446 /*************************************************************************
447 * SafeArrayAllocDescriptor (OLEAUT32.36)
449 * Allocate and initialise a descriptor for a SafeArray.
451 * PARAMS
452 * cDims [I] Number of dimensions of the array
453 * ppsaOut [O] Destination for new descriptor
455 * RETURNS
456 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
457 * Failure: An HRESULT error code indicating the error.
459 * NOTES
460 * See SafeArray.
462 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
464 LONG allocSize;
466 TRACE("(%d,%p)\n", cDims, ppsaOut);
468 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
469 return E_INVALIDARG;
471 if (!ppsaOut)
472 return E_POINTER;
474 /* We need enough space for the header and its bounds */
475 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
477 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
478 return E_UNEXPECTED;
480 (*ppsaOut)->cDims = cDims;
482 TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims, allocSize);
483 return S_OK;
486 /*************************************************************************
487 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
489 * Allocate and initialise a descriptor for a SafeArray of a given type.
491 * PARAMS
492 * vt [I] The type of items to store in the array
493 * cDims [I] Number of dimensions of the array
494 * ppsaOut [O] Destination for new descriptor
496 * RETURNS
497 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
498 * Failure: An HRESULT error code indicating the error.
500 * NOTES
501 * - This function does not chack that vt is an allowed VARTYPE.
502 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
503 * See SafeArray.
505 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
507 ULONG cbElements;
508 HRESULT hRet = E_UNEXPECTED;
510 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
512 cbElements = SAFEARRAY_GetVTSize(vt);
513 if (!cbElements)
514 WARN("Creating a descriptor with an invalid VARTYPE!\n");
516 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
518 if (SUCCEEDED(hRet))
520 SAFEARRAY_SetFeatures(vt, *ppsaOut);
521 (*ppsaOut)->cbElements = cbElements;
523 return hRet;
526 /*************************************************************************
527 * SafeArrayAllocData (OLEAUT32.37)
529 * Allocate the data area of a SafeArray.
531 * PARAMS
532 * psa [I] SafeArray to allocate the data area of.
534 * RETURNS
535 * Success: S_OK. The data area is allocated and initialised.
536 * Failure: An HRESULT error code indicating the error.
538 * NOTES
539 * See SafeArray.
541 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
543 HRESULT hRet = E_INVALIDARG;
545 TRACE("(%p)\n", psa);
547 if (psa)
549 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
551 hRet = E_OUTOFMEMORY;
553 if (psa->cbElements)
555 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
557 if (psa->pvData)
559 hRet = S_OK;
560 TRACE("%lu bytes allocated for data at %p (%lu objects).\n",
561 ulSize * psa->cbElements, psa->pvData, ulSize);
565 return hRet;
568 /*************************************************************************
569 * SafeArrayCreate (OLEAUT32.15)
571 * Create a new SafeArray.
573 * PARAMS
574 * vt [I] Type to store in the safe array
575 * cDims [I] Number of array dimensions
576 * rgsabound [I] Bounds of the array dimensions
578 * RETURNS
579 * Success: A pointer to a new array object.
580 * Failure: NULL, if any parameter is invalid or memory allocation fails.
582 * NOTES
583 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
584 * in the Wine implementation.
585 * See SafeArray.
587 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
589 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
591 if (vt == VT_RECORD)
592 return NULL;
594 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
597 /*************************************************************************
598 * SafeArrayCreateEx (OLEAUT32.15)
600 * Create a new SafeArray.
602 * PARAMS
603 * vt [I] Type to store in the safe array
604 * cDims [I] Number of array dimensions
605 * rgsabound [I] Bounds of the array dimensions
606 * pvExtra [I] Extra data
608 * RETURNS
609 * Success: A pointer to a new array object.
610 * Failure: NULL, if any parameter is invalid or memory allocation fails.
612 * NOTES
613 * See SafeArray.
615 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
617 ULONG ulSize = 0;
618 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
619 SAFEARRAY* psa;
621 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
623 if (vt == VT_RECORD)
625 if (!iRecInfo)
626 return NULL;
627 IRecordInfo_GetSize(iRecInfo, &ulSize);
629 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
631 if (pvExtra)
633 switch(vt)
635 case VT_RECORD:
636 SafeArraySetRecordInfo(psa, pvExtra);
637 break;
638 case VT_UNKNOWN:
639 case VT_DISPATCH:
640 SafeArraySetIID(psa, pvExtra);
641 break;
644 return psa;
647 /************************************************************************
648 * SafeArrayCreateVector (OLEAUT32.411)
650 * Create a one dimensional, contigous SafeArray.
652 * PARAMS
653 * vt [I] Type to store in the safe array
654 * lLbound [I] Lower bound of the array
655 * cElements [I] Number of elements in the array
657 * RETURNS
658 * Success: A pointer to a new array object.
659 * Failure: NULL, if any parameter is invalid or memory allocation fails.
661 * NOTES
662 * See SafeArray.
664 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
666 TRACE("(%d->%s,%ld,%ld\n", vt, debugstr_vt(vt), lLbound, cElements);
668 if (vt == VT_RECORD)
669 return NULL;
671 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
674 /************************************************************************
675 * SafeArrayCreateVectorEx (OLEAUT32.411)
677 * Create a one dimensional, contigous SafeArray.
679 * PARAMS
680 * vt [I] Type to store in the safe array
681 * lLbound [I] Lower bound of the array
682 * cElements [I] Number of elements in the array
683 * pvExtra [I] Extra data
685 * RETURNS
686 * Success: A pointer to a new array object.
687 * Failure: NULL, if any parameter is invalid or memory allocation fails.
689 * NOTES
690 * See SafeArray.
692 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
694 ULONG ulSize;
695 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
696 SAFEARRAY* psa;
698 TRACE("(%d->%s,%ld,%ld,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
700 if (vt == VT_RECORD)
702 if (!iRecInfo)
703 return NULL;
704 IRecordInfo_GetSize(iRecInfo, &ulSize);
706 else
707 ulSize = SAFEARRAY_GetVTSize(vt);
709 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
711 if (pvExtra)
713 switch(vt)
715 case VT_RECORD:
716 SafeArraySetRecordInfo(psa, iRecInfo);
717 break;
718 case VT_UNKNOWN:
719 case VT_DISPATCH:
720 SafeArraySetIID(psa, pvExtra);
721 break;
724 return psa;
727 /*************************************************************************
728 * SafeArrayDestroyDescriptor (OLEAUT32.38)
730 * Destroy a SafeArray.
732 * PARAMS
733 * psa [I] SafeArray to destroy.
735 * RETURNS
736 * Success: S_OK. The resources used by the array are freed.
737 * Failure: An HRESULT error code indicating the error.
739 * NOTES
740 * See SafeArray.
742 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
744 TRACE("(%p)\n", psa);
746 if (psa)
748 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
750 if (psa->cLocks)
751 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
753 if (psa->fFeatures & FADF_RECORD)
754 SafeArraySetRecordInfo(psa, NULL);
756 if (psa->fFeatures & FADF_CREATEVECTOR &&
757 !(psa->fFeatures & FADF_DATADELETED))
758 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
760 if (!SAFEARRAY_Free(lpv))
761 return E_UNEXPECTED;
763 return S_OK;
766 /*************************************************************************
767 * SafeArrayLock (OLEAUT32.21)
769 * Increment the lock counter of a SafeArray.
771 * PARAMS
772 * psa [O] SafeArray to lock
774 * RETURNS
775 * Success: S_OK. The array lock is incremented.
776 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
777 * are held on the array at once.
779 * NOTES
780 * In Win32 these locks are not thread safe.
781 * See SafeArray.
783 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
785 ULONG ulLocks;
787 TRACE("(%p)\n", psa);
789 if (!psa)
790 return E_INVALIDARG;
792 ulLocks = InterlockedIncrement(&psa->cLocks);
794 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
796 WARN("Out of locks!\n");
797 InterlockedDecrement(&psa->cLocks);
798 return E_UNEXPECTED;
800 return S_OK;
803 /*************************************************************************
804 * SafeArrayUnlock (OLEAUT32.22)
806 * Decrement the lock counter of a SafeArray.
808 * PARAMS
809 * psa [O] SafeArray to unlock
811 * RETURNS
812 * Success: S_OK. The array lock is decremented.
813 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
814 * held on the array.
816 * NOTES
817 * See SafeArray.
819 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
821 TRACE("(%p)\n", psa);
823 if (!psa)
824 return E_INVALIDARG;
826 if ((LONG)InterlockedDecrement(&psa->cLocks) < 0)
828 WARN("Unlocked but no lock held!\n");
829 InterlockedIncrement(&psa->cLocks);
830 return E_UNEXPECTED;
832 return S_OK;
835 /*************************************************************************
836 * SafeArrayPutElement (OLEAUT32.26)
838 * Put an item into a SafeArray.
840 * PARAMS
841 * psa [I] SafeArray to insert into
842 * rgIndices [I] Indices to insert at
843 * pvData [I] Data to insert
845 * RETURNS
846 * Success: S_OK. The item is inserted
847 * Failure: An HRESULT error code indicating the error.
849 * NOTES
850 * See SafeArray.
852 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
854 HRESULT hRet;
856 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
858 if (!psa || !rgIndices)
859 return E_INVALIDARG;
861 if (!pvData)
863 ERR("Invalid pvData would crash under Win32!\n");
864 return E_INVALIDARG;
867 hRet = SafeArrayLock(psa);
869 if (SUCCEEDED(hRet))
871 PVOID lpvDest;
873 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
875 if (SUCCEEDED(hRet))
877 if (psa->fFeatures & FADF_VARIANT)
879 VARIANT* lpVariant = (VARIANT*)pvData;
880 VARIANT* lpDest = (VARIANT*)lpvDest;
882 hRet = VariantClear(lpDest);
883 if (FAILED(hRet)) FIXME("VariantClear failed with 0x%lx\n", hRet);
884 hRet = VariantCopy(lpDest, lpVariant);
885 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%lx\n", hRet);
887 else if (psa->fFeatures & FADF_BSTR)
889 BSTR lpBstr = (BSTR)pvData;
890 BSTR* lpDest = (BSTR*)lpvDest;
892 if (*lpDest)
893 SysFreeString(*lpDest);
895 if (lpBstr)
897 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
898 if (!*lpDest)
899 hRet = E_OUTOFMEMORY;
901 else
902 *lpDest = NULL;
904 else
906 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
908 LPUNKNOWN lpUnknown = (LPUNKNOWN)pvData;
909 LPUNKNOWN *lpDest = (LPUNKNOWN *)lpvDest;
911 if (lpUnknown)
912 IUnknown_AddRef(lpUnknown);
913 if (*lpDest)
914 IUnknown_Release(*lpDest);
915 *lpDest = lpUnknown;
916 } else {
917 /* Copy the data over */
918 memcpy(lpvDest, pvData, psa->cbElements);
922 SafeArrayUnlock(psa);
924 return hRet;
928 /*************************************************************************
929 * SafeArrayGetElement (OLEAUT32.25)
931 * Get an item from a SafeArray.
933 * PARAMS
934 * psa [I] SafeArray to get from
935 * rgIndices [I] Indices to get from
936 * pvData [O] Destination for data
938 * RETURNS
939 * Success: S_OK. The item data is returned in pvData.
940 * Failure: An HRESULT error code indicating the error.
942 * NOTES
943 * See SafeArray.
945 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
947 HRESULT hRet;
949 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
951 if (!psa || !rgIndices || !pvData)
952 return E_INVALIDARG;
954 hRet = SafeArrayLock(psa);
956 if (SUCCEEDED(hRet))
958 PVOID lpvSrc;
960 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
962 if (SUCCEEDED(hRet))
964 if (psa->fFeatures & FADF_VARIANT)
966 VARIANT* lpVariant = (VARIANT*)lpvSrc;
967 VARIANT* lpDest = (VARIANT*)pvData;
969 /* The original content of pvData is ignored. */
970 V_VT(lpDest) = VT_EMPTY;
971 hRet = VariantCopy(lpDest, lpVariant);
972 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%lx\n", hRet);
974 else if (psa->fFeatures & FADF_BSTR)
976 BSTR* lpBstr = (BSTR*)lpvSrc;
977 BSTR* lpDest = (BSTR*)pvData;
979 if (*lpBstr)
981 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
982 if (!*lpBstr)
983 hRet = E_OUTOFMEMORY;
985 else
986 *lpDest = NULL;
988 else
990 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
992 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)lpvSrc;
994 if (*lpUnknown)
995 IUnknown_AddRef(*lpUnknown);
997 /* Copy the data over */
998 memcpy(pvData, lpvSrc, psa->cbElements);
1001 SafeArrayUnlock(psa);
1003 return hRet;
1006 /*************************************************************************
1007 * SafeArrayGetUBound (OLEAUT32.19)
1009 * Get the upper bound for a given SafeArray dimension
1011 * PARAMS
1012 * psa [I] Array to get dimension upper bound from
1013 * nDim [I] The dimension number to get the upper bound of
1014 * plUbound [O] Destination for the upper bound
1016 * RETURNS
1017 * Success: S_OK. plUbound contains the dimensions upper bound.
1018 * Failure: An HRESULT error code indicating the error.
1020 * NOTES
1021 * See SafeArray.
1023 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
1025 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1027 if (!psa || !plUbound)
1028 return E_INVALIDARG;
1030 if(!nDim || nDim > psa->cDims)
1031 return DISP_E_BADINDEX;
1033 *plUbound = psa->rgsabound[nDim - 1].lLbound +
1034 psa->rgsabound[nDim - 1].cElements - 1;
1036 return S_OK;
1039 /*************************************************************************
1040 * SafeArrayGetLBound (OLEAUT32.20)
1042 * Get the lower bound for a given SafeArray dimension
1044 * PARAMS
1045 * psa [I] Array to get dimension lower bound from
1046 * nDim [I] The dimension number to get the lowe bound of
1047 * plLbound [O] Destination for the lower bound
1049 * RETURNS
1050 * Success: S_OK. plUbound contains the dimensions lower bound.
1051 * Failure: An HRESULT error code indicating the error.
1053 * NOTES
1054 * See SafeArray.
1056 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1058 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1060 if (!psa || !plLbound)
1061 return E_INVALIDARG;
1063 if(!nDim || nDim > psa->cDims)
1064 return DISP_E_BADINDEX;
1066 *plLbound = psa->rgsabound[nDim - 1].lLbound;
1067 return S_OK;
1070 /*************************************************************************
1071 * SafeArrayGetDim (OLEAUT32.17)
1073 * Get the number of dimensions in a SafeArray.
1075 * PARAMS
1076 * psa [I] Array to get the dimensions of
1078 * RETURNS
1079 * The number of array dimensions in psa, or 0 if psa is NULL.
1081 * NOTES
1082 * See SafeArray.
1084 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1086 TRACE("(%p) returning %ld\n", psa, psa ? psa->cDims : 0ul);
1087 return psa ? psa->cDims : 0;
1090 /*************************************************************************
1091 * SafeArrayGetElemsize (OLEAUT32.18)
1093 * Get the size of an element in a SafeArray.
1095 * PARAMS
1096 * psa [I] Array to get the element size from
1098 * RETURNS
1099 * The size of a single element in psa, or 0 if psa is NULL.
1101 * NOTES
1102 * See SafeArray.
1104 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1106 TRACE("(%p) returning %ld\n", psa, psa ? psa->cbElements : 0ul);
1107 return psa ? psa->cbElements : 0;
1110 /*************************************************************************
1111 * SafeArrayAccessData (OLEAUT32.23)
1113 * Lock a SafeArray and return a pointer to its data.
1115 * PARAMS
1116 * psa [I] Array to get the data pointer from
1117 * ppvData [O] Destination for the arrays data pointer
1119 * RETURNS
1120 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1121 * is locked.
1122 * Failure: An HRESULT error code indicating the error.
1124 * NOTES
1125 * See SafeArray.
1127 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1129 TRACE("(%p,%p)\n", psa, ppvData);
1131 if(!psa || !ppvData)
1132 return E_INVALIDARG;
1134 if (SUCCEEDED(SafeArrayLock(psa)))
1136 *ppvData = psa->pvData;
1137 return S_OK;
1139 *ppvData = NULL;
1140 return E_UNEXPECTED;
1144 /*************************************************************************
1145 * SafeArrayUnaccessData (OLEAUT32.24)
1147 * Unlock a SafeArray after accessing its data.
1149 * PARAMS
1150 * psa [I] Array to unlock
1152 * RETURNS
1153 * Success: S_OK. The array is unlocked.
1154 * Failure: An HRESULT error code indicating the error.
1156 * NOTES
1157 * See SafeArray.
1159 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1161 TRACE("(%p)\n", psa);
1162 return SafeArrayUnlock(psa);
1165 /************************************************************************
1166 * SafeArrayPtrOfIndex (OLEAUT32.148)
1168 * Get the address of an item in a SafeArray.
1170 * PARAMS
1171 * psa [I] Array to get the items address from
1172 * rgIndices [I] Index of the item in the array
1173 * ppvData [O] Destination for item address
1175 * RETURNS
1176 * Success: S_OK. ppvData contains a pointer to the item.
1177 * Failure: An HRESULT error code indicating the error.
1179 * NOTES
1180 * This function does not lock the array.
1182 * NOTES
1183 * See SafeArray.
1185 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1187 USHORT dim;
1188 ULONG cell = 0, dimensionSize = 1;
1189 SAFEARRAYBOUND* psab;
1190 LONG c1;
1192 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1194 /* The general formula for locating the cell number of an entry in an n
1195 * dimensional array (where cn = coordinate in dimension dn) is:
1197 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1199 * We calculate the size of the last dimension at each step through the
1200 * dimensions to avoid recursing to calculate the last dimensions size.
1202 if (!psa || !rgIndices || !ppvData)
1203 return E_INVALIDARG;
1205 psab = psa->rgsabound;
1206 c1 = *rgIndices++;
1208 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1209 return DISP_E_BADINDEX; /* Initial index out of bounds */
1211 for (dim = 1; dim < psa->cDims; dim++)
1213 dimensionSize *= psab->cElements;
1215 psab++;
1217 if (!psab->cElements ||
1218 *rgIndices < psab->lLbound ||
1219 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1220 return DISP_E_BADINDEX; /* Index out of bounds */
1222 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1223 rgIndices++;
1226 cell += (c1 - psa->rgsabound[0].lLbound);
1228 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1229 return S_OK;
1232 /************************************************************************
1233 * SafeArrayDestroyData (OLEAUT32.39)
1235 * Destroy the data associated with a SafeArray.
1237 * PARAMS
1238 * psa [I] Array to delete the data from
1240 * RETURNS
1241 * Success: S_OK. All items and the item data are freed.
1242 * Failure: An HRESULT error code indicating the error.
1244 * NOTES
1245 * See SafeArray.
1247 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1249 TRACE("(%p)\n", psa);
1251 if (!psa)
1252 return E_INVALIDARG;
1254 if (psa->cLocks)
1255 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1257 /* If static, keep pvData and don't free */
1258 if (psa->pvData && !(psa->fFeatures & FADF_STATIC))
1260 /* Delete the actual item data */
1261 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1262 return E_UNEXPECTED;
1264 /* If this is not a vector, free the data memory block */
1265 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1267 if (!SAFEARRAY_Free(psa->pvData))
1268 return E_UNEXPECTED;
1269 psa->pvData = NULL;
1271 else
1272 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1275 return S_OK;
1278 /************************************************************************
1279 * SafeArrayCopyData (OLEAUT32.412)
1281 * Copy all data from one SafeArray to another.
1283 * PARAMS
1284 * psaSource [I] Source for copy
1285 * psaTarget [O] Destination for copy
1287 * RETURNS
1288 * Success: S_OK. psaTarget contains a copy of psaSource.
1289 * Failure: An HRESULT error code indicating the error.
1291 * NOTES
1292 * The two arrays must have the same number of dimensions and elements.
1294 * NOTES
1295 * See SafeArray.
1297 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1299 int dim;
1301 TRACE("(%p,%p)\n", psaSource, psaTarget);
1303 if (!psaSource || !psaTarget ||
1304 psaSource->cDims != psaTarget->cDims ||
1305 psaSource->cbElements != psaTarget->cbElements)
1306 return E_INVALIDARG;
1308 /* Each dimension must be the same size */
1309 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1310 if (psaSource->rgsabound[dim].cElements !=
1311 psaTarget->rgsabound[dim].cElements)
1312 return E_INVALIDARG;
1314 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1315 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1316 return S_OK;
1317 return E_UNEXPECTED;
1320 /************************************************************************
1321 * SafeArrayDestroy (OLEAUT32.16)
1323 * Destroy a SafeArray.
1325 * PARAMS
1326 * psa [I] Array to destroy
1328 * RETURNS
1329 * Success: S_OK. All resources used by the array are freed.
1330 * Failure: An HRESULT error code indicating the error.
1332 * NOTES
1333 * See SafeArray.
1335 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1337 TRACE("(%p)\n", psa);
1339 if(!psa)
1340 return E_INVALIDARG;
1342 if(psa->cLocks > 0)
1343 return DISP_E_ARRAYISLOCKED;
1345 /* Native doesn't check to see if the free succeeds */
1346 SafeArrayDestroyData(psa);
1347 SafeArrayDestroyDescriptor(psa);
1348 return S_OK;
1351 /************************************************************************
1352 * SafeArrayCopy (OLEAUT32.27)
1354 * Make a duplicate of a SafeArray.
1356 * PARAMS
1357 * psa [I] Source for copy
1358 * ppsaOut [O] Destination for new copy
1360 * RETURNS
1361 * Success: S_OK. ppsaOut contains a copy of the array.
1362 * Failure: An HRESULT error code indicating the error.
1364 * NOTES
1365 * See SafeArray.
1367 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1369 HRESULT hRet;
1371 TRACE("(%p,%p)\n", psa, ppsaOut);
1373 if (!ppsaOut)
1374 return E_INVALIDARG;
1376 *ppsaOut = NULL;
1378 if (!psa)
1379 return S_OK; /* Handles copying of NULL arrays */
1381 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1383 VARTYPE vt;
1384 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1385 hRet = E_UNEXPECTED;
1386 else
1387 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1389 else
1391 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1392 if (SUCCEEDED(hRet))
1394 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1395 (*ppsaOut)->cbElements = psa->cbElements;
1399 if (SUCCEEDED(hRet))
1401 /* Copy dimension bounds */
1402 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1404 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1406 if ((*ppsaOut)->pvData)
1408 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1410 if (SUCCEEDED(hRet))
1411 return hRet;
1413 SAFEARRAY_Free((*ppsaOut)->pvData);
1415 SafeArrayDestroyDescriptor(*ppsaOut);
1417 *ppsaOut = NULL;
1418 return hRet;
1421 /************************************************************************
1422 * SafeArrayRedim (OLEAUT32.40)
1424 * Changes the characteristics of the last dimension of a SafeArray
1426 * PARAMS
1427 * psa [I] Array to change
1428 * psabound [I] New bound details for the last dimension
1430 * RETURNS
1431 * Success: S_OK. psa is updated to reflect the new bounds.
1432 * Failure: An HRESULT error code indicating the error.
1434 * NOTES
1435 * See SafeArray.
1437 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1439 SAFEARRAYBOUND *oldBounds;
1441 TRACE("(%p,%p)\n", psa, psabound);
1443 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1444 return E_INVALIDARG;
1446 if (psa->cLocks > 0)
1447 return DISP_E_ARRAYISLOCKED;
1449 if (FAILED(SafeArrayLock(psa)))
1450 return E_UNEXPECTED;
1452 oldBounds = &psa->rgsabound[psa->cDims - 1];
1453 oldBounds->lLbound = psabound->lLbound;
1455 if (psabound->cElements != oldBounds->cElements)
1457 if (psabound->cElements < oldBounds->cElements)
1459 /* Shorten the final dimension. */
1460 ULONG ulStartCell = psa->cDims == 1 ? 0 : SAFEARRAY_GetDimensionCells(psa, psa->cDims - 1);
1462 ulStartCell += psabound->cElements;
1463 SAFEARRAY_DestroyData(psa, ulStartCell);
1465 else
1467 /* Lengthen the final dimension */
1468 ULONG ulOldSize, ulNewSize;
1469 PVOID pvNewData;
1471 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1472 if (ulOldSize)
1473 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1474 else {
1475 int oldelems = oldBounds->cElements;
1476 oldBounds->cElements = psabound->cElements;
1477 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1478 oldBounds->cElements = oldelems;
1481 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1483 SafeArrayUnlock(psa);
1484 return E_UNEXPECTED;
1487 memcpy(pvNewData, psa->pvData, ulOldSize);
1488 SAFEARRAY_Free(psa->pvData);
1489 psa->pvData = pvNewData;
1491 oldBounds->cElements = psabound->cElements;
1494 SafeArrayUnlock(psa);
1495 return S_OK;
1498 /************************************************************************
1499 * SafeArrayGetVartype (OLEAUT32.77)
1501 * Get the type of the items in a SafeArray.
1503 * PARAMS
1504 * psa [I] Array to get the type from
1505 * pvt [O] Destination for the type
1507 * RETURNS
1508 * Success: S_OK. pvt contains the type of the items.
1509 * Failure: An HRESULT error code indicating the error.
1511 * NOTES
1512 * See SafeArray.
1514 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1516 TRACE("(%p,%p)\n", psa, pvt);
1518 if (!psa || !pvt)
1519 return E_INVALIDARG;
1521 if (psa->fFeatures & FADF_RECORD)
1522 *pvt = VT_RECORD;
1523 else if (psa->fFeatures & FADF_HAVEIID)
1524 *pvt = VT_UNKNOWN;
1525 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1527 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1528 *pvt = vt;
1530 else
1531 return E_INVALIDARG;
1533 return S_OK;
1536 /************************************************************************
1537 * SafeArraySetRecordInfo (OLEAUT32.@)
1539 * Set the record info for a SafeArray.
1541 * PARAMS
1542 * psa [I] Array to set the record info for
1543 * pRinfo [I] Record info
1545 * RETURNS
1546 * Success: S_OK. The record info is stored with the array.
1547 * Failure: An HRESULT error code indicating the error.
1549 * NOTES
1550 * See SafeArray.
1552 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1554 IRecordInfo** dest = (IRecordInfo**)psa;
1556 TRACE("(%p,%p)\n", psa, pRinfo);
1558 if (!psa || !(psa->fFeatures & FADF_RECORD))
1559 return E_INVALIDARG;
1561 if (pRinfo)
1562 IRecordInfo_AddRef(pRinfo);
1564 if (dest[-1])
1565 IRecordInfo_Release(dest[-1]);
1567 dest[-1] = pRinfo;
1568 return S_OK;
1571 /************************************************************************
1572 * SafeArrayGetRecordInfo (OLEAUT32.@)
1574 * Get the record info from a SafeArray.
1576 * PARAMS
1577 * psa [I] Array to get the record info from
1578 * pRinfo [O] Destination for the record info
1580 * RETURNS
1581 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1582 * Failure: An HRESULT error code indicating the error.
1584 * NOTES
1585 * See SafeArray.
1587 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1589 IRecordInfo** src = (IRecordInfo**)psa;
1591 TRACE("(%p,%p)\n", psa, pRinfo);
1593 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1594 return E_INVALIDARG;
1596 *pRinfo = src[-1];
1598 if (*pRinfo)
1599 IRecordInfo_AddRef(*pRinfo);
1600 return S_OK;
1603 /************************************************************************
1604 * SafeArraySetIID (OLEAUT32.@)
1606 * Set the IID for a SafeArray.
1608 * PARAMS
1609 * psa [I] Array to set the IID from
1610 * guid [I] IID
1612 * RETURNS
1613 * Success: S_OK. The IID is stored with the array
1614 * Failure: An HRESULT error code indicating the error.
1616 * NOTES
1617 * See SafeArray.
1619 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1621 GUID* dest = (GUID*)psa;
1623 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1625 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1626 return E_INVALIDARG;
1628 dest[-1] = *guid;
1629 return S_OK;
1632 /************************************************************************
1633 * SafeArrayGetIID (OLEAUT32.@)
1635 * Get the IID from a SafeArray.
1637 * PARAMS
1638 * psa [I] Array to get the ID from
1639 * pGuid [O] Destination for the IID
1641 * RETURNS
1642 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1643 * Failure: An HRESULT error code indicating the error.
1645 * NOTES
1646 * See SafeArray.
1648 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1650 GUID* src = (GUID*)psa;
1652 TRACE("(%p,%p)\n", psa, pGuid);
1654 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1655 return E_INVALIDARG;
1657 *pGuid = src[-1];
1658 return S_OK;
1661 /************************************************************************
1662 * VectorFromBstr (OLEAUT32.@)
1664 * Create a SafeArray Vector from the bytes of a BSTR.
1666 * PARAMS
1667 * bstr [I] String to get bytes from
1668 * ppsa [O] Destination for the array
1670 * RETURNS
1671 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1672 * Failure: An HRESULT error code indicating the error.
1674 * NOTES
1675 * See SafeArray.
1677 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1679 SAFEARRAYBOUND sab;
1681 TRACE("(%p,%p)\n", bstr, ppsa);
1683 if (!ppsa)
1684 return E_INVALIDARG;
1686 sab.lLbound = 0;
1687 sab.cElements = SysStringByteLen(bstr);
1689 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1691 if (*ppsa)
1693 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1694 return S_OK;
1696 return E_OUTOFMEMORY;
1699 /************************************************************************
1700 * BstrFromVector (OLEAUT32.@)
1702 * Create a BSTR from a SafeArray.
1704 * PARAMS
1705 * psa [I] Source array
1706 * pbstr [O] Destination for output BSTR
1708 * RETURNS
1709 * Success: S_OK. pbstr contains the arrays data.
1710 * Failure: An HRESULT error code indicating the error.
1712 * NOTES
1713 * psa must be a 1 dimensional array of a 1 byte type.
1715 * NOTES
1716 * See SafeArray.
1718 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1720 TRACE("(%p,%p)\n", psa, pbstr);
1722 if (!pbstr)
1723 return E_INVALIDARG;
1725 *pbstr = NULL;
1727 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1728 return E_INVALIDARG;
1730 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1731 if (!*pbstr)
1732 return E_OUTOFMEMORY;
1733 return S_OK;