Correct errors with move to kernel time functions.
[wine/multimedia.git] / dlls / oleaut32 / safearray.c
blob5b4a0e46f53dcb863bea9f4549057873167a0582
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>
39 #include "windef.h"
40 #include "winerror.h"
41 #include "winbase.h"
42 #include "oleauto.h"
43 #include "wine/debug.h"
44 #include "variant.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(ole);
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 seperately from
64 * the array header. This is the most flexable 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 /* Allocate memory */
87 static inline LPVOID SAFEARRAY_Malloc(ULONG ulSize)
89 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
90 * instance returned from CoGetMalloc().
92 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulSize);
95 /* Free memory */
96 static inline BOOL SAFEARRAY_Free(LPVOID lpData)
98 return HeapFree(GetProcessHeap(), 0, lpData);
101 /* Get the size of a supported VT type (0 means unsupported) */
102 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
104 switch (vt)
106 case VT_I1:
107 case VT_UI1: return sizeof(BYTE);
108 case VT_BOOL:
109 case VT_I2:
110 case VT_UI2: return sizeof(SHORT);
111 case VT_I4:
112 case VT_UI4:
113 case VT_R4:
114 case VT_ERROR: return sizeof(LONG);
115 case VT_R8:
116 case VT_I8:
117 case VT_UI8: return sizeof(LONG64);
118 case VT_INT:
119 case VT_UINT: return sizeof(INT);
120 case VT_INT_PTR:
121 case VT_UINT_PTR: return sizeof(UINT_PTR);
122 case VT_CY: return sizeof(CY);
123 case VT_DATE: return sizeof(DATE);
124 case VT_BSTR: return sizeof(BSTR);
125 case VT_DISPATCH: return sizeof(LPDISPATCH);
126 case VT_VARIANT: return sizeof(VARIANT);
127 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
128 case VT_DECIMAL: return sizeof(DECIMAL);
129 /* Note: Return a non-zero size to indicate vt is valid. The actual size
130 * of a UDT is taken from the result of IRecordInfo_GetSize().
132 case VT_RECORD: return 32;
134 return 0;
137 /* Set the hidden data for an array */
138 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
140 /* Implementation data is stored in the 4 bytes before the header */
141 LPDWORD lpDw = (LPDWORD)psa;
142 lpDw[-1] = dw;
145 /* Get the hidden data from an array */
146 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
148 LPDWORD lpDw = (LPDWORD)psa;
149 return lpDw[-1];
152 /* Get the number of cells in a SafeArray */
153 static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
155 SAFEARRAYBOUND* psab = psa->rgsabound;
156 USHORT cCount = psa->cDims;
157 ULONG ulNumCells = 1;
159 while (cCount--)
161 /* This is a valid bordercase. See testcases. -Marcus */
162 if (!psab->cElements)
163 return 0;
164 ulNumCells *= psab->cElements;
165 psab++;
167 return ulNumCells;
170 /* Get the 0 based index of an index into a dimension */
171 static inline ULONG SAFEARRAY_GetDimensionIndex(SAFEARRAYBOUND *psab, ULONG ulIndex)
173 return ulIndex - psab->lLbound;
176 /* Get the size of a dimension in cells */
177 static inline ULONG SAFEARRAY_GetDimensionCells(SAFEARRAY *psa, ULONG ulDim)
179 ULONG size = psa->rgsabound[0].cElements;
181 while (ulDim)
183 size *= psa->rgsabound[ulDim].cElements;
184 ulDim--;
186 return size;
189 /* Allocate a descriptor for an array */
190 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
192 *ppsaOut = (SAFEARRAY*)((char*)SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE) + SAFEARRAY_HIDDEN_SIZE);
194 if (!*ppsaOut)
195 return E_UNEXPECTED;
197 return S_OK;
200 /* Set the features of an array */
201 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
203 /* Set the IID if we have one, otherwise set the type */
204 if (vt == VT_DISPATCH)
206 psa->fFeatures = FADF_HAVEIID;
207 SafeArraySetIID(psa, &IID_IDispatch);
209 else if (vt == VT_UNKNOWN)
211 psa->fFeatures = FADF_HAVEIID;
212 SafeArraySetIID(psa, &IID_IUnknown);
214 else if (vt == VT_RECORD)
215 psa->fFeatures = FADF_RECORD;
216 else
218 psa->fFeatures = FADF_HAVEVARTYPE;
219 SAFEARRAY_SetHiddenDWORD(psa, vt);
223 /* Create an array */
224 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, ULONG ulSize)
226 SAFEARRAY *psa = NULL;
228 if (!rgsabound)
229 return NULL;
231 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
233 switch (vt)
235 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
236 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
237 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
238 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
241 memcpy(psa->rgsabound, rgsabound, cDims * sizeof(SAFEARRAYBOUND));
243 if (ulSize)
244 psa->cbElements = ulSize;
246 if (FAILED(SafeArrayAllocData(psa)))
248 SafeArrayDestroyDescriptor(psa);
249 psa = NULL;
252 return psa;
255 /* Create an array as a vector */
256 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
258 SAFEARRAY *psa = NULL;
260 if (ulSize || (vt == VT_RECORD))
262 /* Allocate the header and data together */
263 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
265 SAFEARRAY_SetFeatures(vt, psa);
267 psa->cDims = 1;
268 psa->fFeatures |= FADF_CREATEVECTOR;
269 psa->pvData = &psa[1]; /* Data follows the header */
270 psa->cbElements = ulSize;
271 psa->rgsabound[0].cElements = cElements;
272 psa->rgsabound[0].lLbound = lLbound;
274 switch (vt)
276 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
277 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
278 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
279 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
283 return psa;
286 /* Free data items in an array */
287 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
289 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
291 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
293 if (ulStartCell > ulCellCount) {
294 FIXME("unexpted ulcellcount %ld, start %ld\n",ulCellCount,ulStartCell);
295 return E_UNEXPECTED;
298 ulCellCount -= ulStartCell;
300 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
302 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell * psa->cbElements;
304 while(ulCellCount--)
306 if (*lpUnknown)
307 IUnknown_Release(*lpUnknown);
308 lpUnknown++;
311 else if (psa->fFeatures & (FADF_RECORD))
313 IRecordInfo *lpRecInfo;
315 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
317 PBYTE pRecordData = (PBYTE)psa->pvData;
318 while(ulCellCount--)
320 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
321 pRecordData += psa->cbElements;
323 IRecordInfo_Release(lpRecInfo);
326 else if (psa->fFeatures & FADF_BSTR)
328 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell * psa->cbElements;
330 while(ulCellCount--)
332 if (*lpBstr)
333 SysFreeString(*lpBstr);
334 lpBstr++;
337 else if (psa->fFeatures & FADF_VARIANT)
339 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell * psa->cbElements;
341 while(ulCellCount--)
343 VariantClear(lpVariant);
344 lpVariant++;
348 return S_OK;
351 /* Copy data items from one array to another */
352 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
354 if (!psa->pvData || !dest->pvData || psa->fFeatures & FADF_DATADELETED)
355 return E_INVALIDARG;
356 else
358 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
360 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
361 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
363 if (psa->fFeatures & FADF_VARIANT)
365 VARIANT* lpVariant = (VARIANT*)psa->pvData;
366 VARIANT* lpDest = (VARIANT*)dest->pvData;
368 while(ulCellCount--)
370 VariantCopy(lpDest, lpVariant);
371 lpVariant++;
372 lpDest++;
375 else if (psa->fFeatures & FADF_BSTR)
377 BSTR* lpBstr = (BSTR*)psa->pvData;
378 BSTR* lpDest = (BSTR*)dest->pvData;
380 while(ulCellCount--)
382 if (*lpBstr)
384 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
385 if (!*lpDest)
386 return E_OUTOFMEMORY;
388 else
389 *lpDest = NULL;
390 lpBstr++;
391 lpDest++;
394 else
396 /* Copy the data over */
397 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
399 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
401 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)dest->pvData;
403 while(ulCellCount--)
405 if (*lpUnknown)
406 IUnknown_AddRef(*lpUnknown);
407 lpUnknown++;
412 if (psa->fFeatures & FADF_RECORD)
414 IRecordInfo* pRecInfo = NULL;
416 SafeArrayGetRecordInfo(psa, &pRecInfo);
417 SafeArraySetRecordInfo(dest, pRecInfo);
419 if (pRecInfo)
421 /* Release because Get() adds a reference */
422 IRecordInfo_Release(pRecInfo);
425 else if (psa->fFeatures & FADF_HAVEIID)
427 GUID guid;
428 SafeArrayGetIID(psa, &guid);
429 SafeArraySetIID(dest, &guid);
431 else if (psa->fFeatures & FADF_HAVEVARTYPE)
433 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
436 return S_OK;
439 /*************************************************************************
440 * SafeArrayAllocDescriptor (OLEAUT32.36)
442 * Allocate and initialise a descriptor for a SafeArray.
444 * PARAMS
445 * cDims [I] Number of dimensions of the array
446 * ppsaOut [O] Destination for new descriptor
448 * RETURNS
449 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
450 * Failure: An HRESULT error code indicating the error.
452 * NOTES
453 * See SafeArray.
455 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
457 LONG allocSize;
459 TRACE("(%d,%p)\n", cDims, ppsaOut);
461 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
462 return E_INVALIDARG;
464 if (!ppsaOut)
465 return E_POINTER;
467 /* We need enough space for the header and its bounds */
468 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
470 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
471 return E_UNEXPECTED;
473 (*ppsaOut)->cDims = cDims;
475 TRACE("(%d): %lu bytes allocated for descriptor.\n", cDims, allocSize);
476 return S_OK;
479 /*************************************************************************
480 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
482 * Allocate and initialise a descriptor for a SafeArray of a given type.
484 * PARAMS
485 * vt [I] The type of items to store in the array
486 * cDims [I] Number of dimensions of the array
487 * ppsaOut [O] Destination for new descriptor
489 * RETURNS
490 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
491 * Failure: An HRESULT error code indicating the error.
493 * NOTES
494 * - This function does not chack that vt is an allowed VARTYPE.
495 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
496 * See SafeArray.
498 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
500 ULONG cbElements;
501 HRESULT hRet = E_UNEXPECTED;
503 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
505 cbElements = SAFEARRAY_GetVTSize(vt);
506 if (!cbElements)
507 WARN("Creating a descriptor with an invalid VARTYPE!\n");
509 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
511 if (SUCCEEDED(hRet))
513 SAFEARRAY_SetFeatures(vt, *ppsaOut);
514 (*ppsaOut)->cbElements = cbElements;
516 return hRet;
519 /*************************************************************************
520 * SafeArrayAllocData (OLEAUT32.37)
522 * Allocate the data area of a SafeArray.
524 * PARAMS
525 * psa [I] SafeArray to allocate the data area of.
527 * RETURNS
528 * Success: S_OK. The data area is allocated and initialised.
529 * Failure: An HRESULT error code indicating the error.
531 * NOTES
532 * See SafeArray.
534 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
536 HRESULT hRet = E_INVALIDARG;
538 TRACE("(%p)\n", psa);
540 if (psa)
542 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
544 hRet = E_OUTOFMEMORY;
546 if (psa->cbElements)
548 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
550 if (psa->pvData)
552 hRet = S_OK;
553 TRACE("%lu bytes allocated for data at %p (%lu objects).\n",
554 ulSize * psa->cbElements, psa->pvData, ulSize);
558 return hRet;
561 /*************************************************************************
562 * SafeArrayCreate (OLEAUT32.15)
564 * Create a new SafeArray.
566 * PARAMS
567 * vt [I] Type to store in the safe array
568 * cDims [I] Number of array dimensions
569 * rgsabound [I] Bounds of the array dimensions
571 * RETURNS
572 * Success: A pointer to a new array object.
573 * Failure: NULL, if any parameter is invalid or memory allocation fails.
575 * NOTES
576 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
577 * in the Wine implementation.
578 * See SafeArray.
580 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
582 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
584 if (vt == VT_RECORD)
585 return NULL;
587 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
590 /*************************************************************************
591 * SafeArrayCreateEx (OLEAUT32.15)
593 * Create a new SafeArray.
595 * PARAMS
596 * vt [I] Type to store in the safe array
597 * cDims [I] Number of array dimensions
598 * rgsabound [I] Bounds of the array dimensions
599 * pvExtra [I] Extra data
601 * RETURNS
602 * Success: A pointer to a new array object.
603 * Failure: NULL, if any parameter is invalid or memory allocation fails.
605 * NOTES
606 * See SafeArray.
608 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
610 ULONG ulSize = 0;
611 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
612 SAFEARRAY* psa;
614 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
616 if (vt == VT_RECORD)
618 if (!iRecInfo)
619 return NULL;
620 IRecordInfo_GetSize(iRecInfo, &ulSize);
622 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
624 if (pvExtra)
626 switch(vt)
628 case VT_RECORD:
629 SafeArraySetRecordInfo(psa, pvExtra);
630 break;
631 case VT_UNKNOWN:
632 case VT_DISPATCH:
633 SafeArraySetIID(psa, pvExtra);
634 break;
637 return psa;
640 /************************************************************************
641 * SafeArrayCreateVector (OLEAUT32.411)
643 * Create a one dimensional, contigous SafeArray.
645 * PARAMS
646 * vt [I] Type to store in the safe array
647 * lLbound [I] Lower bound of the array
648 * cElements [I] Number of elements in the array
650 * RETURNS
651 * Success: A pointer to a new array object.
652 * Failure: NULL, if any parameter is invalid or memory allocation fails.
654 * NOTES
655 * See SafeArray.
657 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
659 TRACE("(%d->%s,%ld,%ld\n", vt, debugstr_vt(vt), lLbound, cElements);
661 if (vt == VT_RECORD)
662 return NULL;
664 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
667 /************************************************************************
668 * SafeArrayCreateVectorEx (OLEAUT32.411)
670 * Create a one dimensional, contigous SafeArray.
672 * PARAMS
673 * vt [I] Type to store in the safe array
674 * lLbound [I] Lower bound of the array
675 * cElements [I] Number of elements in the array
676 * pvExtra [I] Extra data
678 * RETURNS
679 * Success: A pointer to a new array object.
680 * Failure: NULL, if any parameter is invalid or memory allocation fails.
682 * NOTES
683 * See SafeArray.
685 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
687 ULONG ulSize;
688 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
689 SAFEARRAY* psa;
691 TRACE("(%d->%s,%ld,%ld,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
693 if (vt == VT_RECORD)
695 if (!iRecInfo)
696 return NULL;
697 IRecordInfo_GetSize(iRecInfo, &ulSize);
699 else
700 ulSize = SAFEARRAY_GetVTSize(vt);
702 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
704 if (pvExtra)
706 switch(vt)
708 case VT_RECORD:
709 SafeArraySetRecordInfo(psa, iRecInfo);
710 break;
711 case VT_UNKNOWN:
712 case VT_DISPATCH:
713 SafeArraySetIID(psa, pvExtra);
714 break;
717 return psa;
720 /*************************************************************************
721 * SafeArrayDestroyDescriptor (OLEAUT32.38)
723 * Destroy a SafeArray.
725 * PARAMS
726 * psa [I] SafeArray to destroy.
728 * RETURNS
729 * Success: S_OK. The resources used by the array are freed.
730 * Failure: An HRESULT error code indicating the error.
732 * NOTES
733 * See SafeArray.
735 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
737 TRACE("(%p)\n", psa);
739 if (psa)
741 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
743 if (psa->cLocks)
744 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
746 if (psa->fFeatures & FADF_RECORD)
747 SafeArraySetRecordInfo(psa, NULL);
749 if (psa->fFeatures & FADF_CREATEVECTOR &&
750 !(psa->fFeatures & FADF_DATADELETED))
751 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
753 if (!SAFEARRAY_Free(lpv))
754 return E_UNEXPECTED;
756 return S_OK;
759 /*************************************************************************
760 * SafeArrayLock (OLEAUT32.21)
762 * Increment the lock counter of a SafeArray.
764 * PARAMS
765 * psa [O] SafeArray to lock
767 * RETURNS
768 * Success: S_OK. The array lock is incremented.
769 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
770 * are held on the array at once.
772 * NOTES
773 * In Win32 these locks are not thread safe.
774 * See SafeArray.
776 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
778 ULONG ulLocks;
780 TRACE("(%p)\n", psa);
782 if (!psa)
783 return E_INVALIDARG;
785 ulLocks = InterlockedIncrement(&psa->cLocks);
787 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
789 WARN("Out of locks!\n");
790 InterlockedDecrement(&psa->cLocks);
791 return E_UNEXPECTED;
793 return S_OK;
796 /*************************************************************************
797 * SafeArrayUnlock (OLEAUT32.22)
799 * Decrement the lock counter of a SafeArray.
801 * PARAMS
802 * psa [O] SafeArray to unlock
804 * RETURNS
805 * Success: S_OK. The array lock is decremented.
806 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
807 * held on the array.
809 * NOTES
810 * See SafeArray.
812 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
814 TRACE("(%p)\n", psa);
816 if (!psa)
817 return E_INVALIDARG;
819 if ((LONG)InterlockedDecrement(&psa->cLocks) < 0)
821 WARN("Unlocked but no lock held!\n");
822 InterlockedIncrement(&psa->cLocks);
823 return E_UNEXPECTED;
825 return S_OK;
828 /*************************************************************************
829 * SafeArrayPutElement (OLEAUT32.26)
831 * Put an item into a SafeArray.
833 * PARAMS
834 * psa [I] SafeArray to insert into
835 * rgIndices [I] Indices to insert at
836 * pvData [I] Data to insert
838 * RETURNS
839 * Success: S_OK. The item is inserted
840 * Failure: An HRESULT error code indicating the error.
842 * NOTES
843 * See SafeArray.
845 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
847 HRESULT hRet;
849 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
851 if (!psa || !rgIndices)
852 return E_INVALIDARG;
854 if (!pvData)
856 ERR("Invalid pvData would crash under Win32!\n");
857 return E_INVALIDARG;
860 hRet = SafeArrayLock(psa);
862 if (SUCCEEDED(hRet))
864 PVOID lpvDest;
866 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
868 if (SUCCEEDED(hRet))
870 if (psa->fFeatures & FADF_VARIANT)
872 VARIANT* lpVariant = (VARIANT*)pvData;
873 VARIANT* lpDest = (VARIANT*)lpvDest;
875 VariantClear(lpDest);
876 VariantCopy(lpDest, lpVariant);
878 else if (psa->fFeatures & FADF_BSTR)
880 BSTR lpBstr = (BSTR)pvData;
881 BSTR* lpDest = (BSTR*)lpvDest;
883 if (*lpDest)
884 SysFreeString(*lpDest);
886 if (lpBstr)
888 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
889 if (!*lpDest)
890 hRet = E_OUTOFMEMORY;
892 else
893 *lpDest = NULL;
895 else
897 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
899 LPUNKNOWN lpUnknown = (LPUNKNOWN)pvData;
900 LPUNKNOWN *lpDest = (LPUNKNOWN *)lpvDest;
902 if (lpUnknown)
903 IUnknown_AddRef(lpUnknown);
904 if (*lpDest)
905 IUnknown_Release(*lpDest);
906 *lpDest = lpUnknown;
907 } else {
908 /* Copy the data over */
909 memcpy(lpvDest, pvData, psa->cbElements);
913 SafeArrayUnlock(psa);
915 return hRet;
919 /*************************************************************************
920 * SafeArrayGetElement (OLEAUT32.25)
922 * Get an item from a SafeArray.
924 * PARAMS
925 * psa [I] SafeArray to get from
926 * rgIndices [I] Indices to get from
927 * pvData [O] Destination for data
929 * RETURNS
930 * Success: S_OK. The item data is returned in pvData.
931 * Failure: An HRESULT error code indicating the error.
933 * NOTES
934 * See SafeArray.
936 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
938 HRESULT hRet;
940 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
942 if (!psa || !rgIndices || !pvData)
943 return E_INVALIDARG;
945 hRet = SafeArrayLock(psa);
947 if (SUCCEEDED(hRet))
949 PVOID lpvSrc;
951 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
953 if (SUCCEEDED(hRet))
955 if (psa->fFeatures & FADF_VARIANT)
957 VARIANT* lpVariant = (VARIANT*)lpvSrc;
958 VARIANT* lpDest = (VARIANT*)pvData;
960 VariantCopy(lpDest, lpVariant);
962 else if (psa->fFeatures & FADF_BSTR)
964 BSTR* lpBstr = (BSTR*)lpvSrc;
965 BSTR* lpDest = (BSTR*)pvData;
967 if (*lpBstr)
969 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
970 if (!*lpBstr)
971 hRet = E_OUTOFMEMORY;
973 else
974 *lpDest = NULL;
976 else
978 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
980 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)lpvSrc;
982 if (*lpUnknown)
983 IUnknown_AddRef(*lpUnknown);
985 /* Copy the data over */
986 memcpy(pvData, lpvSrc, psa->cbElements);
989 SafeArrayUnlock(psa);
991 return hRet;
994 /*************************************************************************
995 * SafeArrayGetUBound (OLEAUT32.19)
997 * Get the upper bound for a given SafeArray dimension
999 * PARAMS
1000 * psa [I] Array to get dimension upper bound from
1001 * nDim [I] The dimension number to get the upper bound of
1002 * plUbound [O] Destination for the upper bound
1004 * RETURNS
1005 * Success: S_OK. plUbound contains the dimensions upper bound.
1006 * Failure: An HRESULT error code indicating the error.
1008 * NOTES
1009 * See SafeArray.
1011 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
1013 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1015 if (!psa || !plUbound)
1016 return E_INVALIDARG;
1018 if(!nDim || nDim > psa->cDims)
1019 return DISP_E_BADINDEX;
1021 *plUbound = psa->rgsabound[nDim - 1].lLbound +
1022 psa->rgsabound[nDim - 1].cElements - 1;
1024 return S_OK;
1027 /*************************************************************************
1028 * SafeArrayGetLBound (OLEAUT32.20)
1030 * Get the lower bound for a given SafeArray dimension
1032 * PARAMS
1033 * psa [I] Array to get dimension lower bound from
1034 * nDim [I] The dimension number to get the lowe bound of
1035 * plLbound [O] Destination for the lower bound
1037 * RETURNS
1038 * Success: S_OK. plUbound contains the dimensions lower bound.
1039 * Failure: An HRESULT error code indicating the error.
1041 * NOTES
1042 * See SafeArray.
1044 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1046 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1048 if (!psa || !plLbound)
1049 return E_INVALIDARG;
1051 if(!nDim || nDim > psa->cDims)
1052 return DISP_E_BADINDEX;
1054 *plLbound = psa->rgsabound[nDim - 1].lLbound;
1055 return S_OK;
1058 /*************************************************************************
1059 * SafeArrayGetDim (OLEAUT32.17)
1061 * Get the number of dimensions in a SafeArray.
1063 * PARAMS
1064 * psa [I] Array to get the dimensions of
1066 * RETURNS
1067 * The number of array dimensions in psa, or 0 if psa is NULL.
1069 * NOTES
1070 * See SafeArray.
1072 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1074 TRACE("(%p) returning %ld\n", psa, psa ? psa->cDims : 0ul);
1075 return psa ? psa->cDims : 0;
1078 /*************************************************************************
1079 * SafeArrayGetElemsize (OLEAUT32.18)
1081 * Get the size of an element in a SafeArray.
1083 * PARAMS
1084 * psa [I] Array to get the element size from
1086 * RETURNS
1087 * The size of a single element in psa, or 0 if psa is NULL.
1089 * NOTES
1090 * See SafeArray.
1092 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1094 TRACE("(%p) returning %ld\n", psa, psa ? psa->cbElements : 0ul);
1095 return psa ? psa->cbElements : 0;
1098 /*************************************************************************
1099 * SafeArrayAccessData (OLEAUT32.23)
1101 * Lock a SafeArray and return a pointer to its data.
1103 * PARAMS
1104 * psa [I] Array to get the data pointer from
1105 * ppvData [O] Destination for the arrays data pointer
1107 * RETURNS
1108 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1109 * is locked.
1110 * Failure: An HRESULT error code indicating the error.
1112 * NOTES
1113 * See SafeArray.
1115 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1117 TRACE("(%p,%p)\n", psa, ppvData);
1119 if(!psa || !ppvData)
1120 return E_INVALIDARG;
1122 if (SUCCEEDED(SafeArrayLock(psa)))
1124 *ppvData = psa->pvData;
1125 return S_OK;
1127 *ppvData = NULL;
1128 return E_UNEXPECTED;
1132 /*************************************************************************
1133 * SafeArrayUnaccessData (OLEAUT32.24)
1135 * Unlock a SafeArray after accessing its data.
1137 * PARAMS
1138 * psa [I] Array to unlock
1140 * RETURNS
1141 * Success: S_OK. The array is unlocked.
1142 * Failure: An HRESULT error code indicating the error.
1144 * NOTES
1145 * See SafeArray.
1147 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1149 TRACE("(%p)\n", psa);
1150 return SafeArrayUnlock(psa);
1153 /************************************************************************
1154 * SafeArrayPtrOfIndex (OLEAUT32.148)
1156 * Get the address of an item in a SafeArray.
1158 * PARAMS
1159 * psa [I] Array to get the items address from
1160 * rgIndices [I] Index of the item in the array
1161 * ppvData [O] Destination for item address
1163 * RETURNS
1164 * Success: S_OK. ppvData contains a pointer to the item.
1165 * Failure: An HRESULT error code indicating the error.
1167 * NOTES
1168 * This function does not lock the array.
1170 * NOTES
1171 * See SafeArray.
1173 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1175 USHORT dim;
1176 ULONG cell = 0, dimensionSize = 1;
1177 SAFEARRAYBOUND* psab;
1178 LONG c1;
1180 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1182 /* The general formula for locating the cell number of an entry in an n
1183 * dimensional array (where cn = coordinate in dimension dn) is:
1185 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1187 * We calculate the size of the last dimension at each step through the
1188 * dimensions to avoid recursing to calculate the last dimensions size.
1190 if (!psa || !rgIndices || !ppvData)
1191 return E_INVALIDARG;
1193 psab = psa->rgsabound;
1194 c1 = *rgIndices++;
1196 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1197 return DISP_E_BADINDEX; /* Initial index out of bounds */
1199 for (dim = 1; dim < psa->cDims; dim++)
1201 dimensionSize *= psab->cElements;
1203 psab++;
1205 if (!psab->cElements ||
1206 *rgIndices < psab->lLbound ||
1207 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1208 return DISP_E_BADINDEX; /* Index out of bounds */
1210 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1211 rgIndices++;
1214 cell += (c1 - psa->rgsabound[0].lLbound);
1216 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1217 return S_OK;
1220 /************************************************************************
1221 * SafeArrayDestroyData (OLEAUT32.39)
1223 * Destroy the data associated with a SafeArray.
1225 * PARAMS
1226 * psa [I] Array to delete the data from
1228 * RETURNS
1229 * Success: S_OK. All items and the item data are freed.
1230 * Failure: An HRESULT error code indicating the error.
1232 * NOTES
1233 * See SafeArray.
1235 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1237 TRACE("(%p)\n", psa);
1239 if (!psa)
1240 return E_INVALIDARG;
1242 if (psa->cLocks)
1243 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1245 if (psa->pvData)
1247 /* Delete the actual item data */
1248 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1249 return E_UNEXPECTED;
1251 /* If this is not a vector, free the data memory block */
1252 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1254 if (!SAFEARRAY_Free(psa->pvData))
1255 return E_UNEXPECTED;
1256 psa->pvData = NULL;
1258 else
1259 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1262 return S_OK;
1265 /************************************************************************
1266 * SafeArrayCopyData (OLEAUT32.412)
1268 * Copy all data from one SafeArray to another.
1270 * PARAMS
1271 * psaSource [I] Source for copy
1272 * psaTarget [O] Destination for copy
1274 * RETURNS
1275 * Success: S_OK. psaTarget contains a copy of psaSource.
1276 * Failure: An HRESULT error code indicating the error.
1278 * NOTES
1279 * The two arrays must have the same number of dimensions and elements.
1281 * NOTES
1282 * See SafeArray.
1284 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1286 int dim;
1288 TRACE("(%p,%p)\n", psaSource, psaTarget);
1290 if (!psaSource || !psaTarget ||
1291 psaSource->cDims != psaTarget->cDims ||
1292 psaSource->cbElements != psaTarget->cbElements)
1293 return E_INVALIDARG;
1295 /* Each dimension must be the same size */
1296 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1297 if (psaSource->rgsabound[dim].cElements !=
1298 psaTarget->rgsabound[dim].cElements)
1299 return E_INVALIDARG;
1301 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1302 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1303 return S_OK;
1304 return E_UNEXPECTED;
1307 /************************************************************************
1308 * SafeArrayDestroy (OLEAUT32.16)
1310 * Destroy a SafeArray.
1312 * PARAMS
1313 * psa [I] Array to destroy
1315 * RETURNS
1316 * Success: S_OK. All resources used by the array are freed.
1317 * Failure: An HRESULT error code indicating the error.
1319 * NOTES
1320 * See SafeArray.
1322 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1324 TRACE("(%p)\n", psa);
1326 if(!psa)
1327 return E_INVALIDARG;
1329 if(psa->cLocks > 0)
1330 return DISP_E_ARRAYISLOCKED;
1332 /* Native doesn't check to see if the free succeeds */
1333 SafeArrayDestroyData(psa);
1334 SafeArrayDestroyDescriptor(psa);
1335 return S_OK;
1338 /************************************************************************
1339 * SafeArrayCopy (OLEAUT32.27)
1341 * Make a duplicate of a SafeArray.
1343 * PARAMS
1344 * psa [I] Source for copy
1345 * ppsaOut [O] Destination for new copy
1347 * RETURNS
1348 * Success: S_OK. ppsaOut contains a copy of the array.
1349 * Failure: An HRESULT error code indicating the error.
1351 * NOTES
1352 * See SafeArray.
1354 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1356 HRESULT hRet;
1358 TRACE("(%p,%p)\n", psa, ppsaOut);
1360 if (!ppsaOut)
1361 return E_INVALIDARG;
1363 *ppsaOut = NULL;
1365 if (!psa)
1366 return S_OK; /* Handles copying of NULL arrays */
1368 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1370 VARTYPE vt;
1371 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1372 hRet = E_UNEXPECTED;
1373 else
1374 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1376 else
1378 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1379 if (SUCCEEDED(hRet))
1381 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1382 (*ppsaOut)->cbElements = psa->cbElements;
1386 if (SUCCEEDED(hRet))
1388 /* Copy dimension bounds */
1389 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1391 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1393 if ((*ppsaOut)->pvData)
1395 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1397 if (SUCCEEDED(hRet))
1398 return hRet;
1400 SAFEARRAY_Free((*ppsaOut)->pvData);
1402 SafeArrayDestroyDescriptor(*ppsaOut);
1404 *ppsaOut = NULL;
1405 return hRet;
1408 /************************************************************************
1409 * SafeArrayRedim (OLEAUT32.40)
1411 * Changes the characteristics of the last dimension of a SafeArray
1413 * PARAMS
1414 * psa [I] Array to change
1415 * psabound [I] New bound details for the last dimension
1417 * RETURNS
1418 * Success: S_OK. psa is updated to reflect the new bounds.
1419 * Failure: An HRESULT error code indicating the error.
1421 * NOTES
1422 * See SafeArray.
1424 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1426 SAFEARRAYBOUND *oldBounds;
1428 TRACE("(%p,%p)\n", psa, psabound);
1430 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1431 return E_INVALIDARG;
1433 if (psa->cLocks > 0)
1434 return DISP_E_ARRAYISLOCKED;
1436 if (FAILED(SafeArrayLock(psa)))
1437 return E_UNEXPECTED;
1439 oldBounds = &psa->rgsabound[psa->cDims - 1];
1440 oldBounds->lLbound = psabound->lLbound;
1442 if (psabound->cElements != oldBounds->cElements)
1444 if (psabound->cElements < oldBounds->cElements)
1446 /* Shorten the final dimension. */
1447 ULONG ulStartCell = psa->cDims == 1 ? 0 : SAFEARRAY_GetDimensionCells(psa, psa->cDims - 1);
1449 ulStartCell += psabound->cElements;
1450 SAFEARRAY_DestroyData(psa, ulStartCell);
1452 else
1454 /* Lengthen the final dimension */
1455 ULONG ulOldSize, ulNewSize;
1456 PVOID pvNewData;
1458 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1459 if (ulOldSize)
1460 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1461 else {
1462 int oldelems = oldBounds->cElements;
1463 oldBounds->cElements = psabound->cElements;
1464 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1465 oldBounds->cElements = oldelems;
1468 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1470 SafeArrayUnlock(psa);
1471 return E_UNEXPECTED;
1474 memcpy(pvNewData, psa->pvData, ulOldSize);
1475 SAFEARRAY_Free(psa->pvData);
1476 psa->pvData = pvNewData;
1478 oldBounds->cElements = psabound->cElements;
1481 SafeArrayUnlock(psa);
1482 return S_OK;
1485 /************************************************************************
1486 * SafeArrayGetVartype (OLEAUT32.77)
1488 * Get the type of the items in a SafeArray.
1490 * PARAMS
1491 * psa [I] Array to get the type from
1492 * pvt [O] Destination for the type
1494 * RETURNS
1495 * Success: S_OK. pvt contains the type of the items.
1496 * Failure: An HRESULT error code indicating the error.
1498 * NOTES
1499 * See SafeArray.
1501 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1503 TRACE("(%p,%p)\n", psa, pvt);
1505 if (!psa || !pvt)
1506 return E_INVALIDARG;
1508 if (psa->fFeatures & FADF_RECORD)
1509 *pvt = VT_RECORD;
1510 else if (psa->fFeatures & FADF_HAVEIID)
1511 *pvt = VT_UNKNOWN;
1512 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1514 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1515 *pvt = vt;
1517 else
1518 return E_INVALIDARG;
1520 return S_OK;
1523 /************************************************************************
1524 * SafeArraySetRecordInfo (OLEAUT32.@)
1526 * Set the record info for a SafeArray.
1528 * PARAMS
1529 * psa [I] Array to set the record info for
1530 * pRinfo [I] Record info
1532 * RETURNS
1533 * Success: S_OK. The record info is stored with the array.
1534 * Failure: An HRESULT error code indicating the error.
1536 * NOTES
1537 * See SafeArray.
1539 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1541 IRecordInfo** dest = (IRecordInfo**)psa;
1543 TRACE("(%p,%p)\n", psa, pRinfo);
1545 if (!psa || !(psa->fFeatures & FADF_RECORD))
1546 return E_INVALIDARG;
1548 if (pRinfo)
1549 IRecordInfo_AddRef(pRinfo);
1551 if (dest[-1])
1552 IRecordInfo_Release(dest[-1]);
1554 dest[-1] = pRinfo;
1555 return S_OK;
1558 /************************************************************************
1559 * SafeArrayGetRecordInfo (OLEAUT32.@)
1561 * Get the record info from a SafeArray.
1563 * PARAMS
1564 * psa [I] Array to get the record info from
1565 * pRinfo [O] Destination for the record info
1567 * RETURNS
1568 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1569 * Failure: An HRESULT error code indicating the error.
1571 * NOTES
1572 * See SafeArray.
1574 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1576 IRecordInfo** src = (IRecordInfo**)psa;
1578 TRACE("(%p,%p)\n", psa, pRinfo);
1580 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1581 return E_INVALIDARG;
1583 *pRinfo = src[-1];
1585 if (*pRinfo)
1586 IRecordInfo_AddRef(*pRinfo);
1587 return S_OK;
1590 /************************************************************************
1591 * SafeArraySetIID (OLEAUT32.@)
1593 * Set the IID for a SafeArray.
1595 * PARAMS
1596 * psa [I] Array to set the IID from
1597 * guid [I] IID
1599 * RETURNS
1600 * Success: S_OK. The IID is stored with the array
1601 * Failure: An HRESULT error code indicating the error.
1603 * NOTES
1604 * See SafeArray.
1606 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1608 GUID* dest = (GUID*)psa;
1610 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1612 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1613 return E_INVALIDARG;
1615 dest[-1] = *guid;
1616 return S_OK;
1619 /************************************************************************
1620 * SafeArrayGetIID (OLEAUT32.@)
1622 * Get the IID from a SafeArray.
1624 * PARAMS
1625 * psa [I] Array to get the ID from
1626 * pGuid [O] Destination for the IID
1628 * RETURNS
1629 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1630 * Failure: An HRESULT error code indicating the error.
1632 * NOTES
1633 * See SafeArray.
1635 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1637 GUID* src = (GUID*)psa;
1639 TRACE("(%p,%p)\n", psa, pGuid);
1641 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1642 return E_INVALIDARG;
1644 *pGuid = src[-1];
1645 return S_OK;
1648 /************************************************************************
1649 * VectorFromBstr (OLEAUT32.@)
1651 * Create a SafeArray Vector from the bytes of a BSTR.
1653 * PARAMS
1654 * bstr [I] String to get bytes from
1655 * ppsa [O] Destination for the array
1657 * RETURNS
1658 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1659 * Failure: An HRESULT error code indicating the error.
1661 * NOTES
1662 * See SafeArray.
1664 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1666 SAFEARRAYBOUND sab;
1668 TRACE("(%p,%p)\n", bstr, ppsa);
1670 if (!ppsa)
1671 return E_INVALIDARG;
1673 sab.lLbound = 0;
1674 sab.cElements = SysStringByteLen(bstr);
1676 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1678 if (*ppsa)
1680 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1681 return S_OK;
1683 return E_OUTOFMEMORY;
1686 /************************************************************************
1687 * BstrFromVector (OLEAUT32.@)
1689 * Create a BSTR from a SafeArray.
1691 * PARAMS
1692 * psa [I] Source array
1693 * pbstr [O] Destination for output BSTR
1695 * RETURNS
1696 * Success: S_OK. pbstr contains the arrays data.
1697 * Failure: An HRESULT error code indicating the error.
1699 * NOTES
1700 * psa must be a 1 dimensional array of a 1 byte type.
1702 * NOTES
1703 * See SafeArray.
1705 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1707 TRACE("(%p,%p)\n", psa, pbstr);
1709 if (!pbstr)
1710 return E_INVALIDARG;
1712 *pbstr = NULL;
1714 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1715 return E_INVALIDARG;
1717 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1718 if (!*pbstr)
1719 return E_OUTOFMEMORY;
1720 return S_OK;