push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / dlls / oleaut32 / safearray.c
blob34cfd9141c85158ada57150151dfc9caa7925929
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 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 = (PBYTE)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 if (*lpBstr)
318 SysFreeString(*lpBstr);
319 lpBstr++;
322 else if (psa->fFeatures & FADF_VARIANT)
324 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell;
326 while(ulCellCount--)
328 HRESULT hRet = VariantClear(lpVariant);
330 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
331 lpVariant++;
335 return S_OK;
338 /* Copy data items from one array to another */
339 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
341 if (!psa->pvData)
342 return S_OK;
343 else if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
344 return E_INVALIDARG;
345 else
347 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
349 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) |
350 (psa->fFeatures & ~(FADF_CREATEVECTOR|FADF_DATADELETED));
352 if (psa->fFeatures & FADF_VARIANT)
354 VARIANT* lpVariant = (VARIANT*)psa->pvData;
355 VARIANT* lpDest = (VARIANT*)dest->pvData;
357 while(ulCellCount--)
359 HRESULT hRet;
361 hRet = VariantCopy(lpDest, lpVariant);
362 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
363 lpVariant++;
364 lpDest++;
367 else if (psa->fFeatures & FADF_BSTR)
369 BSTR* lpBstr = (BSTR*)psa->pvData;
370 BSTR* lpDest = (BSTR*)dest->pvData;
372 while(ulCellCount--)
374 if (*lpBstr)
376 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
377 if (!*lpDest)
378 return E_OUTOFMEMORY;
380 else
381 *lpDest = NULL;
382 lpBstr++;
383 lpDest++;
386 else
388 /* Copy the data over */
389 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
391 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
393 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)dest->pvData;
395 while(ulCellCount--)
397 if (*lpUnknown)
398 IUnknown_AddRef(*lpUnknown);
399 lpUnknown++;
404 if (psa->fFeatures & FADF_RECORD)
406 IRecordInfo* pRecInfo = NULL;
408 SafeArrayGetRecordInfo(psa, &pRecInfo);
409 SafeArraySetRecordInfo(dest, pRecInfo);
411 if (pRecInfo)
413 /* Release because Get() adds a reference */
414 IRecordInfo_Release(pRecInfo);
417 else if (psa->fFeatures & FADF_HAVEIID)
419 GUID guid;
420 SafeArrayGetIID(psa, &guid);
421 SafeArraySetIID(dest, &guid);
423 else if (psa->fFeatures & FADF_HAVEVARTYPE)
425 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
428 return S_OK;
431 /*************************************************************************
432 * SafeArrayAllocDescriptor (OLEAUT32.36)
434 * Allocate and initialise a descriptor for a SafeArray.
436 * PARAMS
437 * cDims [I] Number of dimensions of the array
438 * ppsaOut [O] Destination for new descriptor
440 * RETURNS
441 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
442 * Failure: An HRESULT error code indicating the error.
444 * NOTES
445 * See SafeArray.
447 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
449 LONG allocSize;
451 TRACE("(%d,%p)\n", cDims, ppsaOut);
453 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
454 return E_INVALIDARG;
456 if (!ppsaOut)
457 return E_POINTER;
459 /* We need enough space for the header and its bounds */
460 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
462 if (FAILED(SAFEARRAY_AllocDescriptor(allocSize, ppsaOut)))
463 return E_UNEXPECTED;
465 (*ppsaOut)->cDims = cDims;
467 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims, allocSize);
468 return S_OK;
471 /*************************************************************************
472 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
474 * Allocate and initialise a descriptor for a SafeArray of a given type.
476 * PARAMS
477 * vt [I] The type of items to store in the array
478 * cDims [I] Number of dimensions of the array
479 * ppsaOut [O] Destination for new descriptor
481 * RETURNS
482 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
483 * Failure: An HRESULT error code indicating the error.
485 * NOTES
486 * - This function does not check that vt is an allowed VARTYPE.
487 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
488 * See SafeArray.
490 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
492 ULONG cbElements;
493 HRESULT hRet = E_UNEXPECTED;
495 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
497 cbElements = SAFEARRAY_GetVTSize(vt);
498 if (!cbElements)
499 WARN("Creating a descriptor with an invalid VARTYPE!\n");
501 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
503 if (SUCCEEDED(hRet))
505 SAFEARRAY_SetFeatures(vt, *ppsaOut);
506 (*ppsaOut)->cbElements = cbElements;
508 return hRet;
511 /*************************************************************************
512 * SafeArrayAllocData (OLEAUT32.37)
514 * Allocate the data area of a SafeArray.
516 * PARAMS
517 * psa [I] SafeArray to allocate the data area of.
519 * RETURNS
520 * Success: S_OK. The data area is allocated and initialised.
521 * Failure: An HRESULT error code indicating the error.
523 * NOTES
524 * See SafeArray.
526 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
528 HRESULT hRet = E_INVALIDARG;
530 TRACE("(%p)\n", psa);
532 if (psa)
534 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
536 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
538 if (psa->pvData)
540 hRet = S_OK;
541 TRACE("%u bytes allocated for data at %p (%u objects).\n",
542 ulSize * psa->cbElements, psa->pvData, ulSize);
544 else
545 hRet = E_OUTOFMEMORY;
547 return hRet;
550 /*************************************************************************
551 * SafeArrayCreate (OLEAUT32.15)
553 * Create a new SafeArray.
555 * PARAMS
556 * vt [I] Type to store in the safe array
557 * cDims [I] Number of array dimensions
558 * rgsabound [I] Bounds of the array dimensions
560 * RETURNS
561 * Success: A pointer to a new array object.
562 * Failure: NULL, if any parameter is invalid or memory allocation fails.
564 * NOTES
565 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
566 * in the Wine implementation.
567 * See SafeArray.
569 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
571 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
573 if (vt == VT_RECORD)
574 return NULL;
576 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
579 /*************************************************************************
580 * SafeArrayCreateEx (OLEAUT32.15)
582 * Create a new SafeArray.
584 * PARAMS
585 * vt [I] Type to store in the safe array
586 * cDims [I] Number of array dimensions
587 * rgsabound [I] Bounds of the array dimensions
588 * pvExtra [I] Extra data
590 * RETURNS
591 * Success: A pointer to a new array object.
592 * Failure: NULL, if any parameter is invalid or memory allocation fails.
594 * NOTES
595 * See SafeArray.
597 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
599 ULONG ulSize = 0;
600 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
601 SAFEARRAY* psa;
603 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
605 if (vt == VT_RECORD)
607 if (!iRecInfo)
608 return NULL;
609 IRecordInfo_GetSize(iRecInfo, &ulSize);
611 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
613 if (pvExtra)
615 switch(vt)
617 case VT_RECORD:
618 SafeArraySetRecordInfo(psa, pvExtra);
619 break;
620 case VT_UNKNOWN:
621 case VT_DISPATCH:
622 SafeArraySetIID(psa, pvExtra);
623 break;
626 return psa;
629 /************************************************************************
630 * SafeArrayCreateVector (OLEAUT32.411)
632 * Create a one dimensional, contiguous SafeArray.
634 * PARAMS
635 * vt [I] Type to store in the safe array
636 * lLbound [I] Lower bound of the array
637 * cElements [I] Number of elements in the array
639 * RETURNS
640 * Success: A pointer to a new array object.
641 * Failure: NULL, if any parameter is invalid or memory allocation fails.
643 * NOTES
644 * See SafeArray.
646 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
648 TRACE("(%d->%s,%d,%d\n", vt, debugstr_vt(vt), lLbound, cElements);
650 if (vt == VT_RECORD)
651 return NULL;
653 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
656 /************************************************************************
657 * SafeArrayCreateVectorEx (OLEAUT32.411)
659 * Create a one dimensional, contiguous SafeArray.
661 * PARAMS
662 * vt [I] Type to store in the safe array
663 * lLbound [I] Lower bound of the array
664 * cElements [I] Number of elements in the array
665 * pvExtra [I] Extra data
667 * RETURNS
668 * Success: A pointer to a new array object.
669 * Failure: NULL, if any parameter is invalid or memory allocation fails.
671 * NOTES
672 * See SafeArray.
674 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
676 ULONG ulSize;
677 IRecordInfo* iRecInfo = (IRecordInfo*)pvExtra;
678 SAFEARRAY* psa;
680 TRACE("(%d->%s,%d,%d,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
682 if (vt == VT_RECORD)
684 if (!iRecInfo)
685 return NULL;
686 IRecordInfo_GetSize(iRecInfo, &ulSize);
688 else
689 ulSize = SAFEARRAY_GetVTSize(vt);
691 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
693 if (pvExtra)
695 switch(vt)
697 case VT_RECORD:
698 SafeArraySetRecordInfo(psa, iRecInfo);
699 break;
700 case VT_UNKNOWN:
701 case VT_DISPATCH:
702 SafeArraySetIID(psa, pvExtra);
703 break;
706 return psa;
709 /*************************************************************************
710 * SafeArrayDestroyDescriptor (OLEAUT32.38)
712 * Destroy a SafeArray.
714 * PARAMS
715 * psa [I] SafeArray to destroy.
717 * RETURNS
718 * Success: S_OK. The resources used by the array are freed.
719 * Failure: An HRESULT error code indicating the error.
721 * NOTES
722 * See SafeArray.
724 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
726 TRACE("(%p)\n", psa);
728 if (psa)
730 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
732 if (psa->cLocks)
733 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
735 if (psa->fFeatures & FADF_RECORD)
736 SafeArraySetRecordInfo(psa, NULL);
738 if (psa->fFeatures & FADF_CREATEVECTOR &&
739 !(psa->fFeatures & FADF_DATADELETED))
740 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
742 if (!SAFEARRAY_Free(lpv))
743 return E_UNEXPECTED;
745 return S_OK;
748 /*************************************************************************
749 * SafeArrayLock (OLEAUT32.21)
751 * Increment the lock counter of a SafeArray.
753 * PARAMS
754 * psa [O] SafeArray to lock
756 * RETURNS
757 * Success: S_OK. The array lock is incremented.
758 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
759 * are held on the array at once.
761 * NOTES
762 * In Win32 these locks are not thread safe.
763 * See SafeArray.
765 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
767 ULONG ulLocks;
769 TRACE("(%p)\n", psa);
771 if (!psa)
772 return E_INVALIDARG;
774 ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
776 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
778 WARN("Out of locks!\n");
779 InterlockedDecrement( (LONG*) &psa->cLocks);
780 return E_UNEXPECTED;
782 return S_OK;
785 /*************************************************************************
786 * SafeArrayUnlock (OLEAUT32.22)
788 * Decrement the lock counter of a SafeArray.
790 * PARAMS
791 * psa [O] SafeArray to unlock
793 * RETURNS
794 * Success: S_OK. The array lock is decremented.
795 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
796 * held on the array.
798 * NOTES
799 * See SafeArray.
801 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
803 TRACE("(%p)\n", psa);
805 if (!psa)
806 return E_INVALIDARG;
808 if (InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
810 WARN("Unlocked but no lock held!\n");
811 InterlockedIncrement( (LONG*) &psa->cLocks);
812 return E_UNEXPECTED;
814 return S_OK;
817 /*************************************************************************
818 * SafeArrayPutElement (OLEAUT32.26)
820 * Put an item into a SafeArray.
822 * PARAMS
823 * psa [I] SafeArray to insert into
824 * rgIndices [I] Indices to insert at
825 * pvData [I] Data to insert
827 * RETURNS
828 * Success: S_OK. The item is inserted
829 * Failure: An HRESULT error code indicating the error.
831 * NOTES
832 * See SafeArray.
834 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
836 HRESULT hRet;
838 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
840 if (!psa || !rgIndices)
841 return E_INVALIDARG;
843 hRet = SafeArrayLock(psa);
845 if (SUCCEEDED(hRet))
847 PVOID lpvDest;
849 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
851 if (SUCCEEDED(hRet))
853 if (psa->fFeatures & FADF_VARIANT)
855 VARIANT* lpVariant = (VARIANT*)pvData;
856 VARIANT* lpDest = (VARIANT*)lpvDest;
858 hRet = VariantClear(lpDest);
859 if (FAILED(hRet)) FIXME("VariantClear failed with 0x%x\n", hRet);
860 hRet = VariantCopy(lpDest, lpVariant);
861 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
863 else if (psa->fFeatures & FADF_BSTR)
865 BSTR lpBstr = (BSTR)pvData;
866 BSTR* lpDest = (BSTR*)lpvDest;
868 if (*lpDest)
869 SysFreeString(*lpDest);
871 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
872 if (!*lpDest)
873 hRet = E_OUTOFMEMORY;
875 else
877 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
879 LPUNKNOWN lpUnknown = (LPUNKNOWN)pvData;
880 LPUNKNOWN *lpDest = (LPUNKNOWN *)lpvDest;
882 if (lpUnknown)
883 IUnknown_AddRef(lpUnknown);
884 if (*lpDest)
885 IUnknown_Release(*lpDest);
886 *lpDest = lpUnknown;
887 } else {
888 /* Copy the data over */
889 memcpy(lpvDest, pvData, psa->cbElements);
893 SafeArrayUnlock(psa);
895 return hRet;
899 /*************************************************************************
900 * SafeArrayGetElement (OLEAUT32.25)
902 * Get an item from a SafeArray.
904 * PARAMS
905 * psa [I] SafeArray to get from
906 * rgIndices [I] Indices to get from
907 * pvData [O] Destination for data
909 * RETURNS
910 * Success: S_OK. The item data is returned in pvData.
911 * Failure: An HRESULT error code indicating the error.
913 * NOTES
914 * See SafeArray.
916 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
918 HRESULT hRet;
920 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
922 if (!psa || !rgIndices || !pvData)
923 return E_INVALIDARG;
925 hRet = SafeArrayLock(psa);
927 if (SUCCEEDED(hRet))
929 PVOID lpvSrc;
931 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
933 if (SUCCEEDED(hRet))
935 if (psa->fFeatures & FADF_VARIANT)
937 VARIANT* lpVariant = (VARIANT*)lpvSrc;
938 VARIANT* lpDest = (VARIANT*)pvData;
940 /* The original content of pvData is ignored. */
941 V_VT(lpDest) = VT_EMPTY;
942 hRet = VariantCopy(lpDest, lpVariant);
943 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
945 else if (psa->fFeatures & FADF_BSTR)
947 BSTR* lpBstr = (BSTR*)lpvSrc;
948 BSTR* lpDest = (BSTR*)pvData;
950 if (*lpBstr)
952 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
953 if (!*lpBstr)
954 hRet = E_OUTOFMEMORY;
956 else
957 *lpDest = NULL;
959 else
961 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
963 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)lpvSrc;
965 if (*lpUnknown)
966 IUnknown_AddRef(*lpUnknown);
968 /* Copy the data over */
969 memcpy(pvData, lpvSrc, psa->cbElements);
972 SafeArrayUnlock(psa);
974 return hRet;
977 /*************************************************************************
978 * SafeArrayGetUBound (OLEAUT32.19)
980 * Get the upper bound for a given SafeArray dimension
982 * PARAMS
983 * psa [I] Array to get dimension upper bound from
984 * nDim [I] The dimension number to get the upper bound of
985 * plUbound [O] Destination for the upper bound
987 * RETURNS
988 * Success: S_OK. plUbound contains the dimensions upper bound.
989 * Failure: An HRESULT error code indicating the error.
991 * NOTES
992 * See SafeArray.
994 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
996 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
998 if (!psa || !plUbound)
999 return E_INVALIDARG;
1001 if(!nDim || nDim > psa->cDims)
1002 return DISP_E_BADINDEX;
1004 *plUbound = psa->rgsabound[psa->cDims - nDim].lLbound +
1005 psa->rgsabound[psa->cDims - nDim].cElements - 1;
1007 return S_OK;
1010 /*************************************************************************
1011 * SafeArrayGetLBound (OLEAUT32.20)
1013 * Get the lower bound for a given SafeArray dimension
1015 * PARAMS
1016 * psa [I] Array to get dimension lower bound from
1017 * nDim [I] The dimension number to get the lowe bound of
1018 * plLbound [O] Destination for the lower bound
1020 * RETURNS
1021 * Success: S_OK. plUbound contains the dimensions lower bound.
1022 * Failure: An HRESULT error code indicating the error.
1024 * NOTES
1025 * See SafeArray.
1027 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1029 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1031 if (!psa || !plLbound)
1032 return E_INVALIDARG;
1034 if(!nDim || nDim > psa->cDims)
1035 return DISP_E_BADINDEX;
1037 *plLbound = psa->rgsabound[psa->cDims - nDim].lLbound;
1038 return S_OK;
1041 /*************************************************************************
1042 * SafeArrayGetDim (OLEAUT32.17)
1044 * Get the number of dimensions in a SafeArray.
1046 * PARAMS
1047 * psa [I] Array to get the dimensions of
1049 * RETURNS
1050 * The number of array dimensions in psa, or 0 if psa is NULL.
1052 * NOTES
1053 * See SafeArray.
1055 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1057 TRACE("(%p) returning %d\n", psa, psa ? psa->cDims : 0u);
1058 return psa ? psa->cDims : 0;
1061 /*************************************************************************
1062 * SafeArrayGetElemsize (OLEAUT32.18)
1064 * Get the size of an element in a SafeArray.
1066 * PARAMS
1067 * psa [I] Array to get the element size from
1069 * RETURNS
1070 * The size of a single element in psa, or 0 if psa is NULL.
1072 * NOTES
1073 * See SafeArray.
1075 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1077 TRACE("(%p) returning %d\n", psa, psa ? psa->cbElements : 0u);
1078 return psa ? psa->cbElements : 0;
1081 /*************************************************************************
1082 * SafeArrayAccessData (OLEAUT32.23)
1084 * Lock a SafeArray and return a pointer to its data.
1086 * PARAMS
1087 * psa [I] Array to get the data pointer from
1088 * ppvData [O] Destination for the arrays data pointer
1090 * RETURNS
1091 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1092 * is locked.
1093 * Failure: An HRESULT error code indicating the error.
1095 * NOTES
1096 * See SafeArray.
1098 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1100 TRACE("(%p,%p)\n", psa, ppvData);
1102 if(!psa || !ppvData)
1103 return E_INVALIDARG;
1105 if (SUCCEEDED(SafeArrayLock(psa)))
1107 *ppvData = psa->pvData;
1108 return S_OK;
1110 *ppvData = NULL;
1111 return E_UNEXPECTED;
1115 /*************************************************************************
1116 * SafeArrayUnaccessData (OLEAUT32.24)
1118 * Unlock a SafeArray after accessing its data.
1120 * PARAMS
1121 * psa [I] Array to unlock
1123 * RETURNS
1124 * Success: S_OK. The array is unlocked.
1125 * Failure: An HRESULT error code indicating the error.
1127 * NOTES
1128 * See SafeArray.
1130 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1132 TRACE("(%p)\n", psa);
1133 return SafeArrayUnlock(psa);
1136 /************************************************************************
1137 * SafeArrayPtrOfIndex (OLEAUT32.148)
1139 * Get the address of an item in a SafeArray.
1141 * PARAMS
1142 * psa [I] Array to get the items address from
1143 * rgIndices [I] Index of the item in the array
1144 * ppvData [O] Destination for item address
1146 * RETURNS
1147 * Success: S_OK. ppvData contains a pointer to the item.
1148 * Failure: An HRESULT error code indicating the error.
1150 * NOTES
1151 * This function does not lock the array.
1153 * NOTES
1154 * See SafeArray.
1156 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1158 USHORT dim;
1159 ULONG cell = 0, dimensionSize = 1;
1160 SAFEARRAYBOUND* psab;
1161 LONG c1;
1163 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1165 /* The general formula for locating the cell number of an entry in an n
1166 * dimensional array (where cn = coordinate in dimension dn) is:
1168 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1170 * We calculate the size of the last dimension at each step through the
1171 * dimensions to avoid recursing to calculate the last dimensions size.
1173 if (!psa || !rgIndices || !ppvData)
1174 return E_INVALIDARG;
1176 psab = psa->rgsabound + psa->cDims - 1;
1177 c1 = *rgIndices++;
1179 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1180 return DISP_E_BADINDEX; /* Initial index out of bounds */
1182 for (dim = 1; dim < psa->cDims; dim++)
1184 dimensionSize *= psab->cElements;
1186 psab--;
1188 if (!psab->cElements ||
1189 *rgIndices < psab->lLbound ||
1190 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1191 return DISP_E_BADINDEX; /* Index out of bounds */
1193 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1194 rgIndices++;
1197 cell += (c1 - psa->rgsabound[psa->cDims - 1].lLbound);
1199 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1200 return S_OK;
1203 /************************************************************************
1204 * SafeArrayDestroyData (OLEAUT32.39)
1206 * Destroy the data associated with a SafeArray.
1208 * PARAMS
1209 * psa [I] Array to delete the data from
1211 * RETURNS
1212 * Success: S_OK. All items and the item data are freed.
1213 * Failure: An HRESULT error code indicating the error.
1215 * NOTES
1216 * See SafeArray.
1218 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1220 TRACE("(%p)\n", psa);
1222 if (!psa)
1223 return E_INVALIDARG;
1225 if (psa->cLocks)
1226 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1228 /* Delete the actual item data */
1229 if (FAILED(SAFEARRAY_DestroyData(psa, 0)))
1230 return E_UNEXPECTED;
1232 if (psa->pvData)
1234 if (psa->fFeatures & FADF_STATIC)
1236 ZeroMemory(psa->pvData, SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1237 return S_OK;
1239 /* If this is not a vector, free the data memory block */
1240 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1242 if (!SAFEARRAY_Free(psa->pvData))
1243 return E_UNEXPECTED;
1244 psa->pvData = NULL;
1246 else
1247 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1250 return S_OK;
1253 /************************************************************************
1254 * SafeArrayCopyData (OLEAUT32.412)
1256 * Copy all data from one SafeArray to another.
1258 * PARAMS
1259 * psaSource [I] Source for copy
1260 * psaTarget [O] Destination for copy
1262 * RETURNS
1263 * Success: S_OK. psaTarget contains a copy of psaSource.
1264 * Failure: An HRESULT error code indicating the error.
1266 * NOTES
1267 * The two arrays must have the same number of dimensions and elements.
1269 * NOTES
1270 * See SafeArray.
1272 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1274 int dim;
1276 TRACE("(%p,%p)\n", psaSource, psaTarget);
1278 if (!psaSource || !psaTarget ||
1279 psaSource->cDims != psaTarget->cDims ||
1280 psaSource->cbElements != psaTarget->cbElements)
1281 return E_INVALIDARG;
1283 /* Each dimension must be the same size */
1284 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1285 if (psaSource->rgsabound[dim].cElements !=
1286 psaTarget->rgsabound[dim].cElements)
1287 return E_INVALIDARG;
1289 if (SUCCEEDED(SAFEARRAY_DestroyData(psaTarget, 0)) &&
1290 SUCCEEDED(SAFEARRAY_CopyData(psaSource, psaTarget)))
1291 return S_OK;
1292 return E_UNEXPECTED;
1295 /************************************************************************
1296 * SafeArrayDestroy (OLEAUT32.16)
1298 * Destroy a SafeArray.
1300 * PARAMS
1301 * psa [I] Array to destroy
1303 * RETURNS
1304 * Success: S_OK. All resources used by the array are freed.
1305 * Failure: An HRESULT error code indicating the error.
1307 * NOTES
1308 * See SafeArray.
1310 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1312 TRACE("(%p)\n", psa);
1314 if(!psa)
1315 return S_OK;
1317 if(psa->cLocks > 0)
1318 return DISP_E_ARRAYISLOCKED;
1320 /* Native doesn't check to see if the free succeeds */
1321 SafeArrayDestroyData(psa);
1322 SafeArrayDestroyDescriptor(psa);
1323 return S_OK;
1326 /************************************************************************
1327 * SafeArrayCopy (OLEAUT32.27)
1329 * Make a duplicate of a SafeArray.
1331 * PARAMS
1332 * psa [I] Source for copy
1333 * ppsaOut [O] Destination for new copy
1335 * RETURNS
1336 * Success: S_OK. ppsaOut contains a copy of the array.
1337 * Failure: An HRESULT error code indicating the error.
1339 * NOTES
1340 * See SafeArray.
1342 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1344 HRESULT hRet;
1346 TRACE("(%p,%p)\n", psa, ppsaOut);
1348 if (!ppsaOut)
1349 return E_INVALIDARG;
1351 *ppsaOut = NULL;
1353 if (!psa)
1354 return S_OK; /* Handles copying of NULL arrays */
1356 if (!psa->cbElements)
1358 ERR("not copying an array of 0 elements\n");
1359 return E_INVALIDARG;
1362 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1364 VARTYPE vt;
1365 if (FAILED(SafeArrayGetVartype(psa, &vt)))
1366 hRet = E_UNEXPECTED;
1367 else
1368 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1370 else
1372 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1373 if (SUCCEEDED(hRet))
1375 (*ppsaOut)->fFeatures = psa->fFeatures & ~FADF_CREATEVECTOR;
1376 (*ppsaOut)->cbElements = psa->cbElements;
1380 if (SUCCEEDED(hRet))
1382 /* Copy dimension bounds */
1383 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1385 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1387 if ((*ppsaOut)->pvData)
1389 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1391 if (SUCCEEDED(hRet))
1392 return hRet;
1394 SAFEARRAY_Free((*ppsaOut)->pvData);
1396 SafeArrayDestroyDescriptor(*ppsaOut);
1398 *ppsaOut = NULL;
1399 return hRet;
1402 /************************************************************************
1403 * SafeArrayRedim (OLEAUT32.40)
1405 * Changes the characteristics of the last dimension of a SafeArray
1407 * PARAMS
1408 * psa [I] Array to change
1409 * psabound [I] New bound details for the last dimension
1411 * RETURNS
1412 * Success: S_OK. psa is updated to reflect the new bounds.
1413 * Failure: An HRESULT error code indicating the error.
1415 * NOTES
1416 * See SafeArray.
1418 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1420 SAFEARRAYBOUND *oldBounds;
1422 TRACE("(%p,%p)\n", psa, psabound);
1424 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1425 return E_INVALIDARG;
1427 if (psa->cLocks > 0)
1428 return DISP_E_ARRAYISLOCKED;
1430 if (FAILED(SafeArrayLock(psa)))
1431 return E_UNEXPECTED;
1433 oldBounds = psa->rgsabound;
1434 oldBounds->lLbound = psabound->lLbound;
1436 if (psabound->cElements != oldBounds->cElements)
1438 if (psabound->cElements < oldBounds->cElements)
1440 /* Shorten the final dimension. */
1441 ULONG ulStartCell = psabound->cElements *
1442 (SAFEARRAY_GetCellCount(psa) / oldBounds->cElements);
1443 SAFEARRAY_DestroyData(psa, ulStartCell);
1445 else
1447 /* Lengthen the final dimension */
1448 ULONG ulOldSize, ulNewSize;
1449 PVOID pvNewData;
1451 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1452 if (ulOldSize)
1453 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1454 else {
1455 int oldelems = oldBounds->cElements;
1456 oldBounds->cElements = psabound->cElements;
1457 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1458 oldBounds->cElements = oldelems;
1461 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1463 SafeArrayUnlock(psa);
1464 return E_UNEXPECTED;
1467 memcpy(pvNewData, psa->pvData, ulOldSize);
1468 SAFEARRAY_Free(psa->pvData);
1469 psa->pvData = pvNewData;
1471 oldBounds->cElements = psabound->cElements;
1474 SafeArrayUnlock(psa);
1475 return S_OK;
1478 /************************************************************************
1479 * SafeArrayGetVartype (OLEAUT32.77)
1481 * Get the type of the items in a SafeArray.
1483 * PARAMS
1484 * psa [I] Array to get the type from
1485 * pvt [O] Destination for the type
1487 * RETURNS
1488 * Success: S_OK. pvt contains the type of the items.
1489 * Failure: An HRESULT error code indicating the error.
1491 * NOTES
1492 * See SafeArray.
1494 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1496 TRACE("(%p,%p)\n", psa, pvt);
1498 if (!psa || !pvt)
1499 return E_INVALIDARG;
1501 if (psa->fFeatures & FADF_RECORD)
1502 *pvt = VT_RECORD;
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;