Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / oleaut32 / safearray.c
blobf8f87ebd8b97ed1b4fcdf9b9396df2da4b8e8585
1 /*************************************************************************
2 * OLE Automation - SafeArray
4 * This file contains the implementation of the SafeArray functions.
6 * Copyright 1999 Sylvain St-Germain
7 * Copyright 2002-2003 Marcus Meissner
8 * Copyright 2003 Jon Griffiths
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 /* Memory Layout of a SafeArray:
26 * -0x10: start of memory.
27 * -0x10: GUID for VT_DISPATCH and VT_UNKNOWN safearrays (if FADF_HAVEIID)
28 * -0x04: DWORD varianttype; (for all others, except VT_RECORD) (if FADF_HAVEVARTYPE)
29 * -0x4: IRecordInfo* iface; (if FADF_RECORD, for VT_RECORD (can be NULL))
30 * 0x00: SAFEARRAY,
31 * 0x10: SAFEARRAYBOUNDS[0...]
34 #include "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 const 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 /* Allocate a descriptor for an array */
173 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
175 *ppsaOut = (SAFEARRAY*)((char*)SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE) + SAFEARRAY_HIDDEN_SIZE);
177 if (!*ppsaOut)
178 return E_UNEXPECTED;
180 return S_OK;
183 /* Set the features of an array */
184 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
186 /* Set the IID if we have one, otherwise set the type */
187 if (vt == VT_DISPATCH)
189 psa->fFeatures = FADF_HAVEIID;
190 SafeArraySetIID(psa, &IID_IDispatch);
192 else if (vt == VT_UNKNOWN)
194 psa->fFeatures = FADF_HAVEIID;
195 SafeArraySetIID(psa, &IID_IUnknown);
197 else if (vt == VT_RECORD)
198 psa->fFeatures = FADF_RECORD;
199 else
201 psa->fFeatures = FADF_HAVEVARTYPE;
202 SAFEARRAY_SetHiddenDWORD(psa, vt);
206 /* Create an array */
207 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, const SAFEARRAYBOUND *rgsabound, ULONG ulSize)
209 SAFEARRAY *psa = NULL;
210 unsigned int i;
212 if (!rgsabound)
213 return NULL;
215 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
217 switch (vt)
219 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
220 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
221 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
222 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
225 for (i = 0; i < cDims; i++)
226 memcpy(psa->rgsabound + i, rgsabound + cDims - 1 - i, sizeof(SAFEARRAYBOUND));
228 if (ulSize)
229 psa->cbElements = ulSize;
231 if (!psa->cbElements || FAILED(SafeArrayAllocData(psa)))
233 SafeArrayDestroyDescriptor(psa);
234 psa = NULL;
237 return psa;
240 /* Create an array as a vector */
241 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
243 SAFEARRAY *psa = NULL;
245 if (ulSize || (vt == VT_RECORD))
247 /* Allocate the header and data together */
248 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
250 SAFEARRAY_SetFeatures(vt, psa);
252 psa->cDims = 1;
253 psa->fFeatures |= FADF_CREATEVECTOR;
254 psa->pvData = &psa[1]; /* Data follows the header */
255 psa->cbElements = ulSize;
256 psa->rgsabound[0].cElements = cElements;
257 psa->rgsabound[0].lLbound = lLbound;
259 switch (vt)
261 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
262 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
263 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
264 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
268 return psa;
271 /* Free data items in an array */
272 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
274 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
276 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
278 if (ulStartCell > ulCellCount) {
279 FIXME("unexpted ulcellcount %d, start %d\n",ulCellCount,ulStartCell);
280 return E_UNEXPECTED;
283 ulCellCount -= ulStartCell;
285 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
287 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell;
289 while(ulCellCount--)
291 if (*lpUnknown)
292 IUnknown_Release(*lpUnknown);
293 lpUnknown++;
296 else if (psa->fFeatures & (FADF_RECORD))
298 IRecordInfo *lpRecInfo;
300 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
302 PBYTE pRecordData = psa->pvData;
303 while(ulCellCount--)
305 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
306 pRecordData += psa->cbElements;
308 IRecordInfo_Release(lpRecInfo);
311 else if (psa->fFeatures & FADF_BSTR)
313 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell;
315 while(ulCellCount--)
317 SysFreeString(*lpBstr);
318 lpBstr++;
321 else if (psa->fFeatures & FADF_VARIANT)
323 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell;
325 while(ulCellCount--)
327 HRESULT hRet = VariantClear(lpVariant);
329 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
330 lpVariant++;
334 return S_OK;
337 /* Copy data items from one array to another */
338 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
340 if (!psa->pvData)
341 return S_OK;
342 else if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
343 return E_INVALIDARG;
344 else
346 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
348 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
349 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
351 if (psa->fFeatures & FADF_VARIANT)
353 VARIANT* lpVariant = psa->pvData;
354 VARIANT* lpDest = dest->pvData;
356 while(ulCellCount--)
358 HRESULT hRet;
360 hRet = VariantCopy(lpDest, lpVariant);
361 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
362 lpVariant++;
363 lpDest++;
366 else if (psa->fFeatures & FADF_BSTR)
368 BSTR* lpBstr = psa->pvData;
369 BSTR* lpDest = dest->pvData;
371 while(ulCellCount--)
373 if (*lpBstr)
375 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
376 if (!*lpDest)
377 return E_OUTOFMEMORY;
379 else
380 *lpDest = NULL;
381 lpBstr++;
382 lpDest++;
385 else
387 /* Copy the data over */
388 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
390 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
392 LPUNKNOWN *lpUnknown = dest->pvData;
394 while(ulCellCount--)
396 if (*lpUnknown)
397 IUnknown_AddRef(*lpUnknown);
398 lpUnknown++;
403 if (psa->fFeatures & FADF_RECORD)
405 IRecordInfo* pRecInfo = NULL;
407 SafeArrayGetRecordInfo(psa, &pRecInfo);
408 SafeArraySetRecordInfo(dest, pRecInfo);
410 if (pRecInfo)
412 /* Release because Get() adds a reference */
413 IRecordInfo_Release(pRecInfo);
416 else if (psa->fFeatures & FADF_HAVEIID)
418 GUID guid;
419 SafeArrayGetIID(psa, &guid);
420 SafeArraySetIID(dest, &guid);
422 else if (psa->fFeatures & FADF_HAVEVARTYPE)
424 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
427 return S_OK;
430 /*************************************************************************
431 * SafeArrayAllocDescriptor (OLEAUT32.36)
433 * Allocate and initialise a descriptor for a SafeArray.
435 * PARAMS
436 * cDims [I] Number of dimensions of the array
437 * ppsaOut [O] Destination for new descriptor
439 * RETURNS
440 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
441 * Failure: An HRESULT error code indicating the error.
443 * NOTES
444 * See SafeArray.
446 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
448 LONG allocSize;
450 TRACE("(%d,%p)\n", cDims, ppsaOut);
452 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
453 return E_INVALIDARG;
455 if (!ppsaOut)
456 return E_POINTER;
458 /* We need enough space for the header and its bounds */
459 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
461 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
462 return E_UNEXPECTED;
464 (*ppsaOut)->cDims = cDims;
466 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims, allocSize);
467 return S_OK;
470 /*************************************************************************
471 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
473 * Allocate and initialise a descriptor for a SafeArray of a given type.
475 * PARAMS
476 * vt [I] The type of items to store in the array
477 * cDims [I] Number of dimensions of the array
478 * ppsaOut [O] Destination for new descriptor
480 * RETURNS
481 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
482 * Failure: An HRESULT error code indicating the error.
484 * NOTES
485 * - This function does not check that vt is an allowed VARTYPE.
486 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
487 * See SafeArray.
489 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
491 ULONG cbElements;
492 HRESULT hRet = E_UNEXPECTED;
494 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
496 cbElements = SAFEARRAY_GetVTSize(vt);
497 if (!cbElements)
498 WARN("Creating a descriptor with an invalid VARTYPE!\n");
500 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
502 if (SUCCEEDED(hRet))
504 SAFEARRAY_SetFeatures(vt, *ppsaOut);
505 (*ppsaOut)->cbElements = cbElements;
507 return hRet;
510 /*************************************************************************
511 * SafeArrayAllocData (OLEAUT32.37)
513 * Allocate the data area of a SafeArray.
515 * PARAMS
516 * psa [I] SafeArray to allocate the data area of.
518 * RETURNS
519 * Success: S_OK. The data area is allocated and initialised.
520 * Failure: An HRESULT error code indicating the error.
522 * NOTES
523 * See SafeArray.
525 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
527 HRESULT hRet = E_INVALIDARG;
529 TRACE("(%p)\n", psa);
531 if (psa)
533 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
535 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
537 if (psa->pvData)
539 hRet = S_OK;
540 TRACE("%u bytes allocated for data at %p (%u objects).\n",
541 ulSize * psa->cbElements, psa->pvData, ulSize);
543 else
544 hRet = E_OUTOFMEMORY;
546 return hRet;
549 /*************************************************************************
550 * SafeArrayCreate (OLEAUT32.15)
552 * Create a new SafeArray.
554 * PARAMS
555 * vt [I] Type to store in the safe array
556 * cDims [I] Number of array dimensions
557 * rgsabound [I] Bounds of the array dimensions
559 * RETURNS
560 * Success: A pointer to a new array object.
561 * Failure: NULL, if any parameter is invalid or memory allocation fails.
563 * NOTES
564 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
565 * in the Wine implementation.
566 * See SafeArray.
568 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
570 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
572 if (vt == VT_RECORD)
573 return NULL;
575 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
578 /*************************************************************************
579 * SafeArrayCreateEx (OLEAUT32.15)
581 * Create a new SafeArray.
583 * PARAMS
584 * vt [I] Type to store in the safe array
585 * cDims [I] Number of array dimensions
586 * rgsabound [I] Bounds of the array dimensions
587 * pvExtra [I] Extra data
589 * RETURNS
590 * Success: A pointer to a new array object.
591 * Failure: NULL, if any parameter is invalid or memory allocation fails.
593 * NOTES
594 * See SafeArray.
596 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
598 ULONG ulSize = 0;
599 IRecordInfo* iRecInfo = pvExtra;
600 SAFEARRAY* psa;
602 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
604 if (vt == VT_RECORD)
606 if (!iRecInfo)
607 return NULL;
608 IRecordInfo_GetSize(iRecInfo, &ulSize);
610 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
612 if (pvExtra)
614 switch(vt)
616 case VT_RECORD:
617 SafeArraySetRecordInfo(psa, pvExtra);
618 break;
619 case VT_UNKNOWN:
620 case VT_DISPATCH:
621 SafeArraySetIID(psa, pvExtra);
622 break;
625 return psa;
628 /************************************************************************
629 * SafeArrayCreateVector (OLEAUT32.411)
631 * Create a one dimensional, contiguous SafeArray.
633 * PARAMS
634 * vt [I] Type to store in the safe array
635 * lLbound [I] Lower bound of the array
636 * cElements [I] Number of elements in the array
638 * RETURNS
639 * Success: A pointer to a new array object.
640 * Failure: NULL, if any parameter is invalid or memory allocation fails.
642 * NOTES
643 * See SafeArray.
645 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
647 TRACE("(%d->%s,%d,%d\n", vt, debugstr_vt(vt), lLbound, cElements);
649 if (vt == VT_RECORD)
650 return NULL;
652 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
655 /************************************************************************
656 * SafeArrayCreateVectorEx (OLEAUT32.411)
658 * Create a one dimensional, contiguous SafeArray.
660 * PARAMS
661 * vt [I] Type to store in the safe array
662 * lLbound [I] Lower bound of the array
663 * cElements [I] Number of elements in the array
664 * pvExtra [I] Extra data
666 * RETURNS
667 * Success: A pointer to a new array object.
668 * Failure: NULL, if any parameter is invalid or memory allocation fails.
670 * NOTES
671 * See SafeArray.
673 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
675 ULONG ulSize;
676 IRecordInfo* iRecInfo = pvExtra;
677 SAFEARRAY* psa;
679 TRACE("(%d->%s,%d,%d,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
681 if (vt == VT_RECORD)
683 if (!iRecInfo)
684 return NULL;
685 IRecordInfo_GetSize(iRecInfo, &ulSize);
687 else
688 ulSize = SAFEARRAY_GetVTSize(vt);
690 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
692 if (pvExtra)
694 switch(vt)
696 case VT_RECORD:
697 SafeArraySetRecordInfo(psa, iRecInfo);
698 break;
699 case VT_UNKNOWN:
700 case VT_DISPATCH:
701 SafeArraySetIID(psa, pvExtra);
702 break;
705 return psa;
708 /*************************************************************************
709 * SafeArrayDestroyDescriptor (OLEAUT32.38)
711 * Destroy a SafeArray.
713 * PARAMS
714 * psa [I] SafeArray to destroy.
716 * RETURNS
717 * Success: S_OK. The resources used by the array are freed.
718 * Failure: An HRESULT error code indicating the error.
720 * NOTES
721 * See SafeArray.
723 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
725 TRACE("(%p)\n", psa);
727 if (psa)
729 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
731 if (psa->cLocks)
732 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
734 if (psa->fFeatures & FADF_RECORD)
735 SafeArraySetRecordInfo(psa, NULL);
737 if (psa->fFeatures & FADF_CREATEVECTOR &&
738 !(psa->fFeatures & FADF_DATADELETED))
739 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
741 if (!SAFEARRAY_Free(lpv))
742 return E_UNEXPECTED;
744 return S_OK;
747 /*************************************************************************
748 * SafeArrayLock (OLEAUT32.21)
750 * Increment the lock counter of a SafeArray.
752 * PARAMS
753 * psa [O] SafeArray to lock
755 * RETURNS
756 * Success: S_OK. The array lock is incremented.
757 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
758 * are held on the array at once.
760 * NOTES
761 * In Win32 these locks are not thread safe.
762 * See SafeArray.
764 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
766 ULONG ulLocks;
768 TRACE("(%p)\n", psa);
770 if (!psa)
771 return E_INVALIDARG;
773 ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
775 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
777 WARN("Out of locks!\n");
778 InterlockedDecrement( (LONG*) &psa->cLocks);
779 return E_UNEXPECTED;
781 return S_OK;
784 /*************************************************************************
785 * SafeArrayUnlock (OLEAUT32.22)
787 * Decrement the lock counter of a SafeArray.
789 * PARAMS
790 * psa [O] SafeArray to unlock
792 * RETURNS
793 * Success: S_OK. The array lock is decremented.
794 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
795 * held on the array.
797 * NOTES
798 * See SafeArray.
800 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
802 TRACE("(%p)\n", psa);
804 if (!psa)
805 return E_INVALIDARG;
807 if (InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
809 WARN("Unlocked but no lock held!\n");
810 InterlockedIncrement( (LONG*) &psa->cLocks);
811 return E_UNEXPECTED;
813 return S_OK;
816 /*************************************************************************
817 * SafeArrayPutElement (OLEAUT32.26)
819 * Put an item into a SafeArray.
821 * PARAMS
822 * psa [I] SafeArray to insert into
823 * rgIndices [I] Indices to insert at
824 * pvData [I] Data to insert
826 * RETURNS
827 * Success: S_OK. The item is inserted
828 * Failure: An HRESULT error code indicating the error.
830 * NOTES
831 * See SafeArray.
833 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
835 HRESULT hRet;
837 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
839 if (!psa || !rgIndices)
840 return E_INVALIDARG;
842 hRet = SafeArrayLock(psa);
844 if (SUCCEEDED(hRet))
846 PVOID lpvDest;
848 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
850 if (SUCCEEDED(hRet))
852 if (psa->fFeatures & FADF_VARIANT)
854 VARIANT* lpVariant = pvData;
855 VARIANT* lpDest = lpvDest;
857 hRet = VariantClear(lpDest);
858 if (FAILED(hRet)) FIXME("VariantClear failed with 0x%x\n", hRet);
859 hRet = VariantCopy(lpDest, lpVariant);
860 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
862 else if (psa->fFeatures & FADF_BSTR)
864 BSTR lpBstr = (BSTR)pvData;
865 BSTR* lpDest = lpvDest;
867 SysFreeString(*lpDest);
869 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
870 if (!*lpDest)
871 hRet = E_OUTOFMEMORY;
873 else
875 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
877 LPUNKNOWN lpUnknown = pvData;
878 LPUNKNOWN *lpDest = lpvDest;
880 if (lpUnknown)
881 IUnknown_AddRef(lpUnknown);
882 if (*lpDest)
883 IUnknown_Release(*lpDest);
884 *lpDest = lpUnknown;
885 } else {
886 /* Copy the data over */
887 memcpy(lpvDest, pvData, psa->cbElements);
891 SafeArrayUnlock(psa);
893 return hRet;
897 /*************************************************************************
898 * SafeArrayGetElement (OLEAUT32.25)
900 * Get an item from a SafeArray.
902 * PARAMS
903 * psa [I] SafeArray to get from
904 * rgIndices [I] Indices to get from
905 * pvData [O] Destination for data
907 * RETURNS
908 * Success: S_OK. The item data is returned in pvData.
909 * Failure: An HRESULT error code indicating the error.
911 * NOTES
912 * See SafeArray.
914 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
916 HRESULT hRet;
918 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
920 if (!psa || !rgIndices || !pvData)
921 return E_INVALIDARG;
923 hRet = SafeArrayLock(psa);
925 if (SUCCEEDED(hRet))
927 PVOID lpvSrc;
929 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
931 if (SUCCEEDED(hRet))
933 if (psa->fFeatures & FADF_VARIANT)
935 VARIANT* lpVariant = lpvSrc;
936 VARIANT* lpDest = pvData;
938 /* The original content of pvData is ignored. */
939 V_VT(lpDest) = VT_EMPTY;
940 hRet = VariantCopy(lpDest, lpVariant);
941 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
943 else if (psa->fFeatures & FADF_BSTR)
945 BSTR* lpBstr = lpvSrc;
946 BSTR* lpDest = pvData;
948 if (*lpBstr)
950 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
951 if (!*lpBstr)
952 hRet = E_OUTOFMEMORY;
954 else
955 *lpDest = NULL;
957 else
959 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
961 LPUNKNOWN *lpUnknown = lpvSrc;
963 if (*lpUnknown)
964 IUnknown_AddRef(*lpUnknown);
966 /* Copy the data over */
967 memcpy(pvData, lpvSrc, psa->cbElements);
970 SafeArrayUnlock(psa);
972 return hRet;
975 /*************************************************************************
976 * SafeArrayGetUBound (OLEAUT32.19)
978 * Get the upper bound for a given SafeArray dimension
980 * PARAMS
981 * psa [I] Array to get dimension upper bound from
982 * nDim [I] The dimension number to get the upper bound of
983 * plUbound [O] Destination for the upper bound
985 * RETURNS
986 * Success: S_OK. plUbound contains the dimensions upper bound.
987 * Failure: An HRESULT error code indicating the error.
989 * NOTES
990 * See SafeArray.
992 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
994 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
996 if (!psa || !plUbound)
997 return E_INVALIDARG;
999 if(!nDim || nDim > psa->cDims)
1000 return DISP_E_BADINDEX;
1002 *plUbound = psa->rgsabound[psa->cDims - nDim].lLbound +
1003 psa->rgsabound[psa->cDims - nDim].cElements - 1;
1005 return S_OK;
1008 /*************************************************************************
1009 * SafeArrayGetLBound (OLEAUT32.20)
1011 * Get the lower bound for a given SafeArray dimension
1013 * PARAMS
1014 * psa [I] Array to get dimension lower bound from
1015 * nDim [I] The dimension number to get the lowe bound of
1016 * plLbound [O] Destination for the lower bound
1018 * RETURNS
1019 * Success: S_OK. plUbound contains the dimensions lower bound.
1020 * Failure: An HRESULT error code indicating the error.
1022 * NOTES
1023 * See SafeArray.
1025 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1027 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1029 if (!psa || !plLbound)
1030 return E_INVALIDARG;
1032 if(!nDim || nDim > psa->cDims)
1033 return DISP_E_BADINDEX;
1035 *plLbound = psa->rgsabound[psa->cDims - nDim].lLbound;
1036 return S_OK;
1039 /*************************************************************************
1040 * SafeArrayGetDim (OLEAUT32.17)
1042 * Get the number of dimensions in a SafeArray.
1044 * PARAMS
1045 * psa [I] Array to get the dimensions of
1047 * RETURNS
1048 * The number of array dimensions in psa, or 0 if psa is NULL.
1050 * NOTES
1051 * See SafeArray.
1053 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1055 TRACE("(%p) returning %d\n", psa, psa ? psa->cDims : 0u);
1056 return psa ? psa->cDims : 0;
1059 /*************************************************************************
1060 * SafeArrayGetElemsize (OLEAUT32.18)
1062 * Get the size of an element in a SafeArray.
1064 * PARAMS
1065 * psa [I] Array to get the element size from
1067 * RETURNS
1068 * The size of a single element in psa, or 0 if psa is NULL.
1070 * NOTES
1071 * See SafeArray.
1073 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1075 TRACE("(%p) returning %d\n", psa, psa ? psa->cbElements : 0u);
1076 return psa ? psa->cbElements : 0;
1079 /*************************************************************************
1080 * SafeArrayAccessData (OLEAUT32.23)
1082 * Lock a SafeArray and return a pointer to its data.
1084 * PARAMS
1085 * psa [I] Array to get the data pointer from
1086 * ppvData [O] Destination for the arrays data pointer
1088 * RETURNS
1089 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1090 * is locked.
1091 * Failure: An HRESULT error code indicating the error.
1093 * NOTES
1094 * See SafeArray.
1096 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1098 TRACE("(%p,%p)\n", psa, ppvData);
1100 if(!psa || !ppvData)
1101 return E_INVALIDARG;
1103 if (SUCCEEDED(SafeArrayLock(psa)))
1105 *ppvData = psa->pvData;
1106 return S_OK;
1108 *ppvData = NULL;
1109 return E_UNEXPECTED;
1113 /*************************************************************************
1114 * SafeArrayUnaccessData (OLEAUT32.24)
1116 * Unlock a SafeArray after accessing its data.
1118 * PARAMS
1119 * psa [I] Array to unlock
1121 * RETURNS
1122 * Success: S_OK. The array is unlocked.
1123 * Failure: An HRESULT error code indicating the error.
1125 * NOTES
1126 * See SafeArray.
1128 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1130 TRACE("(%p)\n", psa);
1131 return SafeArrayUnlock(psa);
1134 /************************************************************************
1135 * SafeArrayPtrOfIndex (OLEAUT32.148)
1137 * Get the address of an item in a SafeArray.
1139 * PARAMS
1140 * psa [I] Array to get the items address from
1141 * rgIndices [I] Index of the item in the array
1142 * ppvData [O] Destination for item address
1144 * RETURNS
1145 * Success: S_OK. ppvData contains a pointer to the item.
1146 * Failure: An HRESULT error code indicating the error.
1148 * NOTES
1149 * This function does not lock the array.
1151 * NOTES
1152 * See SafeArray.
1154 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1156 USHORT dim;
1157 ULONG cell = 0, dimensionSize = 1;
1158 SAFEARRAYBOUND* psab;
1159 LONG c1;
1161 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1163 /* The general formula for locating the cell number of an entry in an n
1164 * dimensional array (where cn = coordinate in dimension dn) is:
1166 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1168 * We calculate the size of the last dimension at each step through the
1169 * dimensions to avoid recursing to calculate the last dimensions size.
1171 if (!psa || !rgIndices || !ppvData)
1172 return E_INVALIDARG;
1174 psab = psa->rgsabound + psa->cDims - 1;
1175 c1 = *rgIndices++;
1177 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1178 return DISP_E_BADINDEX; /* Initial index out of bounds */
1180 for (dim = 1; dim < psa->cDims; dim++)
1182 dimensionSize *= psab->cElements;
1184 psab--;
1186 if (!psab->cElements ||
1187 *rgIndices < psab->lLbound ||
1188 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1189 return DISP_E_BADINDEX; /* Index out of bounds */
1191 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1192 rgIndices++;
1195 cell += (c1 - psa->rgsabound[psa->cDims - 1].lLbound);
1197 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1198 return S_OK;
1201 /************************************************************************
1202 * SafeArrayDestroyData (OLEAUT32.39)
1204 * Destroy the data associated with a SafeArray.
1206 * PARAMS
1207 * psa [I] Array to delete the data from
1209 * RETURNS
1210 * Success: S_OK. All items and the item data are freed.
1211 * Failure: An HRESULT error code indicating the error.
1213 * NOTES
1214 * See SafeArray.
1216 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1218 TRACE("(%p)\n", psa);
1220 if (!psa)
1221 return E_INVALIDARG;
1223 if (psa->cLocks)
1224 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1226 /* Delete the actual item data */
1227 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1228 return E_UNEXPECTED;
1230 if (psa->pvData)
1232 if (psa->fFeatures & FADF_STATIC)
1234 ZeroMemory(psa->pvData, SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1235 return S_OK;
1237 /* If this is not a vector, free the data memory block */
1238 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1240 if (!SAFEARRAY_Free(psa->pvData))
1241 return E_UNEXPECTED;
1242 psa->pvData = NULL;
1244 else
1245 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1248 return S_OK;
1251 /************************************************************************
1252 * SafeArrayCopyData (OLEAUT32.412)
1254 * Copy all data from one SafeArray to another.
1256 * PARAMS
1257 * psaSource [I] Source for copy
1258 * psaTarget [O] Destination for copy
1260 * RETURNS
1261 * Success: S_OK. psaTarget contains a copy of psaSource.
1262 * Failure: An HRESULT error code indicating the error.
1264 * NOTES
1265 * The two arrays must have the same number of dimensions and elements.
1267 * NOTES
1268 * See SafeArray.
1270 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1272 int dim;
1274 TRACE("(%p,%p)\n", psaSource, psaTarget);
1276 if (!psaSource || !psaTarget ||
1277 psaSource->cDims != psaTarget->cDims ||
1278 psaSource->cbElements != psaTarget->cbElements)
1279 return E_INVALIDARG;
1281 /* Each dimension must be the same size */
1282 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1283 if (psaSource->rgsabound[dim].cElements !=
1284 psaTarget->rgsabound[dim].cElements)
1285 return E_INVALIDARG;
1287 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1288 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1289 return S_OK;
1290 return E_UNEXPECTED;
1293 /************************************************************************
1294 * SafeArrayDestroy (OLEAUT32.16)
1296 * Destroy a SafeArray.
1298 * PARAMS
1299 * psa [I] Array to destroy
1301 * RETURNS
1302 * Success: S_OK. All resources used by the array are freed.
1303 * Failure: An HRESULT error code indicating the error.
1305 * NOTES
1306 * See SafeArray.
1308 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1310 TRACE("(%p)\n", psa);
1312 if(!psa)
1313 return S_OK;
1315 if(psa->cLocks > 0)
1316 return DISP_E_ARRAYISLOCKED;
1318 /* Native doesn't check to see if the free succeeds */
1319 SafeArrayDestroyData(psa);
1320 SafeArrayDestroyDescriptor(psa);
1321 return S_OK;
1324 /************************************************************************
1325 * SafeArrayCopy (OLEAUT32.27)
1327 * Make a duplicate of a SafeArray.
1329 * PARAMS
1330 * psa [I] Source for copy
1331 * ppsaOut [O] Destination for new copy
1333 * RETURNS
1334 * Success: S_OK. ppsaOut contains a copy of the array.
1335 * Failure: An HRESULT error code indicating the error.
1337 * NOTES
1338 * See SafeArray.
1340 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1342 HRESULT hRet;
1344 TRACE("(%p,%p)\n", psa, ppsaOut);
1346 if (!ppsaOut)
1347 return E_INVALIDARG;
1349 *ppsaOut = NULL;
1351 if (!psa)
1352 return S_OK; /* Handles copying of NULL arrays */
1354 if (!psa->cbElements)
1356 ERR("not copying an array of 0 elements\n");
1357 return E_INVALIDARG;
1360 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1362 VARTYPE vt;
1363 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1364 hRet = E_UNEXPECTED;
1365 else
1366 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1368 else
1370 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1371 if (SUCCEEDED(hRet))
1373 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1374 (*ppsaOut)->cbElements = psa->cbElements;
1378 if (SUCCEEDED(hRet))
1380 /* Copy dimension bounds */
1381 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1383 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1385 if ((*ppsaOut)->pvData)
1387 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1389 if (SUCCEEDED(hRet))
1390 return hRet;
1392 SAFEARRAY_Free((*ppsaOut)->pvData);
1394 SafeArrayDestroyDescriptor(*ppsaOut);
1396 *ppsaOut = NULL;
1397 return hRet;
1400 /************************************************************************
1401 * SafeArrayRedim (OLEAUT32.40)
1403 * Changes the characteristics of the last dimension of a SafeArray
1405 * PARAMS
1406 * psa [I] Array to change
1407 * psabound [I] New bound details for the last dimension
1409 * RETURNS
1410 * Success: S_OK. psa is updated to reflect the new bounds.
1411 * Failure: An HRESULT error code indicating the error.
1413 * NOTES
1414 * See SafeArray.
1416 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1418 SAFEARRAYBOUND *oldBounds;
1420 TRACE("(%p,%p)\n", psa, psabound);
1422 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1423 return E_INVALIDARG;
1425 if (psa->cLocks > 0)
1426 return DISP_E_ARRAYISLOCKED;
1428 if (FAILED(SafeArrayLock(psa)))
1429 return E_UNEXPECTED;
1431 oldBounds = psa->rgsabound;
1432 oldBounds->lLbound = psabound->lLbound;
1434 if (psabound->cElements != oldBounds->cElements)
1436 if (psabound->cElements < oldBounds->cElements)
1438 /* Shorten the final dimension. */
1439 ULONG ulStartCell = psabound->cElements *
1440 (SAFEARRAY_GetCellCount(psa) / oldBounds->cElements);
1441 SAFEARRAY_DestroyData(psa, ulStartCell);
1443 else
1445 /* Lengthen the final dimension */
1446 ULONG ulOldSize, ulNewSize;
1447 PVOID pvNewData;
1449 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1450 if (ulOldSize)
1451 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1452 else {
1453 int oldelems = oldBounds->cElements;
1454 oldBounds->cElements = psabound->cElements;
1455 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1456 oldBounds->cElements = oldelems;
1459 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1461 SafeArrayUnlock(psa);
1462 return E_UNEXPECTED;
1465 memcpy(pvNewData, psa->pvData, ulOldSize);
1466 SAFEARRAY_Free(psa->pvData);
1467 psa->pvData = pvNewData;
1469 oldBounds->cElements = psabound->cElements;
1472 SafeArrayUnlock(psa);
1473 return S_OK;
1476 /************************************************************************
1477 * SafeArrayGetVartype (OLEAUT32.77)
1479 * Get the type of the items in a SafeArray.
1481 * PARAMS
1482 * psa [I] Array to get the type from
1483 * pvt [O] Destination for the type
1485 * RETURNS
1486 * Success: S_OK. pvt contains the type of the items.
1487 * Failure: An HRESULT error code indicating the error.
1489 * NOTES
1490 * See SafeArray.
1492 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1494 TRACE("(%p,%p)\n", psa, pvt);
1496 if (!psa || !pvt)
1497 return E_INVALIDARG;
1499 if (psa->fFeatures & FADF_RECORD)
1500 *pvt = VT_RECORD;
1501 else if ((psa->fFeatures & (FADF_HAVEIID|FADF_DISPATCH)) == (FADF_HAVEIID|FADF_DISPATCH))
1502 *pvt = VT_DISPATCH;
1503 else if (psa->fFeatures & FADF_HAVEIID)
1504 *pvt = VT_UNKNOWN;
1505 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1507 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1508 *pvt = vt;
1510 else
1511 return E_INVALIDARG;
1513 return S_OK;
1516 /************************************************************************
1517 * SafeArraySetRecordInfo (OLEAUT32.@)
1519 * Set the record info for a SafeArray.
1521 * PARAMS
1522 * psa [I] Array to set the record info for
1523 * pRinfo [I] Record info
1525 * RETURNS
1526 * Success: S_OK. The record info is stored with the array.
1527 * Failure: An HRESULT error code indicating the error.
1529 * NOTES
1530 * See SafeArray.
1532 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1534 IRecordInfo** dest = (IRecordInfo**)psa;
1536 TRACE("(%p,%p)\n", psa, pRinfo);
1538 if (!psa || !(psa->fFeatures & FADF_RECORD))
1539 return E_INVALIDARG;
1541 if (pRinfo)
1542 IRecordInfo_AddRef(pRinfo);
1544 if (dest[-1])
1545 IRecordInfo_Release(dest[-1]);
1547 dest[-1] = pRinfo;
1548 return S_OK;
1551 /************************************************************************
1552 * SafeArrayGetRecordInfo (OLEAUT32.@)
1554 * Get the record info from a SafeArray.
1556 * PARAMS
1557 * psa [I] Array to get the record info from
1558 * pRinfo [O] Destination for the record info
1560 * RETURNS
1561 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1562 * Failure: An HRESULT error code indicating the error.
1564 * NOTES
1565 * See SafeArray.
1567 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1569 IRecordInfo** src = (IRecordInfo**)psa;
1571 TRACE("(%p,%p)\n", psa, pRinfo);
1573 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1574 return E_INVALIDARG;
1576 *pRinfo = src[-1];
1578 if (*pRinfo)
1579 IRecordInfo_AddRef(*pRinfo);
1580 return S_OK;
1583 /************************************************************************
1584 * SafeArraySetIID (OLEAUT32.@)
1586 * Set the IID for a SafeArray.
1588 * PARAMS
1589 * psa [I] Array to set the IID from
1590 * guid [I] IID
1592 * RETURNS
1593 * Success: S_OK. The IID is stored with the array
1594 * Failure: An HRESULT error code indicating the error.
1596 * NOTES
1597 * See SafeArray.
1599 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1601 GUID* dest = (GUID*)psa;
1603 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1605 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1606 return E_INVALIDARG;
1608 dest[-1] = *guid;
1609 return S_OK;
1612 /************************************************************************
1613 * SafeArrayGetIID (OLEAUT32.@)
1615 * Get the IID from a SafeArray.
1617 * PARAMS
1618 * psa [I] Array to get the ID from
1619 * pGuid [O] Destination for the IID
1621 * RETURNS
1622 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1623 * Failure: An HRESULT error code indicating the error.
1625 * NOTES
1626 * See SafeArray.
1628 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1630 GUID* src = (GUID*)psa;
1632 TRACE("(%p,%p)\n", psa, pGuid);
1634 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1635 return E_INVALIDARG;
1637 *pGuid = src[-1];
1638 return S_OK;
1641 /************************************************************************
1642 * VectorFromBstr (OLEAUT32.@)
1644 * Create a SafeArray Vector from the bytes of a BSTR.
1646 * PARAMS
1647 * bstr [I] String to get bytes from
1648 * ppsa [O] Destination for the array
1650 * RETURNS
1651 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1652 * Failure: An HRESULT error code indicating the error.
1654 * NOTES
1655 * See SafeArray.
1657 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1659 SAFEARRAYBOUND sab;
1661 TRACE("(%p,%p)\n", bstr, ppsa);
1663 if (!ppsa)
1664 return E_INVALIDARG;
1666 sab.lLbound = 0;
1667 sab.cElements = SysStringByteLen(bstr);
1669 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1671 if (*ppsa)
1673 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1674 return S_OK;
1676 return E_OUTOFMEMORY;
1679 /************************************************************************
1680 * BstrFromVector (OLEAUT32.@)
1682 * Create a BSTR from a SafeArray.
1684 * PARAMS
1685 * psa [I] Source array
1686 * pbstr [O] Destination for output BSTR
1688 * RETURNS
1689 * Success: S_OK. pbstr contains the arrays data.
1690 * Failure: An HRESULT error code indicating the error.
1692 * NOTES
1693 * psa must be a 1 dimensional array of a 1 byte type.
1695 * NOTES
1696 * See SafeArray.
1698 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1700 TRACE("(%p,%p)\n", psa, pbstr);
1702 if (!pbstr)
1703 return E_INVALIDARG;
1705 *pbstr = NULL;
1707 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1708 return E_INVALIDARG;
1710 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1711 if (!*pbstr)
1712 return E_OUTOFMEMORY;
1713 return S_OK;