Rewrite: Support I8/UI8,RECORD.
[wine.git] / dlls / oleaut32 / safearray.c
blob142bda40a556022c1df6b86b5909146cc139bc19
1 /*************************************************************************
2 * OLE Automation - SafeArray
4 * This file contains the implementation of the SafeArray functions.
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 2003 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* Memory Layout of a SafeArray:
25 * -0x10: start of memory.
26 * -0x10: GUID for VT_DISPATCH and VT_UNKNOWN safearrays (if FADF_HAVEIID)
27 * -0x04: DWORD varianttype; (for all others, except VT_RECORD) (if FADF_HAVEVARTYPE)
28 * -0x4: IRecordInfo* iface; (if FADF_RECORD, for VT_RECORD (can be NULL))
29 * 0x00: SAFEARRAY,
30 * 0x10: SAFEARRAYBOUNDS[0...]
33 #include "config.h"
35 #ifdef HAVE_STRING_H
36 # include <string.h>
37 #endif
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include "windef.h"
41 #include "winerror.h"
42 #include "winbase.h"
43 #include "oleauto.h"
44 #include "wine/debug.h"
45 #include "variant.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
49 /************************************************************************
50 * SafeArray {OLEAUT32}
52 * NOTES
53 * The SafeArray data type provides the underlying interface for Ole
54 * Automations arrays, used for example to represent array types in
55 * Visual Basic(tm) and to gather user defined parameters for invocation through
56 * an IDispatch interface.
58 * Safe arrays provide bounds checking and automatically manage the data
59 * types they contain, for example handing reference counting and copying
60 * of interface pointers. User defined types can be stored in arrays
61 * using the IRecordInfo interface.
63 * There are two types of SafeArray, normal and vectors. Normal arrays can have
64 * multiple dimensions and the data for the array is allocated seperately from
65 * the array header. This is the most flexable type of array. Vectors, on the
66 * other hand, are fixed in size and consist of a single allocated block, and a
67 * single dimension.
69 * DATATYPES
70 * The following types of data can be stored within a SafeArray.
71 * Numeric:
72 *| VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8, VT_UI8, VT_INT, VT_UINT,
73 *| VT_R4, VT_R8, VT_CY, VT_DECIMAL
74 * Interfaces:
75 *| VT_DISPATCH, VT_UNKNOWN, VT_RECORD
76 * Other:
77 *| VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
79 * FUNCTIONS
80 * BstrFromVector()
81 * VectorFromBstr()
84 /* Undocumented hidden space before the start of a SafeArray descriptor */
85 #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
87 /* Value returned by SAFEARRAY_GetCellCount if a dimension is invalid */
88 #define SAFEARRAY_INVALID_CELLS ~0UL
90 /* Allocate memory */
91 static inline LPVOID SAFEARRAY_Malloc(ULONG ulSize)
93 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
94 * instance returned from CoGetMalloc().
96 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulSize);
99 /* Free memory */
100 static inline BOOL SAFEARRAY_Free(LPVOID lpData)
102 return HeapFree(GetProcessHeap(), 0, lpData);
105 /* Get the size of a supported VT type (0 means unsupported) */
106 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
108 switch (vt)
110 case VT_I1:
111 case VT_UI1: return sizeof(BYTE);
112 case VT_BOOL:
113 case VT_I2:
114 case VT_UI2: return sizeof(SHORT);
115 case VT_I4:
116 case VT_UI4:
117 case VT_R4:
118 case VT_ERROR: return sizeof(LONG);
119 case VT_R8:
120 case VT_I8:
121 case VT_UI8: return sizeof(LONG64);
122 case VT_INT:
123 case VT_UINT: return sizeof(INT);
124 case VT_INT_PTR:
125 case VT_UINT_PTR: return sizeof(UINT_PTR);
126 case VT_CY: return sizeof(CY);
127 case VT_DATE: return sizeof(DATE);
128 case VT_BSTR: return sizeof(BSTR);
129 case VT_DISPATCH: return sizeof(LPDISPATCH);
130 case VT_VARIANT: return sizeof(VARIANT);
131 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
132 case VT_DECIMAL: return sizeof(DECIMAL);
133 /* Note: Return a non-zero size to indicate vt is valid. The actual size
134 * of a UDT is taken from the result of IRecordInfo_GetSize().
136 case VT_RECORD: return 32;
138 return 0;
141 /* Set the hidden data for an array */
142 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
144 /* Implementation data is stored in the 4 bytes before the header */
145 LPDWORD lpDw = (LPDWORD)psa;
146 lpDw[-1] = dw;
149 /* Get the hidden data from an array */
150 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
152 LPDWORD lpDw = (LPDWORD)psa;
153 return lpDw[-1];
156 /* Get the number of cells in a SafeArray */
157 static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
159 SAFEARRAYBOUND* psab = psa->rgsabound;
160 USHORT cCount = psa->cDims;
161 ULONG ulNumCells = 1;
163 while (cCount--)
165 if (!psab->cElements)
167 ERR("Dimension has size=0! Please report.\n");
168 return SAFEARRAY_INVALID_CELLS;
170 ulNumCells *= psab->cElements;
171 psab++;
173 return ulNumCells;
176 /* Get the 0 based index of an index into a dimension */
177 static inline ULONG SAFEARRAY_GetDimensionIndex(SAFEARRAYBOUND *psab, ULONG ulIndex)
179 return ulIndex - psab->lLbound;
182 /* Get the size of a dimension in cells */
183 static inline ULONG SAFEARRAY_GetDimensionCells(SAFEARRAY *psa, ULONG ulDim)
185 ULONG size = psa->rgsabound[0].cElements;
187 while (ulDim)
189 size *= psa->rgsabound[ulDim].cElements;
190 ulDim--;
192 return size;
195 /* Allocate a descriptor for an array */
196 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
198 *ppsaOut = (SAFEARRAY*)((char*)SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE) + SAFEARRAY_HIDDEN_SIZE);
200 if (!*ppsaOut)
201 return E_UNEXPECTED;
203 return S_OK;
206 /* Set the features of an array */
207 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
209 /* Set the IID if we have one, otherwise set the type */
210 if (vt == VT_DISPATCH)
212 psa->fFeatures = FADF_HAVEIID;
213 SafeArraySetIID(psa, &IID_IDispatch);
215 else if (vt == VT_UNKNOWN)
217 psa->fFeatures = FADF_HAVEIID;
218 SafeArraySetIID(psa, &IID_IUnknown);
220 else if (vt == VT_RECORD)
221 psa->fFeatures = FADF_RECORD;
222 else
224 psa->fFeatures = FADF_HAVEVARTYPE;
225 SAFEARRAY_SetHiddenDWORD(psa, vt);
229 /* Create an array */
230 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, ULONG ulSize)
232 SAFEARRAY *psa = NULL;
234 if (!rgsabound)
235 return NULL;
237 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
239 switch (vt)
241 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
242 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
243 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
244 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
247 memcpy(psa->rgsabound, rgsabound, cDims * sizeof(SAFEARRAYBOUND));
249 if (ulSize)
250 psa->cbElements = ulSize;
252 if (FAILED(SafeArrayAllocData(psa)))
254 SafeArrayDestroyDescriptor(psa);
255 psa = NULL;
258 return psa;
261 /* Create an array as a vector */
262 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
264 SAFEARRAY *psa = NULL;
266 if (cElements && (vt == VT_RECORD || ulSize))
268 /* Allocate the header and data together */
269 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
271 SAFEARRAY_SetFeatures(vt, psa);
273 psa->cDims = 1;
274 psa->fFeatures |= FADF_CREATEVECTOR;
275 psa->pvData = &psa[1]; /* Data follows the header */
276 psa->cbElements = ulSize;
277 psa->rgsabound[0].cElements = cElements;
278 psa->rgsabound[0].lLbound = lLbound;
280 switch (vt)
282 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
283 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
284 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
285 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
289 else if (!cElements)
291 ERR("Creating vector of size 0! Please report.\n");
293 return psa;
296 /* Free data items in an array */
297 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
299 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
301 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
303 if (ulCellCount == SAFEARRAY_INVALID_CELLS || ulStartCell > ulCellCount)
304 return E_UNEXPECTED;
306 ulCellCount -= ulStartCell;
308 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
310 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell * psa->cbElements;
312 while(ulCellCount--)
314 if (*lpUnknown)
315 IUnknown_Release(*lpUnknown);
316 lpUnknown++;
319 else if (psa->fFeatures & (FADF_RECORD))
321 IRecordInfo *lpRecInfo;
323 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
325 PBYTE pRecordData = (PBYTE)psa->pvData;
326 while(ulCellCount--)
328 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
329 pRecordData += psa->cbElements;
331 IRecordInfo_Release(lpRecInfo);
334 else if (psa->fFeatures & FADF_BSTR)
336 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell * psa->cbElements;
338 while(ulCellCount--)
340 if (*lpBstr)
341 SysFreeString(*lpBstr);
342 lpBstr++;
345 else if (psa->fFeatures & FADF_VARIANT)
347 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell * psa->cbElements;
349 while(ulCellCount--)
351 VariantClear(lpVariant);
352 lpVariant++;
356 return S_OK;
359 /* Copy data items from one array to another */
360 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
362 if (!psa->pvData || !dest->pvData || psa->fFeatures & FADF_DATADELETED)
363 return E_INVALIDARG;
364 else
366 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
368 if (ulCellCount == SAFEARRAY_INVALID_CELLS)
369 return E_UNEXPECTED;
371 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
372 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
374 if (psa->fFeatures & FADF_VARIANT)
376 VARIANT* lpVariant = (VARIANT*)psa->pvData;
377 VARIANT* lpDest = (VARIANT*)dest->pvData;
379 while(ulCellCount--)
381 VariantCopy(lpDest, lpVariant);
382 lpVariant++;
383 lpDest++;
386 else if (psa->fFeatures & FADF_BSTR)
388 BSTR* lpBstr = (BSTR*)psa->pvData;
389 BSTR* lpDest = (BSTR*)dest->pvData;
391 while(ulCellCount--)
393 if (*lpBstr)
395 *lpDest = SysAllocStringLen(*lpBstr, SysStringLen(*lpBstr));
396 if (!*lpDest)
397 return E_OUTOFMEMORY;
399 else
400 *lpDest = NULL;
401 lpBstr++;
402 lpDest++;
405 else
407 /* Copy the data over */
408 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
410 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
412 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)dest->pvData;
414 while(ulCellCount--)
416 if (*lpUnknown)
417 IUnknown_AddRef(*lpUnknown);
418 lpUnknown++;
423 if (psa->fFeatures & FADF_RECORD)
425 IRecordInfo* pRecInfo = NULL;
427 SafeArrayGetRecordInfo(psa, &pRecInfo);
428 SafeArraySetRecordInfo(dest, pRecInfo);
430 if (pRecInfo)
432 /* Release because Get() adds a reference */
433 IRecordInfo_Release(pRecInfo);
436 else if (psa->fFeatures & FADF_HAVEIID)
438 GUID guid;
439 SafeArrayGetIID(psa, &guid);
440 SafeArraySetIID(dest, &guid);
442 else if (psa->fFeatures & FADF_HAVEVARTYPE)
444 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
447 return S_OK;
450 /*************************************************************************
451 * SafeArrayAllocDescriptor (OLEAUT32.36)
453 * Allocate and initialise a descriptor for a SafeArray.
455 * PARAMS
456 * cDims [I] Number of dimensions of the array
457 * ppsaOut [O] Destination for new descriptor
459 * RETURNS
460 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
461 * Failure: An HRESULT error code indicating the error.
463 * NOTES
464 * See SafeArray.
466 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
468 LONG allocSize;
470 TRACE("(%d,%p)\n", cDims, ppsaOut);
472 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
473 return E_INVALIDARG;
475 if (!ppsaOut)
476 return E_POINTER;
478 /* We need enough space for the header and its bounds */
479 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
481 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
482 return E_UNEXPECTED;
484 (*ppsaOut)->cDims = cDims;
486 TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims, allocSize);
487 return S_OK;
490 /*************************************************************************
491 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
493 * Allocate and initialise a descriptor for a SafeArray of a given type.
495 * PARAMS
496 * vt [I] The type of items to store in the array
497 * cDims [I] Number of dimensions of the array
498 * ppsaOut [O] Destination for new descriptor
500 * RETURNS
501 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
502 * Failure: An HRESULT error code indicating the error.
504 * NOTES
505 * - This function does not chack that vt is an allowed VARTYPE.
506 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
507 * See SafeArray.
509 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
511 ULONG cbElements;
512 HRESULT hRet = E_UNEXPECTED;
514 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
516 cbElements = SAFEARRAY_GetVTSize(vt);
517 if (!cbElements)
518 WARN("Creating a descriptor with an invalid VARTYPE!\n");
520 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
522 if (SUCCEEDED(hRet))
524 SAFEARRAY_SetFeatures(vt, *ppsaOut);
525 (*ppsaOut)->cbElements = cbElements;
527 return hRet;
530 /*************************************************************************
531 * SafeArrayAllocData (OLEAUT32.37)
533 * Allocate the data area of a SafeArray.
535 * PARAMS
536 * psa [I] SafeArray to allocate the data area of.
538 * RETURNS
539 * Success: S_OK. The data area is allocated and initialised.
540 * Failure: An HRESULT error code indicating the error.
542 * NOTES
543 * See SafeArray.
545 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
547 HRESULT hRet = E_INVALIDARG;
549 TRACE("(%p)\n", psa);
551 if (psa)
553 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
555 hRet = E_OUTOFMEMORY;
557 if (ulSize != SAFEARRAY_INVALID_CELLS && psa->cbElements)
559 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
561 if (psa->pvData)
563 hRet = S_OK;
564 TRACE("%lu bytes allocated for data at %p (%lu objects).\n",
565 ulSize * psa->cbElements, psa->pvData, ulSize);
569 return hRet;
572 /*************************************************************************
573 * SafeArrayCreate (OLEAUT32.15)
575 * Create a new SafeArray.
577 * PARAMS
578 * vt [I] Type to store in the safe array
579 * cDims [I] Number of array dimensions
580 * rgsabound [I] Bounds of the array dimensions
582 * RETURNS
583 * Success: A pointer to a new array object.
584 * Failure: NULL, if any parameter is invalid or memory allocation fails.
586 * NOTES
587 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
588 * in the Wine implementation.
589 * See SafeArray.
591 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
593 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
595 if (vt == VT_RECORD)
596 return NULL;
598 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
601 /*************************************************************************
602 * SafeArrayCreateEx (OLEAUT32.15)
604 * Create a new SafeArray.
606 * PARAMS
607 * vt [I] Type to store in the safe array
608 * cDims [I] Number of array dimensions
609 * rgsabound [I] Bounds of the array dimensions
610 * pvExtra [I] Extra data
612 * RETURNS
613 * Success: A pointer to a new array object.
614 * Failure: NULL, if any parameter is invalid or memory allocation fails.
616 * NOTES
617 * See SafeArray.
619 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
621 ULONG ulSize = 0;
622 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
623 SAFEARRAY* psa;
625 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
627 if (vt == VT_RECORD)
629 if (!iRecInfo)
630 return NULL;
631 IRecordInfo_GetSize(iRecInfo, &ulSize);
633 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
635 if (pvExtra)
637 switch(vt)
639 case VT_RECORD:
640 SafeArraySetRecordInfo(psa, pvExtra);
641 break;
642 case VT_UNKNOWN:
643 case VT_DISPATCH:
644 SafeArraySetIID(psa, pvExtra);
645 break;
648 return psa;
651 /************************************************************************
652 * SafeArrayCreateVector (OLEAUT32.411)
654 * Create a one dimensional, contigous SafeArray.
656 * PARAMS
657 * vt [I] Type to store in the safe array
658 * lLbound [I] Lower bound of the array
659 * cElements [I] Number of elements in the array
661 * RETURNS
662 * Success: A pointer to a new array object.
663 * Failure: NULL, if any parameter is invalid or memory allocation fails.
665 * NOTES
666 * See SafeArray.
668 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
670 TRACE("(%d->%s,%ld,%ld\n", vt, debugstr_vt(vt), lLbound, cElements);
672 if (vt == VT_RECORD)
673 return NULL;
675 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
678 /************************************************************************
679 * SafeArrayCreateVectorEx (OLEAUT32.411)
681 * Create a one dimensional, contigous SafeArray.
683 * PARAMS
684 * vt [I] Type to store in the safe array
685 * lLbound [I] Lower bound of the array
686 * cElements [I] Number of elements in the array
687 * pvExtra [I] Extra data
689 * RETURNS
690 * Success: A pointer to a new array object.
691 * Failure: NULL, if any parameter is invalid or memory allocation fails.
693 * NOTES
694 * See SafeArray.
696 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
698 ULONG ulSize;
699 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
700 SAFEARRAY* psa;
702 TRACE("(%d->%s,%ld,%ld,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
704 if (vt == VT_RECORD)
706 if (!iRecInfo)
707 return NULL;
708 IRecordInfo_GetSize(iRecInfo, &ulSize);
710 else
711 ulSize = SAFEARRAY_GetVTSize(vt);
713 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
715 if (pvExtra)
717 switch(vt)
719 case VT_RECORD:
720 SafeArraySetRecordInfo(psa, iRecInfo);
721 break;
722 case VT_UNKNOWN:
723 case VT_DISPATCH:
724 SafeArraySetIID(psa, pvExtra);
725 break;
728 return psa;
731 /*************************************************************************
732 * SafeArrayDestroyDescriptor (OLEAUT32.38)
734 * Destroy a SafeArray.
736 * PARAMS
737 * psa [I] SafeArray to destroy.
739 * RETURNS
740 * Success: S_OK. The resources used by the array are freed.
741 * Failure: An HRESULT error code indicating the error.
743 * NOTES
744 * See SafeArray.
746 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
748 TRACE("(%p)\n", psa);
750 if (psa)
752 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
754 if (psa->cLocks)
755 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
757 if (psa->fFeatures & FADF_RECORD)
758 SafeArraySetRecordInfo(psa, NULL);
760 if (psa->fFeatures & FADF_CREATEVECTOR &&
761 !(psa->fFeatures & FADF_DATADELETED))
762 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
764 if (!SAFEARRAY_Free(lpv))
765 return E_UNEXPECTED;
767 return S_OK;
770 /*************************************************************************
771 * SafeArrayLock (OLEAUT32.21)
773 * Increment the lock counter of a SafeArray.
775 * PARAMS
776 * psa [O] SafeArray to lock
778 * RETURNS
779 * Success: S_OK. The array lock is incremented.
780 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
781 * are held on the array at once.
783 * NOTES
784 * In Win32 these locks are not thread safe.
785 * See SafeArray.
787 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
789 ULONG ulLocks;
791 TRACE("(%p)\n", psa);
793 if (!psa)
794 return E_INVALIDARG;
796 ulLocks = InterlockedIncrement(&psa->cLocks);
798 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
800 WARN("Out of locks!\n");
801 InterlockedDecrement(&psa->cLocks);
802 return E_UNEXPECTED;
804 return S_OK;
807 /*************************************************************************
808 * SafeArrayUnlock (OLEAUT32.22)
810 * Decrement the lock counter of a SafeArray.
812 * PARAMS
813 * psa [O] SafeArray to unlock
815 * RETURNS
816 * Success: S_OK. The array lock is decremented.
817 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
818 * held on the array.
820 * NOTES
821 * See SafeArray.
823 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
825 TRACE("(%p)\n", psa);
827 if (!psa)
828 return E_INVALIDARG;
830 if ((LONG)InterlockedDecrement(&psa->cLocks) < 0)
832 WARN("Unlocked but no lock held!\n");
833 InterlockedIncrement(&psa->cLocks);
834 return E_UNEXPECTED;
836 return S_OK;
839 /*************************************************************************
840 * SafeArrayPutElement (OLEAUT32.26)
842 * Put an item into a SafeArray.
844 * PARAMS
845 * psa [I] SafeArray to insert into
846 * rgIndices [I] Indices to insert at
847 * pvData [I] Data to insert
849 * RETURNS
850 * Success: S_OK. The item is inserted
851 * Failure: An HRESULT error code indicating the error.
853 * NOTES
854 * See SafeArray.
856 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
858 HRESULT hRet;
860 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
862 if (!psa || !rgIndices)
863 return E_INVALIDARG;
865 if (!pvData)
867 ERR("Invalid pvData would crash under Win32!\n");
868 return E_INVALIDARG;
871 hRet = SafeArrayLock(psa);
873 if (SUCCEEDED(hRet))
875 PVOID lpvDest;
877 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
879 if (SUCCEEDED(hRet))
881 if (psa->fFeatures & FADF_VARIANT)
883 VARIANT* lpVariant = (VARIANT*)pvData;
884 VARIANT* lpDest = (VARIANT*)lpvDest;
886 VariantClear(lpDest);
887 VariantCopy(lpDest, lpVariant);
889 else if (psa->fFeatures & FADF_BSTR)
891 BSTR* lpBstr = (BSTR*)pvData;
892 BSTR* lpDest = (BSTR*)lpvDest;
894 if (*lpDest)
895 SysFreeString(*lpDest);
897 if (*lpBstr)
899 *lpDest = SysAllocStringLen(*lpBstr, SysStringLen(*lpBstr));
900 if (!*lpDest)
901 hRet = E_OUTOFMEMORY;
903 else
904 *lpDest = NULL;
906 else
908 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
910 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)pvData;
911 LPUNKNOWN *lpDest = (LPUNKNOWN *)lpvDest;
913 if (*lpUnknown)
914 IUnknown_AddRef(*lpUnknown);
915 if (*lpDest)
916 IUnknown_Release(*lpDest);
918 /* Copy the data over */
919 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 VariantCopy(lpDest, lpVariant);
971 else if (psa->fFeatures & FADF_BSTR)
973 BSTR* lpBstr = (BSTR*)lpvSrc;
974 BSTR* lpDest = (BSTR*)pvData;
976 if (*lpBstr)
978 *lpDest = SysAllocStringLen(*lpBstr, SysStringLen(*lpBstr));
979 if (!*lpBstr)
980 hRet = E_OUTOFMEMORY;
982 else
983 *lpDest = NULL;
985 else
987 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
989 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)lpvSrc;
991 if (*lpUnknown)
992 IUnknown_AddRef(*lpUnknown);
994 /* Copy the data over */
995 memcpy(pvData, lpvSrc, psa->cbElements);
998 SafeArrayUnlock(psa);
1000 return hRet;
1003 /*************************************************************************
1004 * SafeArrayGetUBound (OLEAUT32.19)
1006 * Get the upper bound for a given SafeArray dimension
1008 * PARAMS
1009 * psa [I] Array to get dimension upper bound from
1010 * nDim [I] The dimension number to get the upper bound of
1011 * plUbound [O] Destination for the upper bound
1013 * RETURNS
1014 * Success: S_OK. plUbound contains the dimensions upper bound.
1015 * Failure: An HRESULT error code indicating the error.
1017 * NOTES
1018 * See SafeArray.
1020 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
1022 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1024 if (!psa || !plUbound)
1025 return E_INVALIDARG;
1027 if(!nDim || nDim > psa->cDims || !psa->rgsabound[nDim - 1].cElements)
1028 return DISP_E_BADINDEX;
1030 *plUbound = psa->rgsabound[nDim - 1].lLbound +
1031 psa->rgsabound[nDim - 1].cElements - 1;
1033 return S_OK;
1036 /*************************************************************************
1037 * SafeArrayGetLBound (OLEAUT32.20)
1039 * Get the lower bound for a given SafeArray dimension
1041 * PARAMS
1042 * psa [I] Array to get dimension lower bound from
1043 * nDim [I] The dimension number to get the lowe bound of
1044 * plLbound [O] Destination for the lower bound
1046 * RETURNS
1047 * Success: S_OK. plUbound contains the dimensions lower bound.
1048 * Failure: An HRESULT error code indicating the error.
1050 * NOTES
1051 * See SafeArray.
1053 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1055 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1057 if (!psa || !plLbound)
1058 return E_INVALIDARG;
1060 if(!nDim || nDim > psa->cDims || !psa->rgsabound[nDim - 1].cElements)
1061 return DISP_E_BADINDEX;
1063 *plLbound = psa->rgsabound[nDim - 1].lLbound;
1064 return S_OK;
1067 /*************************************************************************
1068 * SafeArrayGetDim (OLEAUT32.17)
1070 * Get the number of dimensions in a SafeArray.
1072 * PARAMS
1073 * psa [I] Array to get the dimensions of
1075 * RETURNS
1076 * The number of array dimensions in psa, or 0 if psa is NULL.
1078 * NOTES
1079 * See SafeArray.
1081 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1083 TRACE("(%p) returning %ld\n", psa, psa ? psa->cDims : 0ul);
1084 return psa ? psa->cDims : 0;
1087 /*************************************************************************
1088 * SafeArrayGetElemsize (OLEAUT32.18)
1090 * Get the size of an element in a SafeArray.
1092 * PARAMS
1093 * psa [I] Array to get the element size from
1095 * RETURNS
1096 * The size of a single element in psa, or 0 if psa is NULL.
1098 * NOTES
1099 * See SafeArray.
1101 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1103 TRACE("(%p) returning %ld\n", psa, psa ? psa->cbElements : 0ul);
1104 return psa ? psa->cbElements : 0;
1107 /*************************************************************************
1108 * SafeArrayAccessData (OLEAUT32.23)
1110 * Lock a SafeArray and return a pointer to its data.
1112 * PARAMS
1113 * psa [I] Array to get the data pointer from
1114 * ppvData [O] Destination for the arrays data pointer
1116 * RETURNS
1117 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1118 * is locked.
1119 * Failure: An HRESULT error code indicating the error.
1121 * NOTES
1122 * See SafeArray.
1124 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1126 TRACE("(%p,%p)\n", psa, ppvData);
1128 if(!psa || !ppvData)
1129 return E_INVALIDARG;
1131 if (SUCCEEDED(SafeArrayLock(psa)))
1133 *ppvData = psa->pvData;
1134 return S_OK;
1136 *ppvData = NULL;
1137 return E_UNEXPECTED;
1141 /*************************************************************************
1142 * SafeArrayUnaccessData (OLEAUT32.24)
1144 * Unlock a SafeArray after accessing its data.
1146 * PARAMS
1147 * psa [I] Array to unlock
1149 * RETURNS
1150 * Success: S_OK. The array is unlocked.
1151 * Failure: An HRESULT error code indicating the error.
1153 * NOTES
1154 * See SafeArray.
1156 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1158 TRACE("(%p)\n", psa);
1159 return SafeArrayUnlock(psa);
1162 /************************************************************************
1163 * SafeArrayPtrOfIndex (OLEAUT32.148)
1165 * Get the address of an item in a SafeArray.
1167 * PARAMS
1168 * psa [I] Array to get the items address from
1169 * rgIndices [I] Index of the item in the array
1170 * ppvData [O] Destination for item address
1172 * RETURNS
1173 * Success: S_OK. ppvData contains a pointer to the item.
1174 * Failure: An HRESULT error code indicating the error.
1176 * NOTES
1177 * This function does not lock the array.
1179 * NOTES
1180 * See SafeArray.
1182 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1184 USHORT dim;
1185 ULONG cell = 0, dimensionSize = 1;
1186 SAFEARRAYBOUND* psab;
1187 LONG c1;
1189 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1191 /* The general formula for locating the cell number of an entry in an n
1192 * dimensional array (where cn = coordinate in dimension dn) is:
1194 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1196 * We calculate the size of the last dimension at each step through the
1197 * dimensions to avoid recursing to calculate the last dimensions size.
1199 if (!psa || !rgIndices || !ppvData)
1200 return E_INVALIDARG;
1202 psab = psa->rgsabound;
1203 c1 = *rgIndices++;
1205 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1206 return DISP_E_BADINDEX; /* Initial index out of bounds */
1208 for (dim = 1; dim < psa->cDims; dim++)
1210 dimensionSize *= psab->cElements;
1212 psab++;
1214 if (!psab->cElements ||
1215 *rgIndices < psab->lLbound ||
1216 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1217 return DISP_E_BADINDEX; /* Index out of bounds */
1219 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1220 rgIndices++;
1223 cell += (c1 - psa->rgsabound[0].lLbound);
1225 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1226 return S_OK;
1229 /************************************************************************
1230 * SafeArrayDestroyData (OLEAUT32.39)
1232 * Destroy the data associated with a SafeArray.
1234 * PARAMS
1235 * psa [I] Array to delete the data from
1237 * RETURNS
1238 * Success: S_OK. All items and the item data are freed.
1239 * Failure: An HRESULT error code indicating the error.
1241 * NOTES
1242 * See SafeArray.
1244 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1246 TRACE("(%p)\n", psa);
1248 if (!psa)
1249 return E_INVALIDARG;
1251 if (psa->cLocks)
1252 return DISP_E_ARRAYISLOCKED; /* Cant delete a locked array */
1254 if (psa->pvData)
1256 /* Delete the actual item data */
1257 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1258 return E_UNEXPECTED;
1260 /* If this is not a vector, free the data memory block */
1261 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1263 if (!SAFEARRAY_Free(psa->pvData))
1264 return E_UNEXPECTED;
1265 psa->pvData = NULL;
1267 else
1268 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1271 return S_OK;
1274 /************************************************************************
1275 * SafeArrayCopyData (OLEAUT32.412)
1277 * Copy all data from one SafeArray to another.
1279 * PARAMS
1280 * psaSource [I] Source for copy
1281 * psaTarget [O] Destination for copy
1283 * RETURNS
1284 * Success: S_OK. psaTarget contains a copy of psaSource.
1285 * Failure: An HRESULT error code indicating the error.
1287 * NOTES
1288 * The two arrays must have the same number of dimensions and elements.
1290 * NOTES
1291 * See SafeArray.
1293 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1295 int dim;
1297 TRACE("(%p,%p)\n", psaSource, psaTarget);
1299 if (!psaSource || !psaTarget ||
1300 psaSource->cDims != psaTarget->cDims ||
1301 psaSource->cbElements != psaTarget->cbElements)
1302 return E_INVALIDARG;
1304 /* Each dimension must be the same size */
1305 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1306 if (psaSource->rgsabound[dim].cElements !=
1307 psaTarget->rgsabound[dim].cElements)
1308 return E_INVALIDARG;
1310 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1311 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1312 return S_OK;
1313 return E_UNEXPECTED;
1316 /************************************************************************
1317 * SafeArrayDestroy (OLEAUT32.16)
1319 * Destroy a SafeArray.
1321 * PARAMS
1322 * psa [I] Array to destroy
1324 * RETURNS
1325 * Success: S_OK. All resources used by the array are freed.
1326 * Failure: An HRESULT error code indicating the error.
1328 * NOTES
1329 * See SafeArray.
1331 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1333 TRACE("(%p)\n", psa);
1335 if(!psa)
1336 return E_INVALIDARG;
1338 if(psa->cLocks > 0)
1339 return DISP_E_ARRAYISLOCKED;
1341 /* Native doesn't check to see if the free succeeds */
1342 SafeArrayDestroyData(psa);
1343 SafeArrayDestroyDescriptor(psa);
1344 return S_OK;
1347 /************************************************************************
1348 * SafeArrayCopy (OLEAUT32.27)
1350 * Make a duplicate of a SafeArray.
1352 * PARAMS
1353 * psa [I] Source for copy
1354 * ppsaOut [O] Destination for new copy
1356 * RETURNS
1357 * Success: S_OK. ppsaOut contains a copy of the array.
1358 * Failure: An HRESULT error code indicating the error.
1360 * NOTES
1361 * See SafeArray.
1363 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1365 HRESULT hRet;
1367 TRACE("(%p,%p)\n", psa, ppsaOut);
1369 if (!ppsaOut)
1370 return E_INVALIDARG;
1372 *ppsaOut = NULL;
1374 if (!psa)
1375 return S_OK; /* Handles copying of NULL arrays */
1377 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1379 VARTYPE vt;
1380 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1381 hRet = E_UNEXPECTED;
1382 else
1383 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1385 else
1387 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1388 if (SUCCEEDED(hRet))
1390 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1391 (*ppsaOut)->cbElements = psa->cbElements;
1395 if (SUCCEEDED(hRet))
1397 /* Copy dimension bounds */
1398 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1400 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1402 if ((*ppsaOut)->pvData)
1404 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1406 if (SUCCEEDED(hRet))
1407 return hRet;
1409 SAFEARRAY_Free((*ppsaOut)->pvData);
1411 SafeArrayDestroyDescriptor(*ppsaOut);
1413 *ppsaOut = NULL;
1414 return hRet;
1417 /************************************************************************
1418 * SafeArrayRedim (OLEAUT32.40)
1420 * Changes the characteristics of the last dimension of a SafeArray
1422 * PARAMS
1423 * psa [I] Array to change
1424 * psabound [I] New bound details for the last dimension
1426 * RETURNS
1427 * Success: S_OK. psa is updated to reflect the new bounds.
1428 * Failure: An HRESULT error code indicating the error.
1430 * NOTES
1431 * See SafeArray.
1433 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1435 SAFEARRAYBOUND *oldBounds;
1437 TRACE("(%p,%p)\n", psa, psabound);
1439 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound || !psabound->cElements)
1440 return E_INVALIDARG;
1442 if (psa->cLocks > 0)
1443 return DISP_E_ARRAYISLOCKED;
1445 if (FAILED(SafeArrayLock(psa)))
1446 return E_UNEXPECTED;
1448 oldBounds = &psa->rgsabound[psa->cDims - 1];
1449 oldBounds->lLbound = psabound->lLbound;
1451 if (psabound->cElements != oldBounds->cElements)
1453 if (psabound->cElements < oldBounds->cElements)
1455 /* Shorten the final dimension. */
1456 ULONG ulStartCell = psa->cDims == 1 ? 0 : SAFEARRAY_GetDimensionCells(psa, psa->cDims - 1);
1458 ulStartCell += psabound->cElements;
1459 SAFEARRAY_DestroyData(psa, ulStartCell);
1461 else
1463 /* Lengthen the final dimension */
1464 ULONG ulOldSize, ulNewSize;
1465 PVOID pvNewData;
1467 ulOldSize = SAFEARRAY_GetCellCount(psa);
1468 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1470 if (ulOldSize == SAFEARRAY_INVALID_CELLS ||
1471 !(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1473 SafeArrayUnlock(psa);
1474 return E_UNEXPECTED;
1477 memcpy(pvNewData, psa->pvData, ulOldSize);
1478 SAFEARRAY_Free(psa->pvData);
1479 psa->pvData = pvNewData;
1481 oldBounds->cElements = psabound->cElements;
1484 SafeArrayUnlock(psa);
1485 return S_OK;
1488 /************************************************************************
1489 * SafeArrayGetVartype (OLEAUT32.77)
1491 * Get the type of the items in a SafeArray.
1493 * PARAMS
1494 * psa [I] Array to get the type from
1495 * pvt [O] Destination for the type
1497 * RETURNS
1498 * Success: S_OK. pvt contains the type of the items.
1499 * Failure: An HRESULT error code indicating the error.
1501 * NOTES
1502 * See SafeArray.
1504 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1506 TRACE("(%p,%p)\n", psa, pvt);
1508 if (!psa || !pvt)
1509 return E_INVALIDARG;
1511 if (psa->fFeatures & FADF_RECORD)
1512 *pvt = VT_RECORD;
1513 else if (psa->fFeatures & FADF_HAVEIID)
1514 *pvt = VT_UNKNOWN;
1515 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1517 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1518 *pvt = vt;
1520 else
1521 return E_INVALIDARG;
1523 return S_OK;
1526 /************************************************************************
1527 * SafeArraySetRecordInfo (OLEAUT32.@)
1529 * Set the record info for a SafeArray.
1531 * PARAMS
1532 * psa [I] Array to set the record info for
1533 * pRinfo [I] Record info
1535 * RETURNS
1536 * Success: S_OK. The record info is stored with the array.
1537 * Failure: An HRESULT error code indicating the error.
1539 * NOTES
1540 * See SafeArray.
1542 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1544 IRecordInfo** dest = (IRecordInfo**)psa;
1546 TRACE("(%p,%p)\n", psa, pRinfo);
1548 if (!psa || !(psa->fFeatures & FADF_RECORD))
1549 return E_INVALIDARG;
1551 if (pRinfo)
1552 IRecordInfo_AddRef(pRinfo);
1554 if (dest[-1])
1555 IRecordInfo_Release(dest[-1]);
1557 dest[-1] = pRinfo;
1558 return S_OK;
1561 /************************************************************************
1562 * SafeArrayGetRecordInfo (OLEAUT32.@)
1564 * Get the record info from a SafeArray.
1566 * PARAMS
1567 * psa [I] Array to get the record info from
1568 * pRinfo [O] Destination for the record info
1570 * RETURNS
1571 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1572 * Failure: An HRESULT error code indicating the error.
1574 * NOTES
1575 * See SafeArray.
1577 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1579 IRecordInfo** src = (IRecordInfo**)psa;
1581 TRACE("(%p,%p)\n", psa, pRinfo);
1583 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1584 return E_INVALIDARG;
1586 *pRinfo = src[-1];
1588 if (*pRinfo)
1589 IRecordInfo_AddRef(*pRinfo);
1590 return S_OK;
1593 /************************************************************************
1594 * SafeArraySetIID (OLEAUT32.@)
1596 * Set the IID for a SafeArray.
1598 * PARAMS
1599 * psa [I] Array to set the IID from
1600 * guid [I] IID
1602 * RETURNS
1603 * Success: S_OK. The IID is stored with the array
1604 * Failure: An HRESULT error code indicating the error.
1606 * NOTES
1607 * See SafeArray.
1609 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1611 GUID* dest = (GUID*)psa;
1613 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1615 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1616 return E_INVALIDARG;
1618 dest[-1] = *guid;
1619 return S_OK;
1622 /************************************************************************
1623 * SafeArrayGetIID (OLEAUT32.@)
1625 * Get the IID from a SafeArray.
1627 * PARAMS
1628 * psa [I] Array to get the ID from
1629 * pGuid [O] Destination for the IID
1631 * RETURNS
1632 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1633 * Failure: An HRESULT error code indicating the error.
1635 * NOTES
1636 * See SafeArray.
1638 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1640 GUID* src = (GUID*)psa;
1642 TRACE("(%p,%p)\n", psa, pGuid);
1644 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1645 return E_INVALIDARG;
1647 *pGuid = src[-1];
1648 return S_OK;
1651 /************************************************************************
1652 * VectorFromBstr (OLEAUT32.@)
1654 * Create a SafeArray Vector from the bytes of a BSTR.
1656 * PARAMS
1657 * bstr [I] String to get bytes from
1658 * ppsa [O] Destination for the array
1660 * RETURNS
1661 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1662 * Failure: An HRESULT error code indicating the error.
1664 * NOTES
1665 * See SafeArray.
1667 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1669 SAFEARRAYBOUND sab;
1671 TRACE("(%p,%p)\n", bstr, ppsa);
1673 if (!ppsa)
1674 return E_INVALIDARG;
1676 sab.lLbound = 0;
1677 sab.cElements = SysStringByteLen(bstr);
1679 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1681 if (*ppsa)
1683 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1684 return S_OK;
1686 return E_OUTOFMEMORY;
1689 /************************************************************************
1690 * BstrFromVector (OLEAUT32.@)
1692 * Create a BSTR from a SafeArray.
1694 * PARAMS
1695 * psa [I] Source array
1696 * pbstr [O] Destination for output BSTR
1698 * RETURNS
1699 * Success: S_OK. pbstr contains the arrays data.
1700 * Failure: An HRESULT error code indicating the error.
1702 * NOTES
1703 * psa must be a 1 dimensional array of a 1 byte type.
1705 * NOTES
1706 * See SafeArray.
1708 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1710 TRACE("(%p,%p)\n", psa, pbstr);
1712 if (!pbstr)
1713 return E_INVALIDARG;
1715 *pbstr = NULL;
1717 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1718 return E_INVALIDARG;
1720 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1721 if (!*pbstr)
1722 return E_OUTOFMEMORY;
1723 return S_OK;