d2d1: Implement d2d_d3d_render_target_CreateBitmap().
[wine/multimedia.git] / dlls / oleaut32 / safearray.c
blob99ffe27778af7f3cff4c273fd1047d9ad3580f57
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 /* features listed here are not propagated to newly created array or data copy
89 created with SafeArrayCopy()/SafeArrayCopyData() */
90 static const USHORT ignored_copy_features =
91 FADF_AUTO |
92 FADF_STATIC |
93 FADF_EMBEDDED |
94 FADF_FIXEDSIZE |
95 FADF_CREATEVECTOR;
97 /* Allocate memory */
98 static inline LPVOID SAFEARRAY_Malloc(ULONG ulSize)
100 /* FIXME: Memory should be allocated and freed using a per-thread IMalloc
101 * instance returned from CoGetMalloc().
103 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulSize);
106 /* Free memory */
107 static inline BOOL SAFEARRAY_Free(LPVOID lpData)
109 return HeapFree(GetProcessHeap(), 0, lpData);
112 /* Get the size of a supported VT type (0 means unsupported) */
113 static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
115 switch (vt)
117 case VT_I1:
118 case VT_UI1: return sizeof(BYTE);
119 case VT_BOOL:
120 case VT_I2:
121 case VT_UI2: return sizeof(SHORT);
122 case VT_I4:
123 case VT_UI4:
124 case VT_R4:
125 case VT_ERROR: return sizeof(LONG);
126 case VT_R8:
127 case VT_I8:
128 case VT_UI8: return sizeof(LONG64);
129 case VT_INT:
130 case VT_UINT: return sizeof(INT);
131 case VT_INT_PTR:
132 case VT_UINT_PTR: return sizeof(UINT_PTR);
133 case VT_CY: return sizeof(CY);
134 case VT_DATE: return sizeof(DATE);
135 case VT_BSTR: return sizeof(BSTR);
136 case VT_DISPATCH: return sizeof(LPDISPATCH);
137 case VT_VARIANT: return sizeof(VARIANT);
138 case VT_UNKNOWN: return sizeof(LPUNKNOWN);
139 case VT_DECIMAL: return sizeof(DECIMAL);
140 /* Note: Return a non-zero size to indicate vt is valid. The actual size
141 * of a UDT is taken from the result of IRecordInfo_GetSize().
143 case VT_RECORD: return 32;
145 return 0;
148 /* Set the hidden data for an array */
149 static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
151 /* Implementation data is stored in the 4 bytes before the header */
152 LPDWORD lpDw = (LPDWORD)psa;
153 lpDw[-1] = dw;
156 /* Get the hidden data from an array */
157 static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
159 LPDWORD lpDw = (LPDWORD)psa;
160 return lpDw[-1];
163 /* Get the number of cells in a SafeArray */
164 static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
166 const SAFEARRAYBOUND* psab = psa->rgsabound;
167 USHORT cCount = psa->cDims;
168 ULONG ulNumCells = 1;
170 while (cCount--)
172 /* This is a valid bordercase. See testcases. -Marcus */
173 if (!psab->cElements)
174 return 0;
175 ulNumCells *= psab->cElements;
176 psab++;
178 return ulNumCells;
181 /* Allocate a descriptor for an array */
182 static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
184 char *ptr = SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE);
186 if (!ptr)
188 *ppsaOut = NULL;
189 return E_OUTOFMEMORY;
192 *ppsaOut = (SAFEARRAY*)(ptr + SAFEARRAY_HIDDEN_SIZE);
193 return S_OK;
196 /* Set the features of an array */
197 static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
199 /* Set the IID if we have one, otherwise set the type */
200 if (vt == VT_DISPATCH)
202 psa->fFeatures = FADF_HAVEIID;
203 SafeArraySetIID(psa, &IID_IDispatch);
205 else if (vt == VT_UNKNOWN)
207 psa->fFeatures = FADF_HAVEIID;
208 SafeArraySetIID(psa, &IID_IUnknown);
210 else if (vt == VT_RECORD)
211 psa->fFeatures = FADF_RECORD;
212 else
214 psa->fFeatures = FADF_HAVEVARTYPE;
215 SAFEARRAY_SetHiddenDWORD(psa, vt);
219 /* Create an array */
220 static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, const SAFEARRAYBOUND *rgsabound, ULONG ulSize)
222 SAFEARRAY *psa = NULL;
223 unsigned int i;
225 if (!rgsabound)
226 return NULL;
228 if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
230 switch (vt)
232 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
233 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
234 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
235 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
238 for (i = 0; i < cDims; i++)
239 memcpy(psa->rgsabound + i, rgsabound + cDims - 1 - i, sizeof(SAFEARRAYBOUND));
241 if (ulSize)
242 psa->cbElements = ulSize;
244 if (!psa->cbElements || FAILED(SafeArrayAllocData(psa)))
246 SafeArrayDestroyDescriptor(psa);
247 psa = NULL;
250 return psa;
253 /* Create an array as a vector */
254 static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
256 SAFEARRAY *psa = NULL;
258 if (ulSize || (vt == VT_RECORD))
260 /* Allocate the header and data together */
261 if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
263 SAFEARRAY_SetFeatures(vt, psa);
265 psa->cDims = 1;
266 psa->fFeatures |= FADF_CREATEVECTOR;
267 psa->pvData = &psa[1]; /* Data follows the header */
268 psa->cbElements = ulSize;
269 psa->rgsabound[0].cElements = cElements;
270 psa->rgsabound[0].lLbound = lLbound;
272 switch (vt)
274 case VT_BSTR: psa->fFeatures |= FADF_BSTR; break;
275 case VT_UNKNOWN: psa->fFeatures |= FADF_UNKNOWN; break;
276 case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
277 case VT_VARIANT: psa->fFeatures |= FADF_VARIANT; break;
281 return psa;
284 /* Free data items in an array */
285 static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
287 if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
289 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
291 if (ulStartCell > ulCellCount) {
292 FIXME("unexpted ulcellcount %d, start %d\n",ulCellCount,ulStartCell);
293 return E_UNEXPECTED;
296 ulCellCount -= ulStartCell;
298 if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
300 LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell;
302 while(ulCellCount--)
304 if (*lpUnknown)
305 IUnknown_Release(*lpUnknown);
306 lpUnknown++;
309 else if (psa->fFeatures & FADF_RECORD)
311 IRecordInfo *lpRecInfo;
313 if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
315 PBYTE pRecordData = psa->pvData;
316 while(ulCellCount--)
318 IRecordInfo_RecordClear(lpRecInfo, pRecordData);
319 pRecordData += psa->cbElements;
321 IRecordInfo_Release(lpRecInfo);
324 else if (psa->fFeatures & FADF_BSTR)
326 BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell;
328 while(ulCellCount--)
330 SysFreeString(*lpBstr);
331 lpBstr++;
334 else if (psa->fFeatures & FADF_VARIANT)
336 VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell;
338 while(ulCellCount--)
340 HRESULT hRet = VariantClear(lpVariant);
342 if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
343 lpVariant++;
347 return S_OK;
350 /* Copy data items from one array to another. Destination data is freed before copy. */
351 static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
353 HRESULT hr = S_OK;
355 if (!psa->pvData)
356 return S_OK;
358 if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
359 return E_INVALIDARG;
360 else
362 ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
364 dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) | (psa->fFeatures & ~ignored_copy_features);
366 if (psa->fFeatures & FADF_VARIANT)
368 VARIANT *src_var = psa->pvData;
369 VARIANT *dest_var = dest->pvData;
371 while(ulCellCount--)
373 HRESULT hRet;
375 /* destination is cleared automatically */
376 hRet = VariantCopy(dest_var, src_var);
377 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%08x, element %u\n", hRet, ulCellCount);
378 src_var++;
379 dest_var++;
382 else if (psa->fFeatures & FADF_BSTR)
384 BSTR *src_bstr = psa->pvData;
385 BSTR *dest_bstr = dest->pvData;
387 while(ulCellCount--)
389 SysFreeString(*dest_bstr);
390 if (*src_bstr)
392 *dest_bstr = SysAllocStringByteLen((char*)*src_bstr, SysStringByteLen(*src_bstr));
393 if (!*dest_bstr)
394 return E_OUTOFMEMORY;
396 else
397 *dest_bstr = NULL;
398 src_bstr++;
399 dest_bstr++;
402 else if (psa->fFeatures & FADF_RECORD)
404 BYTE *dest_data = dest->pvData;
405 BYTE *src_data = psa->pvData;
406 IRecordInfo *record;
408 SafeArrayGetRecordInfo(psa, &record);
409 while (ulCellCount--)
411 /* RecordCopy() clears destination record */
412 hr = IRecordInfo_RecordCopy(record, src_data, dest_data);
413 if (FAILED(hr)) break;
414 src_data += psa->cbElements;
415 dest_data += psa->cbElements;
418 SafeArraySetRecordInfo(dest, record);
419 /* This value is set to 32 bytes by default on descriptor creation,
420 update with actual structure size. */
421 dest->cbElements = psa->cbElements;
422 IRecordInfo_Release(record);
424 else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
426 IUnknown **dest_unk = dest->pvData;
427 IUnknown **src_unk = psa->pvData;
429 /* release old iface, addref new one */
430 while (ulCellCount--)
432 if (*dest_unk)
433 IUnknown_Release(*dest_unk);
434 *dest_unk = *src_unk;
435 if (*dest_unk)
436 IUnknown_AddRef(*dest_unk);
437 src_unk++;
438 dest_unk++;
441 else
443 /* Copy the data over */
444 memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
447 if (psa->fFeatures & FADF_HAVEIID)
449 GUID guid;
450 SafeArrayGetIID(psa, &guid);
451 SafeArraySetIID(dest, &guid);
453 else if (psa->fFeatures & FADF_HAVEVARTYPE)
455 SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
459 return hr;
462 /*************************************************************************
463 * SafeArrayAllocDescriptor (OLEAUT32.36)
465 * Allocate and initialise a descriptor for a SafeArray.
467 * PARAMS
468 * cDims [I] Number of dimensions of the array
469 * ppsaOut [O] Destination for new descriptor
471 * RETURNS
472 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
473 * Failure: An HRESULT error code indicating the error.
475 * NOTES
476 * See SafeArray.
478 HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
480 LONG allocSize;
481 HRESULT hr;
483 TRACE("(%d,%p)\n", cDims, ppsaOut);
485 if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
486 return E_INVALIDARG;
488 if (!ppsaOut)
489 return E_POINTER;
491 /* We need enough space for the header and its bounds */
492 allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
494 hr = SAFEARRAY_AllocDescriptor(allocSize, ppsaOut);
495 if (FAILED(hr))
496 return hr;
498 (*ppsaOut)->cDims = cDims;
500 TRACE("(%d): %u bytes allocated for descriptor.\n", cDims, allocSize);
501 return S_OK;
504 /*************************************************************************
505 * SafeArrayAllocDescriptorEx (OLEAUT32.41)
507 * Allocate and initialise a descriptor for a SafeArray of a given type.
509 * PARAMS
510 * vt [I] The type of items to store in the array
511 * cDims [I] Number of dimensions of the array
512 * ppsaOut [O] Destination for new descriptor
514 * RETURNS
515 * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
516 * Failure: An HRESULT error code indicating the error.
518 * NOTES
519 * - This function does not check that vt is an allowed VARTYPE.
520 * - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
521 * See SafeArray.
523 HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
525 ULONG cbElements;
526 HRESULT hRet;
528 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, ppsaOut);
530 cbElements = SAFEARRAY_GetVTSize(vt);
531 if (!cbElements)
532 WARN("Creating a descriptor with an invalid VARTYPE!\n");
534 hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
536 if (SUCCEEDED(hRet))
538 SAFEARRAY_SetFeatures(vt, *ppsaOut);
539 (*ppsaOut)->cbElements = cbElements;
541 return hRet;
544 /*************************************************************************
545 * SafeArrayAllocData (OLEAUT32.37)
547 * Allocate the data area of a SafeArray.
549 * PARAMS
550 * psa [I] SafeArray to allocate the data area of.
552 * RETURNS
553 * Success: S_OK. The data area is allocated and initialised.
554 * Failure: An HRESULT error code indicating the error.
556 * NOTES
557 * See SafeArray.
559 HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
561 HRESULT hRet = E_INVALIDARG;
563 TRACE("(%p)\n", psa);
565 if (psa)
567 ULONG ulSize = SAFEARRAY_GetCellCount(psa);
569 psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
571 if (psa->pvData)
573 hRet = S_OK;
574 TRACE("%u bytes allocated for data at %p (%u objects).\n",
575 ulSize * psa->cbElements, psa->pvData, ulSize);
577 else
578 hRet = E_OUTOFMEMORY;
580 return hRet;
583 /*************************************************************************
584 * SafeArrayCreate (OLEAUT32.15)
586 * Create a new SafeArray.
588 * PARAMS
589 * vt [I] Type to store in the safe array
590 * cDims [I] Number of array dimensions
591 * rgsabound [I] Bounds of the array dimensions
593 * RETURNS
594 * Success: A pointer to a new array object.
595 * Failure: NULL, if any parameter is invalid or memory allocation fails.
597 * NOTES
598 * Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
599 * in the Wine implementation.
600 * See SafeArray.
602 SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
604 TRACE("(%d->%s,%d,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound);
606 if (vt == VT_RECORD)
607 return NULL;
609 return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
612 /*************************************************************************
613 * SafeArrayCreateEx (OLEAUT32.15)
615 * Create a new SafeArray.
617 * PARAMS
618 * vt [I] Type to store in the safe array
619 * cDims [I] Number of array dimensions
620 * rgsabound [I] Bounds of the array dimensions
621 * pvExtra [I] Extra data
623 * RETURNS
624 * Success: A pointer to a new array object.
625 * Failure: NULL, if any parameter is invalid or memory allocation fails.
627 * NOTES
628 * See SafeArray.
630 SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
632 ULONG ulSize = 0;
633 IRecordInfo* iRecInfo = pvExtra;
634 SAFEARRAY* psa;
636 TRACE("(%d->%s,%d,%p,%p)\n", vt, debugstr_vt(vt), cDims, rgsabound, pvExtra);
638 if (vt == VT_RECORD)
640 if (!iRecInfo)
641 return NULL;
642 IRecordInfo_GetSize(iRecInfo, &ulSize);
644 psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
646 if (pvExtra)
648 switch(vt)
650 case VT_RECORD:
651 SafeArraySetRecordInfo(psa, pvExtra);
652 break;
653 case VT_UNKNOWN:
654 case VT_DISPATCH:
655 SafeArraySetIID(psa, pvExtra);
656 break;
659 return psa;
662 /************************************************************************
663 * SafeArrayCreateVector (OLEAUT32.411)
665 * Create a one dimensional, contiguous SafeArray.
667 * PARAMS
668 * vt [I] Type to store in the safe array
669 * lLbound [I] Lower bound of the array
670 * cElements [I] Number of elements in the array
672 * RETURNS
673 * Success: A pointer to a new array object.
674 * Failure: NULL, if any parameter is invalid or memory allocation fails.
676 * NOTES
677 * See SafeArray.
679 SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
681 TRACE("(%d->%s,%d,%d\n", vt, debugstr_vt(vt), lLbound, cElements);
683 if (vt == VT_RECORD)
684 return NULL;
686 return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
689 /************************************************************************
690 * SafeArrayCreateVectorEx (OLEAUT32.411)
692 * Create a one dimensional, contiguous SafeArray.
694 * PARAMS
695 * vt [I] Type to store in the safe array
696 * lLbound [I] Lower bound of the array
697 * cElements [I] Number of elements in the array
698 * pvExtra [I] Extra data
700 * RETURNS
701 * Success: A pointer to a new array object.
702 * Failure: NULL, if any parameter is invalid or memory allocation fails.
704 * NOTES
705 * See SafeArray.
707 SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
709 ULONG ulSize;
710 IRecordInfo* iRecInfo = pvExtra;
711 SAFEARRAY* psa;
713 TRACE("(%d->%s,%d,%d,%p\n", vt, debugstr_vt(vt), lLbound, cElements, pvExtra);
715 if (vt == VT_RECORD)
717 if (!iRecInfo)
718 return NULL;
719 IRecordInfo_GetSize(iRecInfo, &ulSize);
721 else
722 ulSize = SAFEARRAY_GetVTSize(vt);
724 psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
726 if (pvExtra)
728 switch(vt)
730 case VT_RECORD:
731 SafeArraySetRecordInfo(psa, iRecInfo);
732 break;
733 case VT_UNKNOWN:
734 case VT_DISPATCH:
735 SafeArraySetIID(psa, pvExtra);
736 break;
739 return psa;
742 /*************************************************************************
743 * SafeArrayDestroyDescriptor (OLEAUT32.38)
745 * Destroy a SafeArray.
747 * PARAMS
748 * psa [I] SafeArray to destroy.
750 * RETURNS
751 * Success: S_OK. The resources used by the array are freed.
752 * Failure: An HRESULT error code indicating the error.
754 * NOTES
755 * See SafeArray.
757 HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
759 TRACE("(%p)\n", psa);
761 if (psa)
763 LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
765 if (psa->cLocks)
766 return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
768 if (psa->fFeatures & FADF_RECORD)
769 SafeArraySetRecordInfo(psa, NULL);
771 if (psa->fFeatures & FADF_CREATEVECTOR &&
772 !(psa->fFeatures & FADF_DATADELETED))
773 SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
775 if (!SAFEARRAY_Free(lpv))
776 return E_UNEXPECTED;
778 return S_OK;
781 /*************************************************************************
782 * SafeArrayLock (OLEAUT32.21)
784 * Increment the lock counter of a SafeArray.
786 * PARAMS
787 * psa [O] SafeArray to lock
789 * RETURNS
790 * Success: S_OK. The array lock is incremented.
791 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
792 * are held on the array at once.
794 * NOTES
795 * In Win32 these locks are not thread safe.
796 * See SafeArray.
798 HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
800 ULONG ulLocks;
802 TRACE("(%p)\n", psa);
804 if (!psa)
805 return E_INVALIDARG;
807 ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
809 if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
811 WARN("Out of locks!\n");
812 InterlockedDecrement( (LONG*) &psa->cLocks);
813 return E_UNEXPECTED;
815 return S_OK;
818 /*************************************************************************
819 * SafeArrayUnlock (OLEAUT32.22)
821 * Decrement the lock counter of a SafeArray.
823 * PARAMS
824 * psa [O] SafeArray to unlock
826 * RETURNS
827 * Success: S_OK. The array lock is decremented.
828 * Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
829 * held on the array.
831 * NOTES
832 * See SafeArray.
834 HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
836 TRACE("(%p)\n", psa);
838 if (!psa)
839 return E_INVALIDARG;
841 if (InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
843 WARN("Unlocked but no lock held!\n");
844 InterlockedIncrement( (LONG*) &psa->cLocks);
845 return E_UNEXPECTED;
847 return S_OK;
850 /*************************************************************************
851 * SafeArrayPutElement (OLEAUT32.26)
853 * Put an item into a SafeArray.
855 * PARAMS
856 * psa [I] SafeArray to insert into
857 * rgIndices [I] Indices to insert at
858 * pvData [I] Data to insert
860 * RETURNS
861 * Success: S_OK. The item is inserted
862 * Failure: An HRESULT error code indicating the error.
864 * NOTES
865 * See SafeArray.
867 HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
869 HRESULT hRet;
871 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
873 if (!psa || !rgIndices)
874 return E_INVALIDARG;
876 hRet = SafeArrayLock(psa);
878 if (SUCCEEDED(hRet))
880 PVOID lpvDest;
882 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
884 if (SUCCEEDED(hRet))
886 if (psa->fFeatures & FADF_VARIANT)
888 VARIANT* lpVariant = pvData;
889 VARIANT* lpDest = lpvDest;
891 hRet = VariantCopy(lpDest, lpVariant);
892 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
894 else if (psa->fFeatures & FADF_BSTR)
896 BSTR lpBstr = (BSTR)pvData;
897 BSTR* lpDest = lpvDest;
899 SysFreeString(*lpDest);
901 *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
902 if (!*lpDest)
903 hRet = E_OUTOFMEMORY;
905 else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
907 IUnknown *lpUnknown = pvData;
908 IUnknown **lpDest = lpvDest;
910 if (lpUnknown)
911 IUnknown_AddRef(lpUnknown);
912 if (*lpDest)
913 IUnknown_Release(*lpDest);
914 *lpDest = lpUnknown;
916 else if (psa->fFeatures & FADF_RECORD)
918 IRecordInfo *record;
920 SafeArrayGetRecordInfo(psa, &record);
921 hRet = IRecordInfo_RecordCopy(record, pvData, lpvDest);
922 IRecordInfo_Release(record);
923 } else
924 /* Copy the data over */
925 memcpy(lpvDest, pvData, psa->cbElements);
927 SafeArrayUnlock(psa);
929 return hRet;
933 /*************************************************************************
934 * SafeArrayGetElement (OLEAUT32.25)
936 * Get an item from a SafeArray.
938 * PARAMS
939 * psa [I] SafeArray to get from
940 * rgIndices [I] Indices to get from
941 * pvData [O] Destination for data
943 * RETURNS
944 * Success: S_OK. The item data is returned in pvData.
945 * Failure: An HRESULT error code indicating the error.
947 * NOTES
948 * See SafeArray.
950 HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
952 HRESULT hRet;
954 TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
956 if (!psa || !rgIndices || !pvData)
957 return E_INVALIDARG;
959 hRet = SafeArrayLock(psa);
961 if (SUCCEEDED(hRet))
963 PVOID lpvSrc;
965 hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
967 if (SUCCEEDED(hRet))
969 if (psa->fFeatures & FADF_VARIANT)
971 VARIANT* lpVariant = lpvSrc;
972 VARIANT* lpDest = pvData;
974 /* The original content of pvData is ignored. */
975 V_VT(lpDest) = VT_EMPTY;
976 hRet = VariantCopy(lpDest, lpVariant);
977 if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
979 else if (psa->fFeatures & FADF_BSTR)
981 BSTR* lpBstr = lpvSrc;
982 BSTR* lpDest = pvData;
984 if (*lpBstr)
986 *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
987 if (!*lpBstr)
988 hRet = E_OUTOFMEMORY;
990 else
991 *lpDest = NULL;
993 else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
995 IUnknown **src_unk = lpvSrc;
996 IUnknown **dest_unk = pvData;
998 if (*src_unk)
999 IUnknown_AddRef(*src_unk);
1000 *dest_unk = *src_unk;
1002 else if (psa->fFeatures & FADF_RECORD)
1004 IRecordInfo *record;
1006 SafeArrayGetRecordInfo(psa, &record);
1007 hRet = IRecordInfo_RecordCopy(record, lpvSrc, pvData);
1008 IRecordInfo_Release(record);
1010 else
1011 /* Copy the data over */
1012 memcpy(pvData, lpvSrc, psa->cbElements);
1014 SafeArrayUnlock(psa);
1016 return hRet;
1019 /*************************************************************************
1020 * SafeArrayGetUBound (OLEAUT32.19)
1022 * Get the upper bound for a given SafeArray dimension
1024 * PARAMS
1025 * psa [I] Array to get dimension upper bound from
1026 * nDim [I] The dimension number to get the upper bound of
1027 * plUbound [O] Destination for the upper bound
1029 * RETURNS
1030 * Success: S_OK. plUbound contains the dimensions upper bound.
1031 * Failure: An HRESULT error code indicating the error.
1033 * NOTES
1034 * See SafeArray.
1036 HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
1038 TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1040 if (!psa || !plUbound)
1041 return E_INVALIDARG;
1043 if(!nDim || nDim > psa->cDims)
1044 return DISP_E_BADINDEX;
1046 *plUbound = psa->rgsabound[psa->cDims - nDim].lLbound +
1047 psa->rgsabound[psa->cDims - nDim].cElements - 1;
1049 return S_OK;
1052 /*************************************************************************
1053 * SafeArrayGetLBound (OLEAUT32.20)
1055 * Get the lower bound for a given SafeArray dimension
1057 * PARAMS
1058 * psa [I] Array to get dimension lower bound from
1059 * nDim [I] The dimension number to get the lower bound of
1060 * plLbound [O] Destination for the lower bound
1062 * RETURNS
1063 * Success: S_OK. plUbound contains the dimensions lower bound.
1064 * Failure: An HRESULT error code indicating the error.
1066 * NOTES
1067 * See SafeArray.
1069 HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1071 TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1073 if (!psa || !plLbound)
1074 return E_INVALIDARG;
1076 if(!nDim || nDim > psa->cDims)
1077 return DISP_E_BADINDEX;
1079 *plLbound = psa->rgsabound[psa->cDims - nDim].lLbound;
1080 return S_OK;
1083 /*************************************************************************
1084 * SafeArrayGetDim (OLEAUT32.17)
1086 * Get the number of dimensions in a SafeArray.
1088 * PARAMS
1089 * psa [I] Array to get the dimensions of
1091 * RETURNS
1092 * The number of array dimensions in psa, or 0 if psa is NULL.
1094 * NOTES
1095 * See SafeArray.
1097 UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1099 TRACE("(%p) returning %d\n", psa, psa ? psa->cDims : 0u);
1100 return psa ? psa->cDims : 0;
1103 /*************************************************************************
1104 * SafeArrayGetElemsize (OLEAUT32.18)
1106 * Get the size of an element in a SafeArray.
1108 * PARAMS
1109 * psa [I] Array to get the element size from
1111 * RETURNS
1112 * The size of a single element in psa, or 0 if psa is NULL.
1114 * NOTES
1115 * See SafeArray.
1117 UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1119 TRACE("(%p) returning %d\n", psa, psa ? psa->cbElements : 0u);
1120 return psa ? psa->cbElements : 0;
1123 /*************************************************************************
1124 * SafeArrayAccessData (OLEAUT32.23)
1126 * Lock a SafeArray and return a pointer to its data.
1128 * PARAMS
1129 * psa [I] Array to get the data pointer from
1130 * ppvData [O] Destination for the arrays data pointer
1132 * RETURNS
1133 * Success: S_OK. ppvData contains the arrays data pointer, and the array
1134 * is locked.
1135 * Failure: An HRESULT error code indicating the error.
1137 * NOTES
1138 * See SafeArray.
1140 HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1142 HRESULT hr;
1144 TRACE("(%p,%p)\n", psa, ppvData);
1146 if(!psa || !ppvData)
1147 return E_INVALIDARG;
1149 hr = SafeArrayLock(psa);
1150 *ppvData = SUCCEEDED(hr) ? psa->pvData : NULL;
1152 return hr;
1156 /*************************************************************************
1157 * SafeArrayUnaccessData (OLEAUT32.24)
1159 * Unlock a SafeArray after accessing its data.
1161 * PARAMS
1162 * psa [I] Array to unlock
1164 * RETURNS
1165 * Success: S_OK. The array is unlocked.
1166 * Failure: An HRESULT error code indicating the error.
1168 * NOTES
1169 * See SafeArray.
1171 HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1173 TRACE("(%p)\n", psa);
1174 return SafeArrayUnlock(psa);
1177 /************************************************************************
1178 * SafeArrayPtrOfIndex (OLEAUT32.148)
1180 * Get the address of an item in a SafeArray.
1182 * PARAMS
1183 * psa [I] Array to get the items address from
1184 * rgIndices [I] Index of the item in the array
1185 * ppvData [O] Destination for item address
1187 * RETURNS
1188 * Success: S_OK. ppvData contains a pointer to the item.
1189 * Failure: An HRESULT error code indicating the error.
1191 * NOTES
1192 * This function does not lock the array.
1194 * NOTES
1195 * See SafeArray.
1197 HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1199 USHORT dim;
1200 ULONG cell = 0, dimensionSize = 1;
1201 SAFEARRAYBOUND* psab;
1202 LONG c1;
1204 TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1206 /* The general formula for locating the cell number of an entry in an n
1207 * dimensional array (where cn = coordinate in dimension dn) is:
1209 * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1211 * We calculate the size of the last dimension at each step through the
1212 * dimensions to avoid recursing to calculate the last dimensions size.
1214 if (!psa || !rgIndices || !ppvData)
1215 return E_INVALIDARG;
1217 psab = psa->rgsabound + psa->cDims - 1;
1218 c1 = *rgIndices++;
1220 if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1221 return DISP_E_BADINDEX; /* Initial index out of bounds */
1223 for (dim = 1; dim < psa->cDims; dim++)
1225 dimensionSize *= psab->cElements;
1227 psab--;
1229 if (!psab->cElements ||
1230 *rgIndices < psab->lLbound ||
1231 *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1232 return DISP_E_BADINDEX; /* Index out of bounds */
1234 cell += (*rgIndices - psab->lLbound) * dimensionSize;
1235 rgIndices++;
1238 cell += (c1 - psa->rgsabound[psa->cDims - 1].lLbound);
1240 *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1241 return S_OK;
1244 /************************************************************************
1245 * SafeArrayDestroyData (OLEAUT32.39)
1247 * Destroy the data associated with a SafeArray.
1249 * PARAMS
1250 * psa [I] Array to delete the data from
1252 * RETURNS
1253 * Success: S_OK. All items and the item data are freed.
1254 * Failure: An HRESULT error code indicating the error.
1256 * NOTES
1257 * See SafeArray.
1259 HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1261 HRESULT hr;
1263 TRACE("(%p)\n", psa);
1265 if (!psa)
1266 return E_INVALIDARG;
1268 if (psa->cLocks)
1269 return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1271 /* Delete the actual item data */
1272 hr = SAFEARRAY_DestroyData(psa, 0);
1273 if (FAILED(hr))
1274 return hr;
1276 if (psa->pvData)
1278 if (psa->fFeatures & FADF_STATIC)
1280 ZeroMemory(psa->pvData, SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1281 return S_OK;
1283 /* If this is not a vector, free the data memory block */
1284 if (!(psa->fFeatures & FADF_CREATEVECTOR))
1286 if (!SAFEARRAY_Free(psa->pvData))
1287 return E_UNEXPECTED;
1288 psa->pvData = NULL;
1290 else
1291 psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1294 return S_OK;
1297 /************************************************************************
1298 * SafeArrayCopyData (OLEAUT32.412)
1300 * Copy all data from one SafeArray to another.
1302 * PARAMS
1303 * psaSource [I] Source for copy
1304 * psaTarget [O] Destination for copy
1306 * RETURNS
1307 * Success: S_OK. psaTarget contains a copy of psaSource.
1308 * Failure: An HRESULT error code indicating the error.
1310 * NOTES
1311 * The two arrays must have the same number of dimensions and elements.
1313 * NOTES
1314 * See SafeArray.
1316 HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1318 int dim;
1320 TRACE("(%p,%p)\n", psaSource, psaTarget);
1322 if (!psaSource || !psaTarget ||
1323 psaSource->cDims != psaTarget->cDims ||
1324 psaSource->cbElements != psaTarget->cbElements)
1325 return E_INVALIDARG;
1327 /* Each dimension must be the same size */
1328 for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1329 if (psaSource->rgsabound[dim].cElements !=
1330 psaTarget->rgsabound[dim].cElements)
1331 return E_INVALIDARG;
1333 return SAFEARRAY_CopyData(psaSource, psaTarget);
1336 /************************************************************************
1337 * SafeArrayDestroy (OLEAUT32.16)
1339 * Destroy a SafeArray.
1341 * PARAMS
1342 * psa [I] Array to destroy
1344 * RETURNS
1345 * Success: S_OK. All resources used by the array are freed.
1346 * Failure: An HRESULT error code indicating the error.
1348 * NOTES
1349 * See SafeArray.
1351 HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1353 TRACE("(%p)\n", psa);
1355 if(!psa)
1356 return S_OK;
1358 if(psa->cLocks > 0)
1359 return DISP_E_ARRAYISLOCKED;
1361 /* Native doesn't check to see if the free succeeds */
1362 SafeArrayDestroyData(psa);
1363 SafeArrayDestroyDescriptor(psa);
1364 return S_OK;
1367 /************************************************************************
1368 * SafeArrayCopy (OLEAUT32.27)
1370 * Make a duplicate of a SafeArray.
1372 * PARAMS
1373 * psa [I] Source for copy
1374 * ppsaOut [O] Destination for new copy
1376 * RETURNS
1377 * Success: S_OK. ppsaOut contains a copy of the array.
1378 * Failure: An HRESULT error code indicating the error.
1380 * NOTES
1381 * See SafeArray.
1383 HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1385 HRESULT hRet;
1387 TRACE("(%p,%p)\n", psa, ppsaOut);
1389 if (!ppsaOut)
1390 return E_INVALIDARG;
1392 *ppsaOut = NULL;
1394 if (!psa)
1395 return S_OK; /* Handles copying of NULL arrays */
1397 if (!psa->cbElements)
1398 return E_INVALIDARG;
1400 if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1402 VARTYPE vt;
1404 hRet = SafeArrayGetVartype(psa, &vt);
1405 if (SUCCEEDED(hRet))
1406 hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1408 else
1410 hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1411 if (SUCCEEDED(hRet))
1413 (*ppsaOut)->fFeatures = psa->fFeatures & ~ignored_copy_features;
1414 (*ppsaOut)->cbElements = psa->cbElements;
1418 if (SUCCEEDED(hRet))
1420 /* Copy dimension bounds */
1421 memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1423 (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1424 if (!(*ppsaOut)->pvData)
1426 SafeArrayDestroyDescriptor(*ppsaOut);
1427 *ppsaOut = NULL;
1428 return E_OUTOFMEMORY;
1431 hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1432 if (FAILED(hRet))
1434 SAFEARRAY_Free((*ppsaOut)->pvData);
1435 SafeArrayDestroyDescriptor(*ppsaOut);
1436 *ppsaOut = NULL;
1437 return hRet;
1441 return hRet;
1444 /************************************************************************
1445 * SafeArrayRedim (OLEAUT32.40)
1447 * Changes the characteristics of the last dimension of a SafeArray
1449 * PARAMS
1450 * psa [I] Array to change
1451 * psabound [I] New bound details for the last dimension
1453 * RETURNS
1454 * Success: S_OK. psa is updated to reflect the new bounds.
1455 * Failure: An HRESULT error code indicating the error.
1457 * NOTES
1458 * See SafeArray.
1460 HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1462 SAFEARRAYBOUND *oldBounds;
1463 HRESULT hr;
1465 TRACE("(%p,%p)\n", psa, psabound);
1467 if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1468 return E_INVALIDARG;
1470 if (psa->cLocks > 0)
1471 return DISP_E_ARRAYISLOCKED;
1473 hr = SafeArrayLock(psa);
1474 if (FAILED(hr))
1475 return hr;
1477 oldBounds = psa->rgsabound;
1478 oldBounds->lLbound = psabound->lLbound;
1480 if (psabound->cElements != oldBounds->cElements)
1482 if (psabound->cElements < oldBounds->cElements)
1484 /* Shorten the final dimension. */
1485 ULONG ulStartCell = psabound->cElements *
1486 (SAFEARRAY_GetCellCount(psa) / oldBounds->cElements);
1487 SAFEARRAY_DestroyData(psa, ulStartCell);
1489 else
1491 /* Lengthen the final dimension */
1492 ULONG ulOldSize, ulNewSize;
1493 PVOID pvNewData;
1495 ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1496 if (ulOldSize)
1497 ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1498 else {
1499 int oldelems = oldBounds->cElements;
1500 oldBounds->cElements = psabound->cElements;
1501 ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1502 oldBounds->cElements = oldelems;
1505 if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1507 SafeArrayUnlock(psa);
1508 return E_OUTOFMEMORY;
1511 memcpy(pvNewData, psa->pvData, ulOldSize);
1512 SAFEARRAY_Free(psa->pvData);
1513 psa->pvData = pvNewData;
1515 oldBounds->cElements = psabound->cElements;
1518 SafeArrayUnlock(psa);
1519 return S_OK;
1522 /************************************************************************
1523 * SafeArrayGetVartype (OLEAUT32.77)
1525 * Get the type of the items in a SafeArray.
1527 * PARAMS
1528 * psa [I] Array to get the type from
1529 * pvt [O] Destination for the type
1531 * RETURNS
1532 * Success: S_OK. pvt contains the type of the items.
1533 * Failure: An HRESULT error code indicating the error.
1535 * NOTES
1536 * See SafeArray.
1538 HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1540 TRACE("(%p,%p)\n", psa, pvt);
1542 if (!psa || !pvt)
1543 return E_INVALIDARG;
1545 if (psa->fFeatures & FADF_RECORD)
1546 *pvt = VT_RECORD;
1547 else if ((psa->fFeatures & (FADF_HAVEIID|FADF_DISPATCH)) == (FADF_HAVEIID|FADF_DISPATCH))
1548 *pvt = VT_DISPATCH;
1549 else if (psa->fFeatures & FADF_HAVEIID)
1550 *pvt = VT_UNKNOWN;
1551 else if (psa->fFeatures & FADF_HAVEVARTYPE)
1553 VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1554 *pvt = vt;
1556 else
1557 return E_INVALIDARG;
1559 return S_OK;
1562 /************************************************************************
1563 * SafeArraySetRecordInfo (OLEAUT32.@)
1565 * Set the record info for a SafeArray.
1567 * PARAMS
1568 * psa [I] Array to set the record info for
1569 * pRinfo [I] Record info
1571 * RETURNS
1572 * Success: S_OK. The record info is stored with the array.
1573 * Failure: An HRESULT error code indicating the error.
1575 * NOTES
1576 * See SafeArray.
1578 HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1580 IRecordInfo** dest = (IRecordInfo**)psa;
1582 TRACE("(%p,%p)\n", psa, pRinfo);
1584 if (!psa || !(psa->fFeatures & FADF_RECORD))
1585 return E_INVALIDARG;
1587 if (pRinfo)
1588 IRecordInfo_AddRef(pRinfo);
1590 if (dest[-1])
1591 IRecordInfo_Release(dest[-1]);
1593 dest[-1] = pRinfo;
1594 return S_OK;
1597 /************************************************************************
1598 * SafeArrayGetRecordInfo (OLEAUT32.@)
1600 * Get the record info from a SafeArray.
1602 * PARAMS
1603 * psa [I] Array to get the record info from
1604 * pRinfo [O] Destination for the record info
1606 * RETURNS
1607 * Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1608 * Failure: An HRESULT error code indicating the error.
1610 * NOTES
1611 * See SafeArray.
1613 HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1615 IRecordInfo** src = (IRecordInfo**)psa;
1617 TRACE("(%p,%p)\n", psa, pRinfo);
1619 if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1620 return E_INVALIDARG;
1622 *pRinfo = src[-1];
1624 if (*pRinfo)
1625 IRecordInfo_AddRef(*pRinfo);
1626 return S_OK;
1629 /************************************************************************
1630 * SafeArraySetIID (OLEAUT32.@)
1632 * Set the IID for a SafeArray.
1634 * PARAMS
1635 * psa [I] Array to set the IID from
1636 * guid [I] IID
1638 * RETURNS
1639 * Success: S_OK. The IID is stored with the array
1640 * Failure: An HRESULT error code indicating the error.
1642 * NOTES
1643 * See SafeArray.
1645 HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1647 GUID* dest = (GUID*)psa;
1649 TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1651 if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1652 return E_INVALIDARG;
1654 dest[-1] = *guid;
1655 return S_OK;
1658 /************************************************************************
1659 * SafeArrayGetIID (OLEAUT32.@)
1661 * Get the IID from a SafeArray.
1663 * PARAMS
1664 * psa [I] Array to get the ID from
1665 * pGuid [O] Destination for the IID
1667 * RETURNS
1668 * Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1669 * Failure: An HRESULT error code indicating the error.
1671 * NOTES
1672 * See SafeArray.
1674 HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1676 GUID* src = (GUID*)psa;
1678 TRACE("(%p,%p)\n", psa, pGuid);
1680 if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1681 return E_INVALIDARG;
1683 *pGuid = src[-1];
1684 return S_OK;
1687 /************************************************************************
1688 * VectorFromBstr (OLEAUT32.@)
1690 * Create a SafeArray Vector from the bytes of a BSTR.
1692 * PARAMS
1693 * bstr [I] String to get bytes from
1694 * ppsa [O] Destination for the array
1696 * RETURNS
1697 * Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1698 * Failure: An HRESULT error code indicating the error.
1700 * NOTES
1701 * See SafeArray.
1703 HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1705 SAFEARRAYBOUND sab;
1707 TRACE("(%p,%p)\n", bstr, ppsa);
1709 if (!ppsa)
1710 return E_INVALIDARG;
1712 sab.lLbound = 0;
1713 sab.cElements = SysStringByteLen(bstr);
1715 *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1717 if (*ppsa)
1719 memcpy((*ppsa)->pvData, bstr, sab.cElements);
1720 return S_OK;
1722 return E_OUTOFMEMORY;
1725 /************************************************************************
1726 * BstrFromVector (OLEAUT32.@)
1728 * Create a BSTR from a SafeArray.
1730 * PARAMS
1731 * psa [I] Source array
1732 * pbstr [O] Destination for output BSTR
1734 * RETURNS
1735 * Success: S_OK. pbstr contains the arrays data.
1736 * Failure: An HRESULT error code indicating the error.
1738 * NOTES
1739 * psa must be a 1 dimensional array of a 1 byte type.
1741 * NOTES
1742 * See SafeArray.
1744 HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1746 TRACE("(%p,%p)\n", psa, pbstr);
1748 if (!pbstr)
1749 return E_INVALIDARG;
1751 *pbstr = NULL;
1753 if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1754 return E_INVALIDARG;
1756 *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1757 if (!*pbstr)
1758 return E_OUTOFMEMORY;
1759 return S_OK;