xref: /reactos/dll/win32/oleaut32/safearray.c (revision ae24453e)
1c2c66affSColin Finck /*************************************************************************
2c2c66affSColin Finck  * OLE Automation - SafeArray
3c2c66affSColin Finck  *
4c2c66affSColin Finck  * This file contains the implementation of the SafeArray functions.
5c2c66affSColin Finck  *
6c2c66affSColin Finck  * Copyright 1999 Sylvain St-Germain
7c2c66affSColin Finck  * Copyright 2002-2003 Marcus Meissner
8c2c66affSColin Finck  * Copyright 2003 Jon Griffiths
9c2c66affSColin Finck  *
10c2c66affSColin Finck  * This library is free software; you can redistribute it and/or
11c2c66affSColin Finck  * modify it under the terms of the GNU Lesser General Public
12c2c66affSColin Finck  * License as published by the Free Software Foundation; either
13c2c66affSColin Finck  * version 2.1 of the License, or (at your option) any later version.
14c2c66affSColin Finck  *
15c2c66affSColin Finck  * This library is distributed in the hope that it will be useful,
16c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18c2c66affSColin Finck  * Lesser General Public License for more details.
19c2c66affSColin Finck  *
20c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public
21c2c66affSColin Finck  * License along with this library; if not, write to the Free Software
22c2c66affSColin Finck  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23c2c66affSColin Finck  */
24c2c66affSColin Finck /* Memory Layout of a SafeArray:
25c2c66affSColin Finck  *
26c2c66affSColin Finck  * -0x10: start of memory.
27c2c66affSColin Finck  * -0x10: GUID for VT_DISPATCH and VT_UNKNOWN safearrays (if FADF_HAVEIID)
28c2c66affSColin Finck  * -0x04: DWORD varianttype; (for all others, except VT_RECORD) (if FADF_HAVEVARTYPE)
29c2c66affSColin Finck  *  -0x4: IRecordInfo* iface;  (if FADF_RECORD, for VT_RECORD (can be NULL))
30c2c66affSColin Finck  *  0x00: SAFEARRAY,
31c2c66affSColin Finck  *  0x10: SAFEARRAYBOUNDS[0...]
32c2c66affSColin Finck  */
33c2c66affSColin Finck 
34d506c2afSAmine Khaldi #include <string.h>
35d506c2afSAmine Khaldi #include <stdarg.h>
36d506c2afSAmine Khaldi #include <stdio.h>
37d506c2afSAmine Khaldi 
38d506c2afSAmine Khaldi #define COBJMACROS
39d506c2afSAmine Khaldi 
40d506c2afSAmine Khaldi #include "windef.h"
41d506c2afSAmine Khaldi #include "winerror.h"
42d506c2afSAmine Khaldi #include "winbase.h"
43d506c2afSAmine Khaldi #include "variant.h"
44d506c2afSAmine Khaldi #include "wine/debug.h"
45c2c66affSColin Finck 
46c2c66affSColin Finck WINE_DEFAULT_DEBUG_CHANNEL(variant);
47c2c66affSColin Finck 
48c2c66affSColin Finck /************************************************************************
49c2c66affSColin Finck  * SafeArray {OLEAUT32}
50c2c66affSColin Finck  *
51c2c66affSColin Finck  * NOTES
52c2c66affSColin Finck  * The SafeArray data type provides the underlying interface for Ole
53c2c66affSColin Finck  * Automations arrays, used for example to represent array types in
54c2c66affSColin Finck  * Visual Basic(tm) and to gather user defined parameters for invocation through
55c2c66affSColin Finck  * an IDispatch interface.
56c2c66affSColin Finck  *
57c2c66affSColin Finck  * Safe arrays provide bounds checking and automatically manage the data
58c2c66affSColin Finck  * types they contain, for example handing reference counting and copying
59c2c66affSColin Finck  * of interface pointers. User defined types can be stored in arrays
60c2c66affSColin Finck  * using the IRecordInfo interface.
61c2c66affSColin Finck  *
62c2c66affSColin Finck  * There are two types of SafeArray, normal and vectors. Normal arrays can have
63c2c66affSColin Finck  * multiple dimensions and the data for the array is allocated separately from
64c2c66affSColin Finck  * the array header. This is the most flexible type of array. Vectors, on the
65c2c66affSColin Finck  * other hand, are fixed in size and consist of a single allocated block, and a
66c2c66affSColin Finck  * single dimension.
67c2c66affSColin Finck  *
68c2c66affSColin Finck  * DATATYPES
69c2c66affSColin Finck  * The following types of data can be stored within a SafeArray.
70c2c66affSColin Finck  * Numeric:
71c2c66affSColin Finck  *|  VT_I1, VT_UI1, VT_I2, VT_UI2, VT_I4, VT_UI4, VT_I8, VT_UI8, VT_INT, VT_UINT,
72c2c66affSColin Finck  *|  VT_R4, VT_R8, VT_CY, VT_DECIMAL
73c2c66affSColin Finck  * Interfaces:
74c2c66affSColin Finck  *|  VT_DISPATCH, VT_UNKNOWN, VT_RECORD
75c2c66affSColin Finck  * Other:
76c2c66affSColin Finck  *|  VT_VARIANT, VT_INT_PTR, VT_UINT_PTR, VT_BOOL, VT_ERROR, VT_DATE, VT_BSTR
77c2c66affSColin Finck  *
78c2c66affSColin Finck  * FUNCTIONS
79c2c66affSColin Finck  *  BstrFromVector()
80c2c66affSColin Finck  *  VectorFromBstr()
81c2c66affSColin Finck  */
82c2c66affSColin Finck 
83c2c66affSColin Finck /* Undocumented hidden space before the start of a SafeArray descriptor */
84c2c66affSColin Finck #define SAFEARRAY_HIDDEN_SIZE sizeof(GUID)
85c2c66affSColin Finck 
86c2c66affSColin Finck /* features listed here are not propagated to newly created array or data copy
87c2c66affSColin Finck    created with SafeArrayCopy()/SafeArrayCopyData() */
88c2c66affSColin Finck static const USHORT ignored_copy_features =
89c2c66affSColin Finck         FADF_AUTO |
90c2c66affSColin Finck         FADF_STATIC |
91c2c66affSColin Finck         FADF_EMBEDDED |
92c2c66affSColin Finck         FADF_FIXEDSIZE |
93c2c66affSColin Finck         FADF_CREATEVECTOR;
94c2c66affSColin Finck 
95c2c66affSColin Finck /* Allocate memory */
SAFEARRAY_Malloc(ULONG size)96c2c66affSColin Finck static inline void* SAFEARRAY_Malloc(ULONG size)
97c2c66affSColin Finck {
98c2c66affSColin Finck   void *ret = CoTaskMemAlloc(size);
99c2c66affSColin Finck   if (ret)
100c2c66affSColin Finck     memset(ret, 0, size);
101c2c66affSColin Finck   return ret;
102c2c66affSColin Finck }
103c2c66affSColin Finck 
104c2c66affSColin Finck /* Free memory */
SAFEARRAY_Free(void * ptr)105c2c66affSColin Finck static inline void SAFEARRAY_Free(void *ptr)
106c2c66affSColin Finck {
107c2c66affSColin Finck   CoTaskMemFree(ptr);
108c2c66affSColin Finck }
109c2c66affSColin Finck 
110c2c66affSColin Finck /* Get the size of a supported VT type (0 means unsupported) */
SAFEARRAY_GetVTSize(VARTYPE vt)111c2c66affSColin Finck static DWORD SAFEARRAY_GetVTSize(VARTYPE vt)
112c2c66affSColin Finck {
113c2c66affSColin Finck   switch (vt)
114c2c66affSColin Finck   {
115c2c66affSColin Finck     case VT_I1:
116c2c66affSColin Finck     case VT_UI1:      return sizeof(BYTE);
117c2c66affSColin Finck     case VT_BOOL:
118c2c66affSColin Finck     case VT_I2:
119c2c66affSColin Finck     case VT_UI2:      return sizeof(SHORT);
120c2c66affSColin Finck     case VT_I4:
121c2c66affSColin Finck     case VT_UI4:
122c2c66affSColin Finck     case VT_R4:
123c2c66affSColin Finck     case VT_ERROR:    return sizeof(LONG);
124c2c66affSColin Finck     case VT_R8:
125c2c66affSColin Finck     case VT_I8:
126c2c66affSColin Finck     case VT_UI8:      return sizeof(LONG64);
127c2c66affSColin Finck     case VT_INT:
128c2c66affSColin Finck     case VT_UINT:     return sizeof(INT);
129c2c66affSColin Finck     case VT_INT_PTR:
130c2c66affSColin Finck     case VT_UINT_PTR: return sizeof(UINT_PTR);
131c2c66affSColin Finck     case VT_CY:       return sizeof(CY);
132c2c66affSColin Finck     case VT_DATE:     return sizeof(DATE);
133c2c66affSColin Finck     case VT_BSTR:     return sizeof(BSTR);
134c2c66affSColin Finck     case VT_DISPATCH: return sizeof(LPDISPATCH);
135c2c66affSColin Finck     case VT_VARIANT:  return sizeof(VARIANT);
136c2c66affSColin Finck     case VT_UNKNOWN:  return sizeof(LPUNKNOWN);
137c2c66affSColin Finck     case VT_DECIMAL:  return sizeof(DECIMAL);
138c2c66affSColin Finck     /* Note: Return a non-zero size to indicate vt is valid. The actual size
139c2c66affSColin Finck      * of a UDT is taken from the result of IRecordInfo_GetSize().
140c2c66affSColin Finck      */
141c2c66affSColin Finck     case VT_RECORD:   return 32;
142c2c66affSColin Finck   }
143c2c66affSColin Finck   return 0;
144c2c66affSColin Finck }
145c2c66affSColin Finck 
146c2c66affSColin Finck /* Set the hidden data for an array */
SAFEARRAY_SetHiddenDWORD(SAFEARRAY * psa,DWORD dw)147c2c66affSColin Finck static inline void SAFEARRAY_SetHiddenDWORD(SAFEARRAY* psa, DWORD dw)
148c2c66affSColin Finck {
149c2c66affSColin Finck   /* Implementation data is stored in the 4 bytes before the header */
150c2c66affSColin Finck   LPDWORD lpDw = (LPDWORD)psa;
151c2c66affSColin Finck   lpDw[-1] = dw;
152c2c66affSColin Finck }
153c2c66affSColin Finck 
154c2c66affSColin Finck /* Get the hidden data from an array */
SAFEARRAY_GetHiddenDWORD(SAFEARRAY * psa)155c2c66affSColin Finck static inline DWORD SAFEARRAY_GetHiddenDWORD(SAFEARRAY* psa)
156c2c66affSColin Finck {
157c2c66affSColin Finck   LPDWORD lpDw = (LPDWORD)psa;
158c2c66affSColin Finck   return lpDw[-1];
159c2c66affSColin Finck }
160c2c66affSColin Finck 
161c2c66affSColin Finck /* Get the number of cells in a SafeArray */
SAFEARRAY_GetCellCount(const SAFEARRAY * psa)162c2c66affSColin Finck static ULONG SAFEARRAY_GetCellCount(const SAFEARRAY *psa)
163c2c66affSColin Finck {
164c2c66affSColin Finck   const SAFEARRAYBOUND* psab = psa->rgsabound;
165c2c66affSColin Finck   USHORT cCount = psa->cDims;
166c2c66affSColin Finck   ULONG ulNumCells = 1;
167c2c66affSColin Finck 
168c2c66affSColin Finck   while (cCount--)
169c2c66affSColin Finck   {
170c2c66affSColin Finck     /* This is a valid bordercase. See testcases. -Marcus */
171c2c66affSColin Finck     if (!psab->cElements)
172c2c66affSColin Finck       return 0;
173c2c66affSColin Finck     ulNumCells *= psab->cElements;
174c2c66affSColin Finck     psab++;
175c2c66affSColin Finck   }
176c2c66affSColin Finck   return ulNumCells;
177c2c66affSColin Finck }
178c2c66affSColin Finck 
179c2c66affSColin Finck /* Allocate a descriptor for an array */
SAFEARRAY_AllocDescriptor(ULONG ulSize,SAFEARRAY ** ppsaOut)180c2c66affSColin Finck static HRESULT SAFEARRAY_AllocDescriptor(ULONG ulSize, SAFEARRAY **ppsaOut)
181c2c66affSColin Finck {
182c2c66affSColin Finck   char *ptr = SAFEARRAY_Malloc(ulSize + SAFEARRAY_HIDDEN_SIZE);
183c2c66affSColin Finck 
184c2c66affSColin Finck   if (!ptr)
185c2c66affSColin Finck   {
186c2c66affSColin Finck       *ppsaOut = NULL;
187c2c66affSColin Finck       return E_OUTOFMEMORY;
188c2c66affSColin Finck   }
189c2c66affSColin Finck 
190c2c66affSColin Finck   *ppsaOut = (SAFEARRAY*)(ptr + SAFEARRAY_HIDDEN_SIZE);
191c2c66affSColin Finck   return S_OK;
192c2c66affSColin Finck }
193c2c66affSColin Finck 
194c2c66affSColin Finck /* Set the features of an array */
SAFEARRAY_SetFeatures(VARTYPE vt,SAFEARRAY * psa)195c2c66affSColin Finck static void SAFEARRAY_SetFeatures(VARTYPE vt, SAFEARRAY *psa)
196c2c66affSColin Finck {
197c2c66affSColin Finck   /* Set the IID if we have one, otherwise set the type */
198c2c66affSColin Finck   if (vt == VT_DISPATCH)
199c2c66affSColin Finck   {
200c2c66affSColin Finck     psa->fFeatures = FADF_HAVEIID;
201c2c66affSColin Finck     SafeArraySetIID(psa, &IID_IDispatch);
202c2c66affSColin Finck   }
203c2c66affSColin Finck   else if (vt == VT_UNKNOWN)
204c2c66affSColin Finck   {
205c2c66affSColin Finck     psa->fFeatures = FADF_HAVEIID;
206c2c66affSColin Finck     SafeArraySetIID(psa, &IID_IUnknown);
207c2c66affSColin Finck   }
208c2c66affSColin Finck   else if (vt == VT_RECORD)
209c2c66affSColin Finck     psa->fFeatures = FADF_RECORD;
210c2c66affSColin Finck   else
211c2c66affSColin Finck   {
212c2c66affSColin Finck     psa->fFeatures = FADF_HAVEVARTYPE;
213c2c66affSColin Finck     SAFEARRAY_SetHiddenDWORD(psa, vt);
214c2c66affSColin Finck   }
215c2c66affSColin Finck }
216c2c66affSColin Finck 
217c2c66affSColin Finck /* Create an array */
SAFEARRAY_Create(VARTYPE vt,UINT cDims,const SAFEARRAYBOUND * rgsabound,ULONG ulSize)218c2c66affSColin Finck static SAFEARRAY* SAFEARRAY_Create(VARTYPE vt, UINT cDims, const SAFEARRAYBOUND *rgsabound, ULONG ulSize)
219c2c66affSColin Finck {
220c2c66affSColin Finck   SAFEARRAY *psa = NULL;
221c2c66affSColin Finck   unsigned int i;
222c2c66affSColin Finck 
223c2c66affSColin Finck   if (!rgsabound)
224c2c66affSColin Finck     return NULL;
225c2c66affSColin Finck 
226c2c66affSColin Finck   if (SUCCEEDED(SafeArrayAllocDescriptorEx(vt, cDims, &psa)))
227c2c66affSColin Finck   {
228c2c66affSColin Finck     switch (vt)
229c2c66affSColin Finck     {
230c2c66affSColin Finck       case VT_BSTR:     psa->fFeatures |= FADF_BSTR; break;
231c2c66affSColin Finck       case VT_UNKNOWN:  psa->fFeatures |= FADF_UNKNOWN; break;
232c2c66affSColin Finck       case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
233c2c66affSColin Finck       case VT_VARIANT:  psa->fFeatures |= FADF_VARIANT; break;
234c2c66affSColin Finck     }
235c2c66affSColin Finck 
236c2c66affSColin Finck     for (i = 0; i < cDims; i++)
237c2c66affSColin Finck       memcpy(psa->rgsabound + i, rgsabound + cDims - 1 - i, sizeof(SAFEARRAYBOUND));
238c2c66affSColin Finck 
239c2c66affSColin Finck     if (ulSize)
240c2c66affSColin Finck       psa->cbElements = ulSize;
241c2c66affSColin Finck 
242c2c66affSColin Finck     if (!psa->cbElements || FAILED(SafeArrayAllocData(psa)))
243c2c66affSColin Finck     {
244c2c66affSColin Finck       SafeArrayDestroyDescriptor(psa);
245c2c66affSColin Finck       psa = NULL;
246c2c66affSColin Finck     }
247c2c66affSColin Finck   }
248c2c66affSColin Finck   return psa;
249c2c66affSColin Finck }
250c2c66affSColin Finck 
251c2c66affSColin Finck /* Create an array as a vector */
SAFEARRAY_CreateVector(VARTYPE vt,LONG lLbound,ULONG cElements,ULONG ulSize)252c2c66affSColin Finck static SAFEARRAY* SAFEARRAY_CreateVector(VARTYPE vt, LONG lLbound, ULONG cElements, ULONG ulSize)
253c2c66affSColin Finck {
254c2c66affSColin Finck   SAFEARRAY *psa = NULL;
255c2c66affSColin Finck 
256c2c66affSColin Finck   if (ulSize || (vt == VT_RECORD))
257c2c66affSColin Finck   {
258c2c66affSColin Finck     /* Allocate the header and data together */
259c2c66affSColin Finck     if (SUCCEEDED(SAFEARRAY_AllocDescriptor(sizeof(SAFEARRAY) + ulSize * cElements, &psa)))
260c2c66affSColin Finck     {
261c2c66affSColin Finck       SAFEARRAY_SetFeatures(vt, psa);
262c2c66affSColin Finck 
263c2c66affSColin Finck       psa->cDims = 1;
264c2c66affSColin Finck       psa->fFeatures |= FADF_CREATEVECTOR;
265c2c66affSColin Finck       psa->pvData = &psa[1]; /* Data follows the header */
266c2c66affSColin Finck       psa->cbElements = ulSize;
267c2c66affSColin Finck       psa->rgsabound[0].cElements = cElements;
268c2c66affSColin Finck       psa->rgsabound[0].lLbound = lLbound;
269c2c66affSColin Finck 
270c2c66affSColin Finck       switch (vt)
271c2c66affSColin Finck       {
272c2c66affSColin Finck         case VT_BSTR:     psa->fFeatures |= FADF_BSTR; break;
273c2c66affSColin Finck         case VT_UNKNOWN:  psa->fFeatures |= FADF_UNKNOWN; break;
274c2c66affSColin Finck         case VT_DISPATCH: psa->fFeatures |= FADF_DISPATCH; break;
275c2c66affSColin Finck         case VT_VARIANT:  psa->fFeatures |= FADF_VARIANT; break;
276c2c66affSColin Finck       }
277c2c66affSColin Finck     }
278c2c66affSColin Finck   }
279c2c66affSColin Finck   return psa;
280c2c66affSColin Finck }
281c2c66affSColin Finck 
282c2c66affSColin Finck /* Free data items in an array */
SAFEARRAY_DestroyData(SAFEARRAY * psa,ULONG ulStartCell)283c2c66affSColin Finck static HRESULT SAFEARRAY_DestroyData(SAFEARRAY *psa, ULONG ulStartCell)
284c2c66affSColin Finck {
285c2c66affSColin Finck   if (psa->pvData && !(psa->fFeatures & FADF_DATADELETED))
286c2c66affSColin Finck   {
287c2c66affSColin Finck     ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
288c2c66affSColin Finck 
289c2c66affSColin Finck     if (ulStartCell > ulCellCount) {
2905db885caSAmine Khaldi       FIXME("unexpected ulCellCount %d, start %d\n",ulCellCount,ulStartCell);
291c2c66affSColin Finck       return E_UNEXPECTED;
292c2c66affSColin Finck     }
293c2c66affSColin Finck 
294c2c66affSColin Finck     ulCellCount -= ulStartCell;
295c2c66affSColin Finck 
296c2c66affSColin Finck     if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
297c2c66affSColin Finck     {
298c2c66affSColin Finck       LPUNKNOWN *lpUnknown = (LPUNKNOWN *)psa->pvData + ulStartCell;
299c2c66affSColin Finck 
300c2c66affSColin Finck       while(ulCellCount--)
301c2c66affSColin Finck       {
302c2c66affSColin Finck         if (*lpUnknown)
303c2c66affSColin Finck           IUnknown_Release(*lpUnknown);
304c2c66affSColin Finck         lpUnknown++;
305c2c66affSColin Finck       }
306c2c66affSColin Finck     }
307c2c66affSColin Finck     else if (psa->fFeatures & FADF_RECORD)
308c2c66affSColin Finck     {
309c2c66affSColin Finck       IRecordInfo *lpRecInfo;
310c2c66affSColin Finck 
311c2c66affSColin Finck       if (SUCCEEDED(SafeArrayGetRecordInfo(psa, &lpRecInfo)))
312c2c66affSColin Finck       {
313c2c66affSColin Finck         PBYTE pRecordData = psa->pvData;
314c2c66affSColin Finck         while(ulCellCount--)
315c2c66affSColin Finck         {
316c2c66affSColin Finck           IRecordInfo_RecordClear(lpRecInfo, pRecordData);
317c2c66affSColin Finck           pRecordData += psa->cbElements;
318c2c66affSColin Finck         }
319c2c66affSColin Finck         IRecordInfo_Release(lpRecInfo);
320c2c66affSColin Finck       }
321c2c66affSColin Finck     }
322c2c66affSColin Finck     else if (psa->fFeatures & FADF_BSTR)
323c2c66affSColin Finck     {
324c2c66affSColin Finck       BSTR* lpBstr = (BSTR*)psa->pvData + ulStartCell;
325c2c66affSColin Finck 
326c2c66affSColin Finck       while(ulCellCount--)
327c2c66affSColin Finck       {
328c2c66affSColin Finck         SysFreeString(*lpBstr);
329c2c66affSColin Finck         lpBstr++;
330c2c66affSColin Finck       }
331c2c66affSColin Finck     }
332c2c66affSColin Finck     else if (psa->fFeatures & FADF_VARIANT)
333c2c66affSColin Finck     {
334c2c66affSColin Finck       VARIANT* lpVariant = (VARIANT*)psa->pvData + ulStartCell;
335c2c66affSColin Finck 
336c2c66affSColin Finck       while(ulCellCount--)
337c2c66affSColin Finck       {
338c2c66affSColin Finck         HRESULT hRet = VariantClear(lpVariant);
339c2c66affSColin Finck 
340c2c66affSColin Finck         if (FAILED(hRet)) FIXME("VariantClear of element failed!\n");
341c2c66affSColin Finck         lpVariant++;
342c2c66affSColin Finck       }
343c2c66affSColin Finck     }
344c2c66affSColin Finck   }
345c2c66affSColin Finck   return S_OK;
346c2c66affSColin Finck }
347c2c66affSColin Finck 
348c2c66affSColin Finck /* Copy data items from one array to another. Destination data is freed before copy. */
SAFEARRAY_CopyData(SAFEARRAY * psa,SAFEARRAY * dest)349c2c66affSColin Finck static HRESULT SAFEARRAY_CopyData(SAFEARRAY *psa, SAFEARRAY *dest)
350c2c66affSColin Finck {
351c2c66affSColin Finck   HRESULT hr = S_OK;
352c2c66affSColin Finck 
353c2c66affSColin Finck   if (!psa->pvData)
354c2c66affSColin Finck     return S_OK;
355c2c66affSColin Finck 
356c2c66affSColin Finck   if (!dest->pvData || psa->fFeatures & FADF_DATADELETED)
357c2c66affSColin Finck     return E_INVALIDARG;
358c2c66affSColin Finck   else
359c2c66affSColin Finck   {
360c2c66affSColin Finck     ULONG ulCellCount = SAFEARRAY_GetCellCount(psa);
361c2c66affSColin Finck 
362c2c66affSColin Finck     dest->fFeatures = (dest->fFeatures & FADF_CREATEVECTOR) | (psa->fFeatures & ~ignored_copy_features);
363c2c66affSColin Finck 
364c2c66affSColin Finck     if (psa->fFeatures & FADF_VARIANT)
365c2c66affSColin Finck     {
366c2c66affSColin Finck       VARIANT *src_var = psa->pvData;
367c2c66affSColin Finck       VARIANT *dest_var = dest->pvData;
368c2c66affSColin Finck 
369c2c66affSColin Finck       while(ulCellCount--)
370c2c66affSColin Finck       {
371c2c66affSColin Finck         HRESULT hRet;
372c2c66affSColin Finck 
373c2c66affSColin Finck         /* destination is cleared automatically */
374c2c66affSColin Finck         hRet = VariantCopy(dest_var, src_var);
375c2c66affSColin Finck         if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%08x, element %u\n", hRet, ulCellCount);
376c2c66affSColin Finck         src_var++;
377c2c66affSColin Finck         dest_var++;
378c2c66affSColin Finck       }
379c2c66affSColin Finck     }
380c2c66affSColin Finck     else if (psa->fFeatures & FADF_BSTR)
381c2c66affSColin Finck     {
382c2c66affSColin Finck       BSTR *src_bstr = psa->pvData;
383c2c66affSColin Finck       BSTR *dest_bstr = dest->pvData;
384c2c66affSColin Finck 
385c2c66affSColin Finck       while(ulCellCount--)
386c2c66affSColin Finck       {
387c2c66affSColin Finck         SysFreeString(*dest_bstr);
388c2c66affSColin Finck         if (*src_bstr)
389c2c66affSColin Finck         {
390c2c66affSColin Finck           *dest_bstr = SysAllocStringByteLen((char*)*src_bstr, SysStringByteLen(*src_bstr));
391c2c66affSColin Finck           if (!*dest_bstr)
392c2c66affSColin Finck             return E_OUTOFMEMORY;
393c2c66affSColin Finck         }
394c2c66affSColin Finck         else
395c2c66affSColin Finck           *dest_bstr = NULL;
396c2c66affSColin Finck         src_bstr++;
397c2c66affSColin Finck         dest_bstr++;
398c2c66affSColin Finck       }
399c2c66affSColin Finck     }
400c2c66affSColin Finck     else if (psa->fFeatures & FADF_RECORD)
401c2c66affSColin Finck     {
402c2c66affSColin Finck       BYTE *dest_data = dest->pvData;
403c2c66affSColin Finck       BYTE *src_data = psa->pvData;
404c2c66affSColin Finck       IRecordInfo *record;
405c2c66affSColin Finck 
406c2c66affSColin Finck       SafeArrayGetRecordInfo(psa, &record);
407c2c66affSColin Finck       while (ulCellCount--)
408c2c66affSColin Finck       {
409c2c66affSColin Finck           /* RecordCopy() clears destination record */
410c2c66affSColin Finck           hr = IRecordInfo_RecordCopy(record, src_data, dest_data);
411c2c66affSColin Finck           if (FAILED(hr)) break;
412c2c66affSColin Finck           src_data += psa->cbElements;
413c2c66affSColin Finck           dest_data += psa->cbElements;
414c2c66affSColin Finck       }
415c2c66affSColin Finck 
416c2c66affSColin Finck       SafeArraySetRecordInfo(dest, record);
417c2c66affSColin Finck       /* This value is set to 32 bytes by default on descriptor creation,
418c2c66affSColin Finck          update with actual structure size. */
419c2c66affSColin Finck       dest->cbElements = psa->cbElements;
420c2c66affSColin Finck       IRecordInfo_Release(record);
421c2c66affSColin Finck     }
422c2c66affSColin Finck     else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
423c2c66affSColin Finck     {
424c2c66affSColin Finck       IUnknown **dest_unk = dest->pvData;
425c2c66affSColin Finck       IUnknown **src_unk = psa->pvData;
426c2c66affSColin Finck 
427c2c66affSColin Finck       /* release old iface, addref new one */
428c2c66affSColin Finck       while (ulCellCount--)
429c2c66affSColin Finck       {
430c2c66affSColin Finck           if (*dest_unk)
431c2c66affSColin Finck               IUnknown_Release(*dest_unk);
432c2c66affSColin Finck           *dest_unk = *src_unk;
433c2c66affSColin Finck           if (*dest_unk)
434c2c66affSColin Finck               IUnknown_AddRef(*dest_unk);
435c2c66affSColin Finck           src_unk++;
436c2c66affSColin Finck           dest_unk++;
437c2c66affSColin Finck       }
438c2c66affSColin Finck     }
439c2c66affSColin Finck     else
440c2c66affSColin Finck     {
441c2c66affSColin Finck       /* Copy the data over */
442c2c66affSColin Finck       memcpy(dest->pvData, psa->pvData, ulCellCount * psa->cbElements);
443c2c66affSColin Finck     }
444c2c66affSColin Finck 
445c2c66affSColin Finck     if (psa->fFeatures & FADF_HAVEIID)
446c2c66affSColin Finck     {
447c2c66affSColin Finck       GUID guid;
448c2c66affSColin Finck       SafeArrayGetIID(psa, &guid);
449c2c66affSColin Finck       SafeArraySetIID(dest, &guid);
450c2c66affSColin Finck     }
451c2c66affSColin Finck     else if (psa->fFeatures & FADF_HAVEVARTYPE)
452c2c66affSColin Finck     {
453c2c66affSColin Finck       SAFEARRAY_SetHiddenDWORD(dest, SAFEARRAY_GetHiddenDWORD(psa));
454c2c66affSColin Finck     }
455c2c66affSColin Finck   }
456c2c66affSColin Finck 
457c2c66affSColin Finck   return hr;
458c2c66affSColin Finck }
459c2c66affSColin Finck 
460c2c66affSColin Finck /*************************************************************************
461c2c66affSColin Finck  *		SafeArrayAllocDescriptor (OLEAUT32.36)
462c2c66affSColin Finck  *
463c2c66affSColin Finck  * Allocate and initialise a descriptor for a SafeArray.
464c2c66affSColin Finck  *
465c2c66affSColin Finck  * PARAMS
466c2c66affSColin Finck  *  cDims   [I] Number of dimensions of the array
467c2c66affSColin Finck  *  ppsaOut [O] Destination for new descriptor
468c2c66affSColin Finck  *
469c2c66affSColin Finck  * RETURNS
470c2c66affSColin Finck  * Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
471c2c66affSColin Finck  * Failure: An HRESULT error code indicating the error.
472c2c66affSColin Finck  *
473c2c66affSColin Finck  * NOTES
474c2c66affSColin Finck  * See SafeArray.
475c2c66affSColin Finck  */
SafeArrayAllocDescriptor(UINT cDims,SAFEARRAY ** ppsaOut)476c2c66affSColin Finck HRESULT WINAPI SafeArrayAllocDescriptor(UINT cDims, SAFEARRAY **ppsaOut)
477c2c66affSColin Finck {
478c2c66affSColin Finck   LONG allocSize;
479c2c66affSColin Finck   HRESULT hr;
480c2c66affSColin Finck 
481c2c66affSColin Finck   TRACE("(%d,%p)\n", cDims, ppsaOut);
482c2c66affSColin Finck 
483c2c66affSColin Finck   if (!cDims || cDims >= 0x10000) /* Maximum 65535 dimensions */
484c2c66affSColin Finck     return E_INVALIDARG;
485c2c66affSColin Finck 
486c2c66affSColin Finck   if (!ppsaOut)
487c2c66affSColin Finck     return E_POINTER;
488c2c66affSColin Finck 
489c2c66affSColin Finck   /* We need enough space for the header and its bounds */
490c2c66affSColin Finck   allocSize = sizeof(SAFEARRAY) + sizeof(SAFEARRAYBOUND) * (cDims - 1);
491c2c66affSColin Finck 
492c2c66affSColin Finck   hr = SAFEARRAY_AllocDescriptor(allocSize, ppsaOut);
493c2c66affSColin Finck   if (FAILED(hr))
494c2c66affSColin Finck     return hr;
495c2c66affSColin Finck 
496c2c66affSColin Finck   (*ppsaOut)->cDims = cDims;
497c2c66affSColin Finck 
498c2c66affSColin Finck   TRACE("(%d): %u bytes allocated for descriptor.\n", cDims, allocSize);
499c2c66affSColin Finck   return S_OK;
500c2c66affSColin Finck }
501c2c66affSColin Finck 
502c2c66affSColin Finck /*************************************************************************
503c2c66affSColin Finck  *		SafeArrayAllocDescriptorEx (OLEAUT32.41)
504c2c66affSColin Finck  *
505c2c66affSColin Finck  * Allocate and initialise a descriptor for a SafeArray of a given type.
506c2c66affSColin Finck  *
507c2c66affSColin Finck  * PARAMS
508c2c66affSColin Finck  *  vt      [I] The type of items to store in the array
509c2c66affSColin Finck  *  cDims   [I] Number of dimensions of the array
510c2c66affSColin Finck  *  ppsaOut [O] Destination for new descriptor
511c2c66affSColin Finck  *
512c2c66affSColin Finck  * RETURNS
513c2c66affSColin Finck  *  Success: S_OK. ppsaOut is filled with a newly allocated descriptor.
514c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
515c2c66affSColin Finck  *
516c2c66affSColin Finck  * NOTES
517c2c66affSColin Finck  *  - This function does not check that vt is an allowed VARTYPE.
518c2c66affSColin Finck  *  - Unlike SafeArrayAllocDescriptor(), vt is associated with the array.
519c2c66affSColin Finck  *  See SafeArray.
520c2c66affSColin Finck  */
SafeArrayAllocDescriptorEx(VARTYPE vt,UINT cDims,SAFEARRAY ** ppsaOut)521c2c66affSColin Finck HRESULT WINAPI SafeArrayAllocDescriptorEx(VARTYPE vt, UINT cDims, SAFEARRAY **ppsaOut)
522c2c66affSColin Finck {
523c2c66affSColin Finck   ULONG cbElements;
524c2c66affSColin Finck   HRESULT hRet;
525c2c66affSColin Finck 
526*ae24453eSAmine Khaldi   TRACE("(%s,%u,%p)\n", debugstr_vt(vt), cDims, ppsaOut);
527c2c66affSColin Finck 
528c2c66affSColin Finck   cbElements = SAFEARRAY_GetVTSize(vt);
529c2c66affSColin Finck   if (!cbElements)
530c2c66affSColin Finck     WARN("Creating a descriptor with an invalid VARTYPE!\n");
531c2c66affSColin Finck 
532c2c66affSColin Finck   hRet = SafeArrayAllocDescriptor(cDims, ppsaOut);
533c2c66affSColin Finck 
534c2c66affSColin Finck   if (SUCCEEDED(hRet))
535c2c66affSColin Finck   {
536c2c66affSColin Finck     SAFEARRAY_SetFeatures(vt, *ppsaOut);
537c2c66affSColin Finck     (*ppsaOut)->cbElements = cbElements;
538c2c66affSColin Finck   }
539c2c66affSColin Finck   return hRet;
540c2c66affSColin Finck }
541c2c66affSColin Finck 
542c2c66affSColin Finck /*************************************************************************
543c2c66affSColin Finck  *		SafeArrayAllocData (OLEAUT32.37)
544c2c66affSColin Finck  *
545c2c66affSColin Finck  * Allocate the data area of a SafeArray.
546c2c66affSColin Finck  *
547c2c66affSColin Finck  * PARAMS
548c2c66affSColin Finck  *  psa [I] SafeArray to allocate the data area of.
549c2c66affSColin Finck  *
550c2c66affSColin Finck  * RETURNS
551c2c66affSColin Finck  *  Success: S_OK. The data area is allocated and initialised.
552c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
553c2c66affSColin Finck  *
554c2c66affSColin Finck  * NOTES
555c2c66affSColin Finck  *  See SafeArray.
556c2c66affSColin Finck  */
SafeArrayAllocData(SAFEARRAY * psa)557c2c66affSColin Finck HRESULT WINAPI SafeArrayAllocData(SAFEARRAY *psa)
558c2c66affSColin Finck {
559c2c66affSColin Finck   HRESULT hRet = E_INVALIDARG;
560c2c66affSColin Finck 
561c2c66affSColin Finck   TRACE("(%p)\n", psa);
562c2c66affSColin Finck 
563c2c66affSColin Finck   if (psa)
564c2c66affSColin Finck   {
565c2c66affSColin Finck     ULONG ulSize = SAFEARRAY_GetCellCount(psa);
566c2c66affSColin Finck 
567c2c66affSColin Finck     psa->pvData = SAFEARRAY_Malloc(ulSize * psa->cbElements);
568c2c66affSColin Finck 
569c2c66affSColin Finck     if (psa->pvData)
570c2c66affSColin Finck     {
571c2c66affSColin Finck       hRet = S_OK;
572c2c66affSColin Finck       TRACE("%u bytes allocated for data at %p (%u objects).\n",
573c2c66affSColin Finck             ulSize * psa->cbElements, psa->pvData, ulSize);
574c2c66affSColin Finck     }
575c2c66affSColin Finck     else
576c2c66affSColin Finck       hRet = E_OUTOFMEMORY;
577c2c66affSColin Finck   }
578c2c66affSColin Finck   return hRet;
579c2c66affSColin Finck }
580c2c66affSColin Finck 
581c2c66affSColin Finck /*************************************************************************
582c2c66affSColin Finck  *		SafeArrayCreate (OLEAUT32.15)
583c2c66affSColin Finck  *
584c2c66affSColin Finck  * Create a new SafeArray.
585c2c66affSColin Finck  *
586c2c66affSColin Finck  * PARAMS
587c2c66affSColin Finck  *  vt        [I] Type to store in the safe array
588c2c66affSColin Finck  *  cDims     [I] Number of array dimensions
589c2c66affSColin Finck  *  rgsabound [I] Bounds of the array dimensions
590c2c66affSColin Finck  *
591c2c66affSColin Finck  * RETURNS
592c2c66affSColin Finck  *  Success: A pointer to a new array object.
593c2c66affSColin Finck  *  Failure: NULL, if any parameter is invalid or memory allocation fails.
594c2c66affSColin Finck  *
595c2c66affSColin Finck  * NOTES
596c2c66affSColin Finck  *  Win32 allows arrays with 0 sized dimensions. This bug is not reproduced
597c2c66affSColin Finck  *  in the Wine implementation.
598c2c66affSColin Finck  *  See SafeArray.
599c2c66affSColin Finck  */
SafeArrayCreate(VARTYPE vt,UINT cDims,SAFEARRAYBOUND * rgsabound)600c2c66affSColin Finck SAFEARRAY* WINAPI SafeArrayCreate(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound)
601c2c66affSColin Finck {
602*ae24453eSAmine Khaldi   TRACE("(%s,%u,%p)\n", debugstr_vt(vt), cDims, rgsabound);
603c2c66affSColin Finck 
604c2c66affSColin Finck   if (vt == VT_RECORD)
605c2c66affSColin Finck     return NULL;
606c2c66affSColin Finck 
607c2c66affSColin Finck   return SAFEARRAY_Create(vt, cDims, rgsabound, 0);
608c2c66affSColin Finck }
609c2c66affSColin Finck 
610c2c66affSColin Finck /*************************************************************************
611c2c66affSColin Finck  *		SafeArrayCreateEx (OLEAUT32.15)
612c2c66affSColin Finck  *
613c2c66affSColin Finck  * Create a new SafeArray.
614c2c66affSColin Finck  *
615c2c66affSColin Finck  * PARAMS
616c2c66affSColin Finck  *  vt        [I] Type to store in the safe array
617c2c66affSColin Finck  *  cDims     [I] Number of array dimensions
618c2c66affSColin Finck  *  rgsabound [I] Bounds of the array dimensions
619c2c66affSColin Finck  *  pvExtra   [I] Extra data
620c2c66affSColin Finck  *
621c2c66affSColin Finck  * RETURNS
622c2c66affSColin Finck  *  Success: A pointer to a new array object.
623c2c66affSColin Finck  *  Failure: NULL, if any parameter is invalid or memory allocation fails.
624c2c66affSColin Finck  *
625c2c66affSColin Finck  * NOTES
626c2c66affSColin Finck  * See SafeArray.
627c2c66affSColin Finck  */
SafeArrayCreateEx(VARTYPE vt,UINT cDims,SAFEARRAYBOUND * rgsabound,LPVOID pvExtra)628c2c66affSColin Finck SAFEARRAY* WINAPI SafeArrayCreateEx(VARTYPE vt, UINT cDims, SAFEARRAYBOUND *rgsabound, LPVOID pvExtra)
629c2c66affSColin Finck {
630c2c66affSColin Finck   ULONG ulSize = 0;
631c2c66affSColin Finck   IRecordInfo* iRecInfo = pvExtra;
632c2c66affSColin Finck   SAFEARRAY* psa;
633c2c66affSColin Finck 
634*ae24453eSAmine Khaldi   TRACE("(%s,%u,%p,%p)\n", debugstr_vt(vt), cDims, rgsabound, pvExtra);
635c2c66affSColin Finck 
636c2c66affSColin Finck   if (vt == VT_RECORD)
637c2c66affSColin Finck   {
638c2c66affSColin Finck     if  (!iRecInfo)
639c2c66affSColin Finck       return NULL;
640c2c66affSColin Finck     IRecordInfo_GetSize(iRecInfo, &ulSize);
641c2c66affSColin Finck   }
642c2c66affSColin Finck   psa = SAFEARRAY_Create(vt, cDims, rgsabound, ulSize);
643c2c66affSColin Finck 
644c2c66affSColin Finck   if (pvExtra)
645c2c66affSColin Finck   {
646c2c66affSColin Finck     switch(vt)
647c2c66affSColin Finck     {
648c2c66affSColin Finck       case VT_RECORD:
649c2c66affSColin Finck         SafeArraySetRecordInfo(psa, pvExtra);
650c2c66affSColin Finck         break;
651c2c66affSColin Finck       case VT_UNKNOWN:
652c2c66affSColin Finck       case VT_DISPATCH:
653c2c66affSColin Finck         SafeArraySetIID(psa, pvExtra);
654c2c66affSColin Finck         break;
655c2c66affSColin Finck     }
656c2c66affSColin Finck   }
657c2c66affSColin Finck   return psa;
658c2c66affSColin Finck }
659c2c66affSColin Finck 
660c2c66affSColin Finck /************************************************************************
661c2c66affSColin Finck  *		SafeArrayCreateVector (OLEAUT32.411)
662c2c66affSColin Finck  *
663c2c66affSColin Finck  * Create a one dimensional, contiguous SafeArray.
664c2c66affSColin Finck  *
665c2c66affSColin Finck  * PARAMS
666c2c66affSColin Finck  *  vt        [I] Type to store in the safe array
667c2c66affSColin Finck  *  lLbound   [I] Lower bound of the array
668c2c66affSColin Finck  *  cElements [I] Number of elements in the array
669c2c66affSColin Finck  *
670c2c66affSColin Finck  * RETURNS
671c2c66affSColin Finck  *  Success: A pointer to a new array object.
672c2c66affSColin Finck  *  Failure: NULL, if any parameter is invalid or memory allocation fails.
673c2c66affSColin Finck  *
674c2c66affSColin Finck  * NOTES
675c2c66affSColin Finck  * See SafeArray.
676c2c66affSColin Finck  */
SafeArrayCreateVector(VARTYPE vt,LONG lLbound,ULONG cElements)677c2c66affSColin Finck SAFEARRAY* WINAPI SafeArrayCreateVector(VARTYPE vt, LONG lLbound, ULONG cElements)
678c2c66affSColin Finck {
679*ae24453eSAmine Khaldi   TRACE("(%s,%d,%u)\n", debugstr_vt(vt), lLbound, cElements);
680c2c66affSColin Finck 
681c2c66affSColin Finck   if (vt == VT_RECORD)
682c2c66affSColin Finck     return NULL;
683c2c66affSColin Finck 
684c2c66affSColin Finck   return SAFEARRAY_CreateVector(vt, lLbound, cElements, SAFEARRAY_GetVTSize(vt));
685c2c66affSColin Finck }
686c2c66affSColin Finck 
687c2c66affSColin Finck /************************************************************************
688c2c66affSColin Finck  *		SafeArrayCreateVectorEx (OLEAUT32.411)
689c2c66affSColin Finck  *
690c2c66affSColin Finck  * Create a one dimensional, contiguous SafeArray.
691c2c66affSColin Finck  *
692c2c66affSColin Finck  * PARAMS
693c2c66affSColin Finck  *  vt        [I] Type to store in the safe array
694c2c66affSColin Finck  *  lLbound   [I] Lower bound of the array
695c2c66affSColin Finck  *  cElements [I] Number of elements in the array
696c2c66affSColin Finck  *  pvExtra   [I] Extra data
697c2c66affSColin Finck  *
698c2c66affSColin Finck  * RETURNS
699c2c66affSColin Finck  *  Success: A pointer to a new array object.
700c2c66affSColin Finck  *  Failure: NULL, if any parameter is invalid or memory allocation fails.
701c2c66affSColin Finck  *
702c2c66affSColin Finck  * NOTES
703c2c66affSColin Finck  * See SafeArray.
704c2c66affSColin Finck  */
SafeArrayCreateVectorEx(VARTYPE vt,LONG lLbound,ULONG cElements,LPVOID pvExtra)705c2c66affSColin Finck SAFEARRAY* WINAPI SafeArrayCreateVectorEx(VARTYPE vt, LONG lLbound, ULONG cElements, LPVOID pvExtra)
706c2c66affSColin Finck {
707c2c66affSColin Finck   ULONG ulSize;
708c2c66affSColin Finck   IRecordInfo* iRecInfo = pvExtra;
709c2c66affSColin Finck   SAFEARRAY* psa;
710c2c66affSColin Finck 
711*ae24453eSAmine Khaldi   TRACE("(%s,%d,%u,%p)\n", debugstr_vt(vt), lLbound, cElements, pvExtra);
712c2c66affSColin Finck 
713c2c66affSColin Finck   if (vt == VT_RECORD)
714c2c66affSColin Finck   {
715c2c66affSColin Finck     if  (!iRecInfo)
716c2c66affSColin Finck       return NULL;
717c2c66affSColin Finck     IRecordInfo_GetSize(iRecInfo, &ulSize);
718c2c66affSColin Finck   }
719c2c66affSColin Finck   else
720c2c66affSColin Finck     ulSize = SAFEARRAY_GetVTSize(vt);
721c2c66affSColin Finck 
722c2c66affSColin Finck   psa = SAFEARRAY_CreateVector(vt, lLbound, cElements, ulSize);
723c2c66affSColin Finck 
724c2c66affSColin Finck   if (pvExtra)
725c2c66affSColin Finck   {
726c2c66affSColin Finck     switch(vt)
727c2c66affSColin Finck     {
728c2c66affSColin Finck       case VT_RECORD:
729c2c66affSColin Finck         SafeArraySetRecordInfo(psa, iRecInfo);
730c2c66affSColin Finck         break;
731c2c66affSColin Finck       case VT_UNKNOWN:
732c2c66affSColin Finck       case VT_DISPATCH:
733c2c66affSColin Finck         SafeArraySetIID(psa, pvExtra);
734c2c66affSColin Finck         break;
735c2c66affSColin Finck     }
736c2c66affSColin Finck   }
737c2c66affSColin Finck   return psa;
738c2c66affSColin Finck }
739c2c66affSColin Finck 
740c2c66affSColin Finck /*************************************************************************
741c2c66affSColin Finck  *		SafeArrayDestroyDescriptor (OLEAUT32.38)
742c2c66affSColin Finck  *
743c2c66affSColin Finck  * Destroy a SafeArray.
744c2c66affSColin Finck  *
745c2c66affSColin Finck  * PARAMS
746c2c66affSColin Finck  *  psa [I] SafeArray to destroy.
747c2c66affSColin Finck  *
748c2c66affSColin Finck  * RETURNS
749c2c66affSColin Finck  *  Success: S_OK. The resources used by the array are freed.
750c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
751c2c66affSColin Finck  *
752c2c66affSColin Finck  * NOTES
753c2c66affSColin Finck  * See SafeArray.
754c2c66affSColin Finck  */
SafeArrayDestroyDescriptor(SAFEARRAY * psa)755c2c66affSColin Finck HRESULT WINAPI SafeArrayDestroyDescriptor(SAFEARRAY *psa)
756c2c66affSColin Finck {
757c2c66affSColin Finck   TRACE("(%p)\n", psa);
758c2c66affSColin Finck 
759c2c66affSColin Finck   if (psa)
760c2c66affSColin Finck   {
761c2c66affSColin Finck     LPVOID lpv = (char*)psa - SAFEARRAY_HIDDEN_SIZE;
762c2c66affSColin Finck 
763c2c66affSColin Finck     if (psa->cLocks)
764c2c66affSColin Finck       return DISP_E_ARRAYISLOCKED; /* Can't destroy a locked array */
765c2c66affSColin Finck 
766c2c66affSColin Finck     if (psa->fFeatures & FADF_RECORD)
767c2c66affSColin Finck       SafeArraySetRecordInfo(psa, NULL);
768c2c66affSColin Finck 
769c2c66affSColin Finck     if (psa->fFeatures & FADF_CREATEVECTOR &&
770c2c66affSColin Finck         !(psa->fFeatures & FADF_DATADELETED))
771c2c66affSColin Finck         SAFEARRAY_DestroyData(psa, 0); /* Data not previously deleted */
772c2c66affSColin Finck 
773c2c66affSColin Finck     SAFEARRAY_Free(lpv);
774c2c66affSColin Finck   }
775c2c66affSColin Finck   return S_OK;
776c2c66affSColin Finck }
777c2c66affSColin Finck 
778c2c66affSColin Finck /*************************************************************************
779c2c66affSColin Finck  *		SafeArrayLock (OLEAUT32.21)
780c2c66affSColin Finck  *
781c2c66affSColin Finck  * Increment the lock counter of a SafeArray.
782c2c66affSColin Finck  *
783c2c66affSColin Finck  * PARAMS
784c2c66affSColin Finck  *  psa [O] SafeArray to lock
785c2c66affSColin Finck  *
786c2c66affSColin Finck  * RETURNS
787c2c66affSColin Finck  *  Success: S_OK. The array lock is incremented.
788c2c66affSColin Finck  *  Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if too many locks
789c2c66affSColin Finck  *           are held on the array at once.
790c2c66affSColin Finck  *
791c2c66affSColin Finck  * NOTES
792c2c66affSColin Finck  *  In Win32 these locks are not thread safe.
793c2c66affSColin Finck  *  See SafeArray.
794c2c66affSColin Finck  */
SafeArrayLock(SAFEARRAY * psa)795c2c66affSColin Finck HRESULT WINAPI SafeArrayLock(SAFEARRAY *psa)
796c2c66affSColin Finck {
797c2c66affSColin Finck   ULONG ulLocks;
798c2c66affSColin Finck 
799c2c66affSColin Finck   TRACE("(%p)\n", psa);
800c2c66affSColin Finck 
801c2c66affSColin Finck   if (!psa)
802c2c66affSColin Finck     return E_INVALIDARG;
803c2c66affSColin Finck 
804c2c66affSColin Finck   ulLocks = InterlockedIncrement( (LONG*) &psa->cLocks);
805c2c66affSColin Finck 
806c2c66affSColin Finck   if (ulLocks > 0xffff) /* Maximum of 16384 locks at a time */
807c2c66affSColin Finck   {
808c2c66affSColin Finck     WARN("Out of locks!\n");
809c2c66affSColin Finck     InterlockedDecrement( (LONG*) &psa->cLocks);
810c2c66affSColin Finck     return E_UNEXPECTED;
811c2c66affSColin Finck   }
812c2c66affSColin Finck   return S_OK;
813c2c66affSColin Finck }
814c2c66affSColin Finck 
815c2c66affSColin Finck /*************************************************************************
816c2c66affSColin Finck  *		SafeArrayUnlock (OLEAUT32.22)
817c2c66affSColin Finck  *
818c2c66affSColin Finck  * Decrement the lock counter of a SafeArray.
819c2c66affSColin Finck  *
820c2c66affSColin Finck  * PARAMS
821c2c66affSColin Finck  *  psa [O] SafeArray to unlock
822c2c66affSColin Finck  *
823c2c66affSColin Finck  * RETURNS
824c2c66affSColin Finck  *  Success: S_OK. The array lock is decremented.
825c2c66affSColin Finck  *  Failure: E_INVALIDARG if psa is NULL, or E_UNEXPECTED if no locks are
826c2c66affSColin Finck  *           held on the array.
827c2c66affSColin Finck  *
828c2c66affSColin Finck  * NOTES
829c2c66affSColin Finck  * See SafeArray.
830c2c66affSColin Finck  */
SafeArrayUnlock(SAFEARRAY * psa)831c2c66affSColin Finck HRESULT WINAPI SafeArrayUnlock(SAFEARRAY *psa)
832c2c66affSColin Finck {
833c2c66affSColin Finck   TRACE("(%p)\n", psa);
834c2c66affSColin Finck 
835c2c66affSColin Finck   if (!psa)
836c2c66affSColin Finck     return E_INVALIDARG;
837c2c66affSColin Finck 
838c2c66affSColin Finck   if (InterlockedDecrement( (LONG*) &psa->cLocks) < 0)
839c2c66affSColin Finck   {
840c2c66affSColin Finck     WARN("Unlocked but no lock held!\n");
841c2c66affSColin Finck     InterlockedIncrement( (LONG*) &psa->cLocks);
842c2c66affSColin Finck     return E_UNEXPECTED;
843c2c66affSColin Finck   }
844c2c66affSColin Finck   return S_OK;
845c2c66affSColin Finck }
846c2c66affSColin Finck 
847c2c66affSColin Finck /*************************************************************************
848c2c66affSColin Finck  *		SafeArrayPutElement (OLEAUT32.26)
849c2c66affSColin Finck  *
850c2c66affSColin Finck  * Put an item into a SafeArray.
851c2c66affSColin Finck  *
852c2c66affSColin Finck  * PARAMS
853c2c66affSColin Finck  *  psa       [I] SafeArray to insert into
854c2c66affSColin Finck  *  rgIndices [I] Indices to insert at
855c2c66affSColin Finck  *  pvData    [I] Data to insert
856c2c66affSColin Finck  *
857c2c66affSColin Finck  * RETURNS
858c2c66affSColin Finck  *  Success: S_OK. The item is inserted
859c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
860c2c66affSColin Finck  *
861c2c66affSColin Finck  * NOTES
862c2c66affSColin Finck  * See SafeArray.
863c2c66affSColin Finck  */
SafeArrayPutElement(SAFEARRAY * psa,LONG * rgIndices,void * pvData)864c2c66affSColin Finck HRESULT WINAPI SafeArrayPutElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
865c2c66affSColin Finck {
866c2c66affSColin Finck   HRESULT hRet;
867c2c66affSColin Finck 
868c2c66affSColin Finck   TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
869c2c66affSColin Finck 
870c2c66affSColin Finck   if (!psa || !rgIndices)
871c2c66affSColin Finck     return E_INVALIDARG;
872c2c66affSColin Finck 
873c2c66affSColin Finck   hRet = SafeArrayLock(psa);
874c2c66affSColin Finck 
875c2c66affSColin Finck   if (SUCCEEDED(hRet))
876c2c66affSColin Finck   {
877c2c66affSColin Finck     PVOID lpvDest;
878c2c66affSColin Finck 
879c2c66affSColin Finck     hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvDest);
880c2c66affSColin Finck 
881c2c66affSColin Finck     if (SUCCEEDED(hRet))
882c2c66affSColin Finck     {
883c2c66affSColin Finck       if (psa->fFeatures & FADF_VARIANT)
884c2c66affSColin Finck       {
885c2c66affSColin Finck         VARIANT* lpVariant = pvData;
886c2c66affSColin Finck         VARIANT* lpDest = lpvDest;
887c2c66affSColin Finck 
888c2c66affSColin Finck         hRet = VariantCopy(lpDest, lpVariant);
889c2c66affSColin Finck         if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
890c2c66affSColin Finck       }
891c2c66affSColin Finck       else if (psa->fFeatures & FADF_BSTR)
892c2c66affSColin Finck       {
893c2c66affSColin Finck         BSTR  lpBstr = (BSTR)pvData;
894c2c66affSColin Finck         BSTR* lpDest = lpvDest;
895c2c66affSColin Finck 
896c2c66affSColin Finck         SysFreeString(*lpDest);
897c2c66affSColin Finck 
898c2c66affSColin Finck         *lpDest = SysAllocStringByteLen((char*)lpBstr, SysStringByteLen(lpBstr));
899c2c66affSColin Finck         if (!*lpDest)
900c2c66affSColin Finck           hRet = E_OUTOFMEMORY;
901c2c66affSColin Finck       }
902c2c66affSColin Finck       else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
903c2c66affSColin Finck       {
904c2c66affSColin Finck         IUnknown  *lpUnknown = pvData;
905c2c66affSColin Finck         IUnknown **lpDest = lpvDest;
906c2c66affSColin Finck 
907c2c66affSColin Finck         if (lpUnknown)
908c2c66affSColin Finck           IUnknown_AddRef(lpUnknown);
909c2c66affSColin Finck         if (*lpDest)
910c2c66affSColin Finck           IUnknown_Release(*lpDest);
911c2c66affSColin Finck         *lpDest = lpUnknown;
912c2c66affSColin Finck       }
913c2c66affSColin Finck       else if (psa->fFeatures & FADF_RECORD)
914c2c66affSColin Finck       {
915c2c66affSColin Finck         IRecordInfo *record;
916c2c66affSColin Finck 
917c2c66affSColin Finck         SafeArrayGetRecordInfo(psa, &record);
918c2c66affSColin Finck         hRet = IRecordInfo_RecordCopy(record, pvData, lpvDest);
919c2c66affSColin Finck         IRecordInfo_Release(record);
920c2c66affSColin Finck       } else
921c2c66affSColin Finck         /* Copy the data over */
922c2c66affSColin Finck         memcpy(lpvDest, pvData, psa->cbElements);
923c2c66affSColin Finck     }
924c2c66affSColin Finck     SafeArrayUnlock(psa);
925c2c66affSColin Finck   }
926c2c66affSColin Finck   return hRet;
927c2c66affSColin Finck }
928c2c66affSColin Finck 
929c2c66affSColin Finck 
930c2c66affSColin Finck /*************************************************************************
931c2c66affSColin Finck  *		SafeArrayGetElement (OLEAUT32.25)
932c2c66affSColin Finck  *
933c2c66affSColin Finck  * Get an item from a SafeArray.
934c2c66affSColin Finck  *
935c2c66affSColin Finck  * PARAMS
936c2c66affSColin Finck  *  psa       [I] SafeArray to get from
937c2c66affSColin Finck  *  rgIndices [I] Indices to get from
938c2c66affSColin Finck  *  pvData    [O] Destination for data
939c2c66affSColin Finck  *
940c2c66affSColin Finck  * RETURNS
941c2c66affSColin Finck  *  Success: S_OK. The item data is returned in pvData.
942c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
943c2c66affSColin Finck  *
944c2c66affSColin Finck  * NOTES
945c2c66affSColin Finck  * See SafeArray.
946c2c66affSColin Finck  */
SafeArrayGetElement(SAFEARRAY * psa,LONG * rgIndices,void * pvData)947c2c66affSColin Finck HRESULT WINAPI SafeArrayGetElement(SAFEARRAY *psa, LONG *rgIndices, void *pvData)
948c2c66affSColin Finck {
949c2c66affSColin Finck   HRESULT hRet;
950c2c66affSColin Finck 
951c2c66affSColin Finck   TRACE("(%p,%p,%p)\n", psa, rgIndices, pvData);
952c2c66affSColin Finck 
953c2c66affSColin Finck   if (!psa || !rgIndices || !pvData)
954c2c66affSColin Finck     return E_INVALIDARG;
955c2c66affSColin Finck 
956c2c66affSColin Finck   hRet = SafeArrayLock(psa);
957c2c66affSColin Finck 
958c2c66affSColin Finck   if (SUCCEEDED(hRet))
959c2c66affSColin Finck   {
960c2c66affSColin Finck     PVOID lpvSrc;
961c2c66affSColin Finck 
962c2c66affSColin Finck     hRet = SafeArrayPtrOfIndex(psa, rgIndices, &lpvSrc);
963c2c66affSColin Finck 
964c2c66affSColin Finck     if (SUCCEEDED(hRet))
965c2c66affSColin Finck     {
966c2c66affSColin Finck       if (psa->fFeatures & FADF_VARIANT)
967c2c66affSColin Finck       {
968c2c66affSColin Finck         VARIANT* lpVariant = lpvSrc;
969c2c66affSColin Finck         VARIANT* lpDest = pvData;
970c2c66affSColin Finck 
971c2c66affSColin Finck         /* The original content of pvData is ignored. */
972c2c66affSColin Finck         V_VT(lpDest) = VT_EMPTY;
973c2c66affSColin Finck         hRet = VariantCopy(lpDest, lpVariant);
974c2c66affSColin Finck 	if (FAILED(hRet)) FIXME("VariantCopy failed with 0x%x\n", hRet);
975c2c66affSColin Finck       }
976c2c66affSColin Finck       else if (psa->fFeatures & FADF_BSTR)
977c2c66affSColin Finck       {
978c2c66affSColin Finck         BSTR* lpBstr = lpvSrc;
979c2c66affSColin Finck         BSTR* lpDest = pvData;
980c2c66affSColin Finck 
981c2c66affSColin Finck         if (*lpBstr)
982c2c66affSColin Finck         {
983c2c66affSColin Finck           *lpDest = SysAllocStringByteLen((char*)*lpBstr, SysStringByteLen(*lpBstr));
984c2c66affSColin Finck           if (!*lpBstr)
985c2c66affSColin Finck             hRet = E_OUTOFMEMORY;
986c2c66affSColin Finck         }
987c2c66affSColin Finck         else
988c2c66affSColin Finck           *lpDest = NULL;
989c2c66affSColin Finck       }
990c2c66affSColin Finck       else if (psa->fFeatures & (FADF_UNKNOWN|FADF_DISPATCH))
991c2c66affSColin Finck       {
992c2c66affSColin Finck         IUnknown **src_unk = lpvSrc;
993c2c66affSColin Finck         IUnknown **dest_unk = pvData;
994c2c66affSColin Finck 
995c2c66affSColin Finck         if (*src_unk)
996c2c66affSColin Finck           IUnknown_AddRef(*src_unk);
997c2c66affSColin Finck         *dest_unk = *src_unk;
998c2c66affSColin Finck       }
999c2c66affSColin Finck       else if (psa->fFeatures & FADF_RECORD)
1000c2c66affSColin Finck       {
1001c2c66affSColin Finck         IRecordInfo *record;
1002c2c66affSColin Finck 
1003c2c66affSColin Finck         SafeArrayGetRecordInfo(psa, &record);
1004c2c66affSColin Finck         hRet = IRecordInfo_RecordCopy(record, lpvSrc, pvData);
1005c2c66affSColin Finck         IRecordInfo_Release(record);
1006c2c66affSColin Finck       }
1007c2c66affSColin Finck       else
1008c2c66affSColin Finck         /* Copy the data over */
1009c2c66affSColin Finck         memcpy(pvData, lpvSrc, psa->cbElements);
1010c2c66affSColin Finck     }
1011c2c66affSColin Finck     SafeArrayUnlock(psa);
1012c2c66affSColin Finck   }
1013c2c66affSColin Finck   return hRet;
1014c2c66affSColin Finck }
1015c2c66affSColin Finck 
1016c2c66affSColin Finck /*************************************************************************
1017c2c66affSColin Finck  *		SafeArrayGetUBound (OLEAUT32.19)
1018c2c66affSColin Finck  *
1019c2c66affSColin Finck  * Get the upper bound for a given SafeArray dimension
1020c2c66affSColin Finck  *
1021c2c66affSColin Finck  * PARAMS
1022c2c66affSColin Finck  *  psa      [I] Array to get dimension upper bound from
1023c2c66affSColin Finck  *  nDim     [I] The dimension number to get the upper bound of
1024c2c66affSColin Finck  *  plUbound [O] Destination for the upper bound
1025c2c66affSColin Finck  *
1026c2c66affSColin Finck  * RETURNS
1027c2c66affSColin Finck  *  Success: S_OK. plUbound contains the dimensions upper bound.
1028c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1029c2c66affSColin Finck  *
1030c2c66affSColin Finck  * NOTES
1031c2c66affSColin Finck  * See SafeArray.
1032c2c66affSColin Finck  */
SafeArrayGetUBound(SAFEARRAY * psa,UINT nDim,LONG * plUbound)1033c2c66affSColin Finck HRESULT WINAPI SafeArrayGetUBound(SAFEARRAY *psa, UINT nDim, LONG *plUbound)
1034c2c66affSColin Finck {
1035c2c66affSColin Finck   TRACE("(%p,%d,%p)\n", psa, nDim, plUbound);
1036c2c66affSColin Finck 
1037c2c66affSColin Finck   if (!psa || !plUbound)
1038c2c66affSColin Finck     return E_INVALIDARG;
1039c2c66affSColin Finck 
1040c2c66affSColin Finck   if(!nDim || nDim > psa->cDims)
1041c2c66affSColin Finck     return DISP_E_BADINDEX;
1042c2c66affSColin Finck 
1043c2c66affSColin Finck   *plUbound = psa->rgsabound[psa->cDims - nDim].lLbound +
1044c2c66affSColin Finck               psa->rgsabound[psa->cDims - nDim].cElements - 1;
1045c2c66affSColin Finck 
1046c2c66affSColin Finck   return S_OK;
1047c2c66affSColin Finck }
1048c2c66affSColin Finck 
1049c2c66affSColin Finck /*************************************************************************
1050c2c66affSColin Finck  *		SafeArrayGetLBound (OLEAUT32.20)
1051c2c66affSColin Finck  *
1052c2c66affSColin Finck  * Get the lower bound for a given SafeArray dimension
1053c2c66affSColin Finck  *
1054c2c66affSColin Finck  * PARAMS
1055c2c66affSColin Finck  *  psa      [I] Array to get dimension lower bound from
1056c2c66affSColin Finck  *  nDim     [I] The dimension number to get the lower bound of
1057c2c66affSColin Finck  *  plLbound [O] Destination for the lower bound
1058c2c66affSColin Finck  *
1059c2c66affSColin Finck  * RETURNS
1060c2c66affSColin Finck  *  Success: S_OK. plUbound contains the dimensions lower bound.
1061c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1062c2c66affSColin Finck  *
1063c2c66affSColin Finck  * NOTES
1064c2c66affSColin Finck  * See SafeArray.
1065c2c66affSColin Finck  */
SafeArrayGetLBound(SAFEARRAY * psa,UINT nDim,LONG * plLbound)1066c2c66affSColin Finck HRESULT WINAPI SafeArrayGetLBound(SAFEARRAY *psa, UINT nDim, LONG *plLbound)
1067c2c66affSColin Finck {
1068c2c66affSColin Finck   TRACE("(%p,%d,%p)\n", psa, nDim, plLbound);
1069c2c66affSColin Finck 
1070c2c66affSColin Finck   if (!psa || !plLbound)
1071c2c66affSColin Finck     return E_INVALIDARG;
1072c2c66affSColin Finck 
1073c2c66affSColin Finck   if(!nDim || nDim > psa->cDims)
1074c2c66affSColin Finck     return DISP_E_BADINDEX;
1075c2c66affSColin Finck 
1076c2c66affSColin Finck   *plLbound = psa->rgsabound[psa->cDims - nDim].lLbound;
1077c2c66affSColin Finck   return S_OK;
1078c2c66affSColin Finck }
1079c2c66affSColin Finck 
1080c2c66affSColin Finck /*************************************************************************
1081c2c66affSColin Finck  *		SafeArrayGetDim (OLEAUT32.17)
1082c2c66affSColin Finck  *
1083c2c66affSColin Finck  * Get the number of dimensions in a SafeArray.
1084c2c66affSColin Finck  *
1085c2c66affSColin Finck  * PARAMS
1086c2c66affSColin Finck  *  psa [I] Array to get the dimensions of
1087c2c66affSColin Finck  *
1088c2c66affSColin Finck  * RETURNS
1089c2c66affSColin Finck  *  The number of array dimensions in psa, or 0 if psa is NULL.
1090c2c66affSColin Finck  *
1091c2c66affSColin Finck  * NOTES
1092c2c66affSColin Finck  * See SafeArray.
1093c2c66affSColin Finck  */
SafeArrayGetDim(SAFEARRAY * psa)1094c2c66affSColin Finck UINT WINAPI SafeArrayGetDim(SAFEARRAY *psa)
1095c2c66affSColin Finck {
1096c2c66affSColin Finck   TRACE("(%p) returning %d\n", psa, psa ? psa->cDims : 0u);
1097c2c66affSColin Finck   return psa ? psa->cDims : 0;
1098c2c66affSColin Finck }
1099c2c66affSColin Finck 
1100c2c66affSColin Finck /*************************************************************************
1101c2c66affSColin Finck  *		SafeArrayGetElemsize (OLEAUT32.18)
1102c2c66affSColin Finck  *
1103c2c66affSColin Finck  * Get the size of an element in a SafeArray.
1104c2c66affSColin Finck  *
1105c2c66affSColin Finck  * PARAMS
1106c2c66affSColin Finck  *  psa [I] Array to get the element size from
1107c2c66affSColin Finck  *
1108c2c66affSColin Finck  * RETURNS
1109c2c66affSColin Finck  *  The size of a single element in psa, or 0 if psa is NULL.
1110c2c66affSColin Finck  *
1111c2c66affSColin Finck  * NOTES
1112c2c66affSColin Finck  * See SafeArray.
1113c2c66affSColin Finck  */
SafeArrayGetElemsize(SAFEARRAY * psa)1114c2c66affSColin Finck UINT WINAPI SafeArrayGetElemsize(SAFEARRAY *psa)
1115c2c66affSColin Finck {
1116c2c66affSColin Finck   TRACE("(%p) returning %d\n", psa, psa ? psa->cbElements : 0u);
1117c2c66affSColin Finck   return psa ? psa->cbElements : 0;
1118c2c66affSColin Finck }
1119c2c66affSColin Finck 
1120c2c66affSColin Finck /*************************************************************************
1121c2c66affSColin Finck  *		SafeArrayAccessData (OLEAUT32.23)
1122c2c66affSColin Finck  *
1123c2c66affSColin Finck  * Lock a SafeArray and return a pointer to its data.
1124c2c66affSColin Finck  *
1125c2c66affSColin Finck  * PARAMS
1126c2c66affSColin Finck  *  psa     [I] Array to get the data pointer from
1127c2c66affSColin Finck  *  ppvData [O] Destination for the arrays data pointer
1128c2c66affSColin Finck  *
1129c2c66affSColin Finck  * RETURNS
1130c2c66affSColin Finck  *  Success: S_OK. ppvData contains the arrays data pointer, and the array
1131c2c66affSColin Finck  *           is locked.
1132c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1133c2c66affSColin Finck  *
1134c2c66affSColin Finck  * NOTES
1135c2c66affSColin Finck  * See SafeArray.
1136c2c66affSColin Finck  */
SafeArrayAccessData(SAFEARRAY * psa,void ** ppvData)1137c2c66affSColin Finck HRESULT WINAPI SafeArrayAccessData(SAFEARRAY *psa, void **ppvData)
1138c2c66affSColin Finck {
1139c2c66affSColin Finck   HRESULT hr;
1140c2c66affSColin Finck 
1141c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, ppvData);
1142c2c66affSColin Finck 
1143c2c66affSColin Finck   if(!psa || !ppvData)
1144c2c66affSColin Finck     return E_INVALIDARG;
1145c2c66affSColin Finck 
1146c2c66affSColin Finck   hr = SafeArrayLock(psa);
1147c2c66affSColin Finck   *ppvData = SUCCEEDED(hr) ? psa->pvData : NULL;
1148c2c66affSColin Finck 
1149c2c66affSColin Finck   return hr;
1150c2c66affSColin Finck }
1151c2c66affSColin Finck 
1152c2c66affSColin Finck 
1153c2c66affSColin Finck /*************************************************************************
1154c2c66affSColin Finck  *		SafeArrayUnaccessData (OLEAUT32.24)
1155c2c66affSColin Finck  *
1156c2c66affSColin Finck  * Unlock a SafeArray after accessing its data.
1157c2c66affSColin Finck  *
1158c2c66affSColin Finck  * PARAMS
1159c2c66affSColin Finck  *  psa     [I] Array to unlock
1160c2c66affSColin Finck  *
1161c2c66affSColin Finck  * RETURNS
1162c2c66affSColin Finck  *  Success: S_OK. The array is unlocked.
1163c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1164c2c66affSColin Finck  *
1165c2c66affSColin Finck  * NOTES
1166c2c66affSColin Finck  * See SafeArray.
1167c2c66affSColin Finck  */
SafeArrayUnaccessData(SAFEARRAY * psa)1168c2c66affSColin Finck HRESULT WINAPI SafeArrayUnaccessData(SAFEARRAY *psa)
1169c2c66affSColin Finck {
1170c2c66affSColin Finck   TRACE("(%p)\n", psa);
1171c2c66affSColin Finck   return SafeArrayUnlock(psa);
1172c2c66affSColin Finck }
1173c2c66affSColin Finck 
1174c2c66affSColin Finck /************************************************************************
1175c2c66affSColin Finck  *		SafeArrayPtrOfIndex (OLEAUT32.148)
1176c2c66affSColin Finck  *
1177c2c66affSColin Finck  * Get the address of an item in a SafeArray.
1178c2c66affSColin Finck  *
1179c2c66affSColin Finck  * PARAMS
1180c2c66affSColin Finck  *  psa       [I] Array to get the items address from
1181c2c66affSColin Finck  *  rgIndices [I] Index of the item in the array
1182c2c66affSColin Finck  *  ppvData   [O] Destination for item address
1183c2c66affSColin Finck  *
1184c2c66affSColin Finck  * RETURNS
1185c2c66affSColin Finck  *  Success: S_OK. ppvData contains a pointer to the item.
1186c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1187c2c66affSColin Finck  *
1188c2c66affSColin Finck  * NOTES
1189c2c66affSColin Finck  *  This function does not lock the array.
1190c2c66affSColin Finck  *
1191c2c66affSColin Finck  * NOTES
1192c2c66affSColin Finck  * See SafeArray.
1193c2c66affSColin Finck  */
SafeArrayPtrOfIndex(SAFEARRAY * psa,LONG * rgIndices,void ** ppvData)1194c2c66affSColin Finck HRESULT WINAPI SafeArrayPtrOfIndex(SAFEARRAY *psa, LONG *rgIndices, void **ppvData)
1195c2c66affSColin Finck {
1196c2c66affSColin Finck   USHORT dim;
1197c2c66affSColin Finck   ULONG cell = 0, dimensionSize = 1;
1198c2c66affSColin Finck   SAFEARRAYBOUND* psab;
1199c2c66affSColin Finck   LONG c1;
1200c2c66affSColin Finck 
1201c2c66affSColin Finck   TRACE("(%p,%p,%p)\n", psa, rgIndices, ppvData);
1202c2c66affSColin Finck 
1203c2c66affSColin Finck   /* The general formula for locating the cell number of an entry in an n
1204c2c66affSColin Finck    * dimensional array (where cn = coordinate in dimension dn) is:
1205c2c66affSColin Finck    *
1206c2c66affSColin Finck    * c1 + c2 * sizeof(d1) + c3 * sizeof(d2) ... + cn * sizeof(c(n-1))
1207c2c66affSColin Finck    *
1208c2c66affSColin Finck    * We calculate the size of the last dimension at each step through the
1209c2c66affSColin Finck    * dimensions to avoid recursing to calculate the last dimensions size.
1210c2c66affSColin Finck    */
1211c2c66affSColin Finck   if (!psa || !rgIndices || !ppvData)
1212c2c66affSColin Finck     return E_INVALIDARG;
1213c2c66affSColin Finck 
1214c2c66affSColin Finck   psab = psa->rgsabound + psa->cDims - 1;
1215c2c66affSColin Finck   c1 = *rgIndices++;
1216c2c66affSColin Finck 
1217c2c66affSColin Finck   if (c1 < psab->lLbound || c1 >= psab->lLbound + (LONG)psab->cElements)
1218c2c66affSColin Finck     return DISP_E_BADINDEX; /* Initial index out of bounds */
1219c2c66affSColin Finck 
1220c2c66affSColin Finck   for (dim = 1; dim < psa->cDims; dim++)
1221c2c66affSColin Finck   {
1222c2c66affSColin Finck     dimensionSize *= psab->cElements;
1223c2c66affSColin Finck 
1224c2c66affSColin Finck     psab--;
1225c2c66affSColin Finck 
1226c2c66affSColin Finck     if (!psab->cElements ||
1227c2c66affSColin Finck         *rgIndices < psab->lLbound ||
1228c2c66affSColin Finck         *rgIndices >= psab->lLbound + (LONG)psab->cElements)
1229c2c66affSColin Finck     return DISP_E_BADINDEX; /* Index out of bounds */
1230c2c66affSColin Finck 
1231c2c66affSColin Finck     cell += (*rgIndices - psab->lLbound) * dimensionSize;
1232c2c66affSColin Finck     rgIndices++;
1233c2c66affSColin Finck   }
1234c2c66affSColin Finck 
1235c2c66affSColin Finck   cell += (c1 - psa->rgsabound[psa->cDims - 1].lLbound);
1236c2c66affSColin Finck 
1237c2c66affSColin Finck   *ppvData = (char*)psa->pvData + cell * psa->cbElements;
1238c2c66affSColin Finck   return S_OK;
1239c2c66affSColin Finck }
1240c2c66affSColin Finck 
1241c2c66affSColin Finck /************************************************************************
1242c2c66affSColin Finck  *		SafeArrayDestroyData (OLEAUT32.39)
1243c2c66affSColin Finck  *
1244c2c66affSColin Finck  * Destroy the data associated with a SafeArray.
1245c2c66affSColin Finck  *
1246c2c66affSColin Finck  * PARAMS
1247c2c66affSColin Finck  *  psa [I] Array to delete the data from
1248c2c66affSColin Finck  *
1249c2c66affSColin Finck  * RETURNS
1250c2c66affSColin Finck  *  Success: S_OK. All items and the item data are freed.
1251c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1252c2c66affSColin Finck  *
1253c2c66affSColin Finck  * NOTES
1254c2c66affSColin Finck  * See SafeArray.
1255c2c66affSColin Finck  */
SafeArrayDestroyData(SAFEARRAY * psa)1256c2c66affSColin Finck HRESULT WINAPI SafeArrayDestroyData(SAFEARRAY *psa)
1257c2c66affSColin Finck {
1258c2c66affSColin Finck   HRESULT hr;
1259c2c66affSColin Finck 
1260c2c66affSColin Finck   TRACE("(%p)\n", psa);
1261c2c66affSColin Finck 
1262c2c66affSColin Finck   if (!psa)
1263c2c66affSColin Finck     return E_INVALIDARG;
1264c2c66affSColin Finck 
1265c2c66affSColin Finck   if (psa->cLocks)
1266c2c66affSColin Finck     return DISP_E_ARRAYISLOCKED; /* Can't delete a locked array */
1267c2c66affSColin Finck 
1268c2c66affSColin Finck   /* Delete the actual item data */
1269c2c66affSColin Finck   hr = SAFEARRAY_DestroyData(psa, 0);
1270c2c66affSColin Finck   if (FAILED(hr))
1271c2c66affSColin Finck     return hr;
1272c2c66affSColin Finck 
1273c2c66affSColin Finck   if (psa->pvData)
1274c2c66affSColin Finck   {
1275c2c66affSColin Finck     if (psa->fFeatures & FADF_STATIC)
1276c2c66affSColin Finck     {
1277c2c66affSColin Finck       ZeroMemory(psa->pvData, SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1278c2c66affSColin Finck       return S_OK;
1279c2c66affSColin Finck     }
1280c2c66affSColin Finck     /* If this is not a vector, free the data memory block */
1281c2c66affSColin Finck     if (!(psa->fFeatures & FADF_CREATEVECTOR))
1282c2c66affSColin Finck     {
1283c2c66affSColin Finck       SAFEARRAY_Free(psa->pvData);
1284c2c66affSColin Finck       psa->pvData = NULL;
1285c2c66affSColin Finck     }
1286c2c66affSColin Finck     else
1287c2c66affSColin Finck       psa->fFeatures |= FADF_DATADELETED; /* Mark the data deleted */
1288c2c66affSColin Finck 
1289c2c66affSColin Finck   }
1290c2c66affSColin Finck   return S_OK;
1291c2c66affSColin Finck }
1292c2c66affSColin Finck 
1293c2c66affSColin Finck /************************************************************************
1294c2c66affSColin Finck  *		SafeArrayCopyData (OLEAUT32.412)
1295c2c66affSColin Finck  *
1296c2c66affSColin Finck  * Copy all data from one SafeArray to another.
1297c2c66affSColin Finck  *
1298c2c66affSColin Finck  * PARAMS
1299c2c66affSColin Finck  *  psaSource [I] Source for copy
1300c2c66affSColin Finck  *  psaTarget [O] Destination for copy
1301c2c66affSColin Finck  *
1302c2c66affSColin Finck  * RETURNS
1303c2c66affSColin Finck  *  Success: S_OK. psaTarget contains a copy of psaSource.
1304c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1305c2c66affSColin Finck  *
1306c2c66affSColin Finck  * NOTES
1307c2c66affSColin Finck  *  The two arrays must have the same number of dimensions and elements.
1308c2c66affSColin Finck  *
1309c2c66affSColin Finck  * NOTES
1310c2c66affSColin Finck  * See SafeArray.
1311c2c66affSColin Finck  */
SafeArrayCopyData(SAFEARRAY * psaSource,SAFEARRAY * psaTarget)1312c2c66affSColin Finck HRESULT WINAPI SafeArrayCopyData(SAFEARRAY *psaSource, SAFEARRAY *psaTarget)
1313c2c66affSColin Finck {
1314c2c66affSColin Finck   int dim;
1315c2c66affSColin Finck 
1316c2c66affSColin Finck   TRACE("(%p,%p)\n", psaSource, psaTarget);
1317c2c66affSColin Finck 
1318c2c66affSColin Finck   if (!psaSource || !psaTarget ||
1319c2c66affSColin Finck       psaSource->cDims != psaTarget->cDims ||
1320c2c66affSColin Finck       psaSource->cbElements != psaTarget->cbElements)
1321c2c66affSColin Finck     return E_INVALIDARG;
1322c2c66affSColin Finck 
1323c2c66affSColin Finck   /* Each dimension must be the same size */
1324c2c66affSColin Finck   for (dim = psaSource->cDims - 1; dim >= 0 ; dim--)
1325c2c66affSColin Finck     if (psaSource->rgsabound[dim].cElements !=
1326c2c66affSColin Finck        psaTarget->rgsabound[dim].cElements)
1327c2c66affSColin Finck       return E_INVALIDARG;
1328c2c66affSColin Finck 
1329c2c66affSColin Finck   return SAFEARRAY_CopyData(psaSource, psaTarget);
1330c2c66affSColin Finck }
1331c2c66affSColin Finck 
1332c2c66affSColin Finck /************************************************************************
1333c2c66affSColin Finck  *		SafeArrayDestroy (OLEAUT32.16)
1334c2c66affSColin Finck  *
1335c2c66affSColin Finck  * Destroy a SafeArray.
1336c2c66affSColin Finck  *
1337c2c66affSColin Finck  * PARAMS
1338c2c66affSColin Finck  *  psa [I] Array to destroy
1339c2c66affSColin Finck  *
1340c2c66affSColin Finck  * RETURNS
1341c2c66affSColin Finck  *  Success: S_OK. All resources used by the array are freed.
1342c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1343c2c66affSColin Finck  *
1344c2c66affSColin Finck  * NOTES
1345c2c66affSColin Finck  * See SafeArray.
1346c2c66affSColin Finck  */
SafeArrayDestroy(SAFEARRAY * psa)1347c2c66affSColin Finck HRESULT WINAPI SafeArrayDestroy(SAFEARRAY *psa)
1348c2c66affSColin Finck {
1349c2c66affSColin Finck   TRACE("(%p)\n", psa);
1350c2c66affSColin Finck 
1351c2c66affSColin Finck   if(!psa)
1352c2c66affSColin Finck     return S_OK;
1353c2c66affSColin Finck 
1354c2c66affSColin Finck   if(psa->cLocks > 0)
1355c2c66affSColin Finck     return DISP_E_ARRAYISLOCKED;
1356c2c66affSColin Finck 
1357c2c66affSColin Finck   /* Native doesn't check to see if the free succeeds */
1358c2c66affSColin Finck   SafeArrayDestroyData(psa);
1359c2c66affSColin Finck   SafeArrayDestroyDescriptor(psa);
1360c2c66affSColin Finck   return S_OK;
1361c2c66affSColin Finck }
1362c2c66affSColin Finck 
1363c2c66affSColin Finck /************************************************************************
1364c2c66affSColin Finck  *		SafeArrayCopy (OLEAUT32.27)
1365c2c66affSColin Finck  *
1366c2c66affSColin Finck  * Make a duplicate of a SafeArray.
1367c2c66affSColin Finck  *
1368c2c66affSColin Finck  * PARAMS
1369c2c66affSColin Finck  *  psa     [I] Source for copy
1370c2c66affSColin Finck  *  ppsaOut [O] Destination for new copy
1371c2c66affSColin Finck  *
1372c2c66affSColin Finck  * RETURNS
1373c2c66affSColin Finck  *  Success: S_OK. ppsaOut contains a copy of the array.
1374c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1375c2c66affSColin Finck  *
1376c2c66affSColin Finck  * NOTES
1377c2c66affSColin Finck  * See SafeArray.
1378c2c66affSColin Finck  */
SafeArrayCopy(SAFEARRAY * psa,SAFEARRAY ** ppsaOut)1379c2c66affSColin Finck HRESULT WINAPI SafeArrayCopy(SAFEARRAY *psa, SAFEARRAY **ppsaOut)
1380c2c66affSColin Finck {
1381c2c66affSColin Finck   HRESULT hRet;
1382c2c66affSColin Finck 
1383c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, ppsaOut);
1384c2c66affSColin Finck 
1385c2c66affSColin Finck   if (!ppsaOut)
1386c2c66affSColin Finck     return E_INVALIDARG;
1387c2c66affSColin Finck 
1388c2c66affSColin Finck   *ppsaOut = NULL;
1389c2c66affSColin Finck 
1390c2c66affSColin Finck   if (!psa)
1391c2c66affSColin Finck     return S_OK; /* Handles copying of NULL arrays */
1392c2c66affSColin Finck 
1393c2c66affSColin Finck   if (!psa->cbElements)
1394c2c66affSColin Finck     return E_INVALIDARG;
1395c2c66affSColin Finck 
1396c2c66affSColin Finck   if (psa->fFeatures & (FADF_RECORD|FADF_HAVEIID|FADF_HAVEVARTYPE))
1397c2c66affSColin Finck   {
1398c2c66affSColin Finck     VARTYPE vt;
1399c2c66affSColin Finck 
1400c2c66affSColin Finck     hRet = SafeArrayGetVartype(psa, &vt);
1401c2c66affSColin Finck     if (SUCCEEDED(hRet))
1402c2c66affSColin Finck       hRet = SafeArrayAllocDescriptorEx(vt, psa->cDims, ppsaOut);
1403c2c66affSColin Finck   }
1404c2c66affSColin Finck   else
1405c2c66affSColin Finck   {
1406c2c66affSColin Finck     hRet = SafeArrayAllocDescriptor(psa->cDims, ppsaOut);
1407c2c66affSColin Finck     if (SUCCEEDED(hRet))
1408c2c66affSColin Finck     {
1409c2c66affSColin Finck       (*ppsaOut)->fFeatures = psa->fFeatures & ~ignored_copy_features;
1410c2c66affSColin Finck       (*ppsaOut)->cbElements = psa->cbElements;
1411c2c66affSColin Finck     }
1412c2c66affSColin Finck   }
1413c2c66affSColin Finck 
1414c2c66affSColin Finck   if (SUCCEEDED(hRet))
1415c2c66affSColin Finck   {
1416c2c66affSColin Finck     /* Copy dimension bounds */
1417c2c66affSColin Finck     memcpy((*ppsaOut)->rgsabound, psa->rgsabound, psa->cDims * sizeof(SAFEARRAYBOUND));
1418c2c66affSColin Finck 
1419c2c66affSColin Finck     (*ppsaOut)->pvData = SAFEARRAY_Malloc(SAFEARRAY_GetCellCount(psa) * psa->cbElements);
1420c2c66affSColin Finck     if (!(*ppsaOut)->pvData)
1421c2c66affSColin Finck     {
1422c2c66affSColin Finck       SafeArrayDestroyDescriptor(*ppsaOut);
1423c2c66affSColin Finck       *ppsaOut = NULL;
1424c2c66affSColin Finck       return E_OUTOFMEMORY;
1425c2c66affSColin Finck     }
1426c2c66affSColin Finck 
1427c2c66affSColin Finck     hRet = SAFEARRAY_CopyData(psa, *ppsaOut);
1428c2c66affSColin Finck     if (FAILED(hRet))
1429c2c66affSColin Finck     {
1430c2c66affSColin Finck       SAFEARRAY_Free((*ppsaOut)->pvData);
1431c2c66affSColin Finck       SafeArrayDestroyDescriptor(*ppsaOut);
1432c2c66affSColin Finck       *ppsaOut = NULL;
1433c2c66affSColin Finck       return hRet;
1434c2c66affSColin Finck     }
1435c2c66affSColin Finck   }
1436c2c66affSColin Finck 
1437c2c66affSColin Finck   return hRet;
1438c2c66affSColin Finck }
1439c2c66affSColin Finck 
1440c2c66affSColin Finck /************************************************************************
1441c2c66affSColin Finck  *		SafeArrayRedim (OLEAUT32.40)
1442c2c66affSColin Finck  *
1443c2c66affSColin Finck  * Changes the characteristics of the last dimension of a SafeArray
1444c2c66affSColin Finck  *
1445c2c66affSColin Finck  * PARAMS
1446c2c66affSColin Finck  *  psa      [I] Array to change
1447c2c66affSColin Finck  *  psabound [I] New bound details for the last dimension
1448c2c66affSColin Finck  *
1449c2c66affSColin Finck  * RETURNS
1450c2c66affSColin Finck  *  Success: S_OK. psa is updated to reflect the new bounds.
1451c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1452c2c66affSColin Finck  *
1453c2c66affSColin Finck  * NOTES
1454c2c66affSColin Finck  * See SafeArray.
1455c2c66affSColin Finck  */
SafeArrayRedim(SAFEARRAY * psa,SAFEARRAYBOUND * psabound)1456c2c66affSColin Finck HRESULT WINAPI SafeArrayRedim(SAFEARRAY *psa, SAFEARRAYBOUND *psabound)
1457c2c66affSColin Finck {
1458c2c66affSColin Finck   SAFEARRAYBOUND *oldBounds;
1459c2c66affSColin Finck   HRESULT hr;
1460c2c66affSColin Finck 
1461c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, psabound);
1462c2c66affSColin Finck 
1463c2c66affSColin Finck   if (!psa || psa->fFeatures & FADF_FIXEDSIZE || !psabound)
1464c2c66affSColin Finck     return E_INVALIDARG;
1465c2c66affSColin Finck 
1466c2c66affSColin Finck   if (psa->cLocks > 0)
1467c2c66affSColin Finck     return DISP_E_ARRAYISLOCKED;
1468c2c66affSColin Finck 
1469c2c66affSColin Finck   hr = SafeArrayLock(psa);
1470c2c66affSColin Finck   if (FAILED(hr))
1471c2c66affSColin Finck     return hr;
1472c2c66affSColin Finck 
1473c2c66affSColin Finck   oldBounds = psa->rgsabound;
1474c2c66affSColin Finck   oldBounds->lLbound = psabound->lLbound;
1475c2c66affSColin Finck 
1476c2c66affSColin Finck   if (psabound->cElements != oldBounds->cElements)
1477c2c66affSColin Finck   {
1478c2c66affSColin Finck     if (psabound->cElements < oldBounds->cElements)
1479c2c66affSColin Finck     {
1480c2c66affSColin Finck       /* Shorten the final dimension. */
1481c2c66affSColin Finck       ULONG ulStartCell = psabound->cElements *
1482c2c66affSColin Finck                           (SAFEARRAY_GetCellCount(psa) / oldBounds->cElements);
1483c2c66affSColin Finck       SAFEARRAY_DestroyData(psa, ulStartCell);
1484c2c66affSColin Finck     }
1485c2c66affSColin Finck     else
1486c2c66affSColin Finck     {
1487c2c66affSColin Finck       /* Lengthen the final dimension */
1488c2c66affSColin Finck       ULONG ulOldSize, ulNewSize;
1489c2c66affSColin Finck       PVOID pvNewData;
1490c2c66affSColin Finck 
1491c2c66affSColin Finck       ulOldSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1492c2c66affSColin Finck       if (ulOldSize)
1493c2c66affSColin Finck         ulNewSize = (ulOldSize / oldBounds->cElements) * psabound->cElements;
1494c2c66affSColin Finck       else {
1495c2c66affSColin Finck 	int oldelems = oldBounds->cElements;
1496c2c66affSColin Finck 	oldBounds->cElements = psabound->cElements;
1497c2c66affSColin Finck         ulNewSize = SAFEARRAY_GetCellCount(psa) * psa->cbElements;
1498c2c66affSColin Finck 	oldBounds->cElements = oldelems;
1499c2c66affSColin Finck       }
1500c2c66affSColin Finck 
1501c2c66affSColin Finck       if (!(pvNewData = SAFEARRAY_Malloc(ulNewSize)))
1502c2c66affSColin Finck       {
1503c2c66affSColin Finck         SafeArrayUnlock(psa);
1504c2c66affSColin Finck         return E_OUTOFMEMORY;
1505c2c66affSColin Finck       }
1506c2c66affSColin Finck 
1507c2c66affSColin Finck       memcpy(pvNewData, psa->pvData, ulOldSize);
1508c2c66affSColin Finck       SAFEARRAY_Free(psa->pvData);
1509c2c66affSColin Finck       psa->pvData = pvNewData;
1510c2c66affSColin Finck     }
1511c2c66affSColin Finck     oldBounds->cElements = psabound->cElements;
1512c2c66affSColin Finck   }
1513c2c66affSColin Finck 
1514c2c66affSColin Finck   SafeArrayUnlock(psa);
1515c2c66affSColin Finck   return S_OK;
1516c2c66affSColin Finck }
1517c2c66affSColin Finck 
1518c2c66affSColin Finck /************************************************************************
1519c2c66affSColin Finck  *		SafeArrayGetVartype (OLEAUT32.77)
1520c2c66affSColin Finck  *
1521c2c66affSColin Finck  * Get the type of the items in a SafeArray.
1522c2c66affSColin Finck  *
1523c2c66affSColin Finck  * PARAMS
1524c2c66affSColin Finck  *  psa [I] Array to get the type from
1525c2c66affSColin Finck  *  pvt [O] Destination for the type
1526c2c66affSColin Finck  *
1527c2c66affSColin Finck  * RETURNS
1528c2c66affSColin Finck  *  Success: S_OK. pvt contains the type of the items.
1529c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1530c2c66affSColin Finck  *
1531c2c66affSColin Finck  * NOTES
1532c2c66affSColin Finck  * See SafeArray.
1533c2c66affSColin Finck  */
SafeArrayGetVartype(SAFEARRAY * psa,VARTYPE * pvt)1534c2c66affSColin Finck HRESULT WINAPI SafeArrayGetVartype(SAFEARRAY* psa, VARTYPE* pvt)
1535c2c66affSColin Finck {
1536c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, pvt);
1537c2c66affSColin Finck 
1538c2c66affSColin Finck   if (!psa || !pvt)
1539c2c66affSColin Finck     return E_INVALIDARG;
1540c2c66affSColin Finck 
1541c2c66affSColin Finck   if (psa->fFeatures & FADF_RECORD)
1542c2c66affSColin Finck     *pvt = VT_RECORD;
1543c2c66affSColin Finck   else if ((psa->fFeatures & (FADF_HAVEIID|FADF_DISPATCH)) == (FADF_HAVEIID|FADF_DISPATCH))
1544c2c66affSColin Finck     *pvt = VT_DISPATCH;
1545c2c66affSColin Finck   else if (psa->fFeatures & FADF_HAVEIID)
1546c2c66affSColin Finck     *pvt = VT_UNKNOWN;
1547c2c66affSColin Finck   else if (psa->fFeatures & FADF_HAVEVARTYPE)
1548c2c66affSColin Finck   {
1549c2c66affSColin Finck     VARTYPE vt = SAFEARRAY_GetHiddenDWORD(psa);
1550c2c66affSColin Finck     *pvt = vt;
1551c2c66affSColin Finck   }
1552c2c66affSColin Finck   else
1553c2c66affSColin Finck     return E_INVALIDARG;
1554c2c66affSColin Finck 
1555c2c66affSColin Finck   return S_OK;
1556c2c66affSColin Finck }
1557c2c66affSColin Finck 
1558c2c66affSColin Finck /************************************************************************
1559c2c66affSColin Finck  *		SafeArraySetRecordInfo (OLEAUT32.@)
1560c2c66affSColin Finck  *
1561c2c66affSColin Finck  * Set the record info for a SafeArray.
1562c2c66affSColin Finck  *
1563c2c66affSColin Finck  * PARAMS
1564c2c66affSColin Finck  *  psa    [I] Array to set the record info for
1565c2c66affSColin Finck  *  pRinfo [I] Record info
1566c2c66affSColin Finck  *
1567c2c66affSColin Finck  * RETURNS
1568c2c66affSColin Finck  *  Success: S_OK. The record info is stored with the array.
1569c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1570c2c66affSColin Finck  *
1571c2c66affSColin Finck  * NOTES
1572c2c66affSColin Finck  * See SafeArray.
1573c2c66affSColin Finck  */
SafeArraySetRecordInfo(SAFEARRAY * psa,IRecordInfo * pRinfo)1574c2c66affSColin Finck HRESULT WINAPI SafeArraySetRecordInfo(SAFEARRAY *psa, IRecordInfo *pRinfo)
1575c2c66affSColin Finck {
1576c2c66affSColin Finck   IRecordInfo** dest = (IRecordInfo**)psa;
1577c2c66affSColin Finck 
1578c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, pRinfo);
1579c2c66affSColin Finck 
1580c2c66affSColin Finck   if (!psa || !(psa->fFeatures & FADF_RECORD))
1581c2c66affSColin Finck     return E_INVALIDARG;
1582c2c66affSColin Finck 
1583c2c66affSColin Finck   if (pRinfo)
1584c2c66affSColin Finck     IRecordInfo_AddRef(pRinfo);
1585c2c66affSColin Finck 
1586c2c66affSColin Finck   if (dest[-1])
1587c2c66affSColin Finck     IRecordInfo_Release(dest[-1]);
1588c2c66affSColin Finck 
1589c2c66affSColin Finck   dest[-1] = pRinfo;
1590c2c66affSColin Finck   return S_OK;
1591c2c66affSColin Finck }
1592c2c66affSColin Finck 
1593c2c66affSColin Finck /************************************************************************
1594c2c66affSColin Finck  *		SafeArrayGetRecordInfo (OLEAUT32.@)
1595c2c66affSColin Finck  *
1596c2c66affSColin Finck  * Get the record info from a SafeArray.
1597c2c66affSColin Finck  *
1598c2c66affSColin Finck  * PARAMS
1599c2c66affSColin Finck  *  psa    [I] Array to get the record info from
1600c2c66affSColin Finck  *  pRinfo [O] Destination for the record info
1601c2c66affSColin Finck  *
1602c2c66affSColin Finck  * RETURNS
1603c2c66affSColin Finck  *  Success: S_OK. pRinfo contains the record info, or NULL if there was none.
1604c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1605c2c66affSColin Finck  *
1606c2c66affSColin Finck  * NOTES
1607c2c66affSColin Finck  * See SafeArray.
1608c2c66affSColin Finck  */
SafeArrayGetRecordInfo(SAFEARRAY * psa,IRecordInfo ** pRinfo)1609c2c66affSColin Finck HRESULT WINAPI SafeArrayGetRecordInfo(SAFEARRAY *psa, IRecordInfo **pRinfo)
1610c2c66affSColin Finck {
1611c2c66affSColin Finck   IRecordInfo** src = (IRecordInfo**)psa;
1612c2c66affSColin Finck 
1613c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, pRinfo);
1614c2c66affSColin Finck 
1615c2c66affSColin Finck   if (!psa || !pRinfo || !(psa->fFeatures & FADF_RECORD))
1616c2c66affSColin Finck     return E_INVALIDARG;
1617c2c66affSColin Finck 
1618c2c66affSColin Finck   *pRinfo = src[-1];
1619c2c66affSColin Finck 
1620c2c66affSColin Finck   if (*pRinfo)
1621c2c66affSColin Finck     IRecordInfo_AddRef(*pRinfo);
1622c2c66affSColin Finck   return S_OK;
1623c2c66affSColin Finck }
1624c2c66affSColin Finck 
1625c2c66affSColin Finck /************************************************************************
1626c2c66affSColin Finck  *		SafeArraySetIID (OLEAUT32.@)
1627c2c66affSColin Finck  *
1628c2c66affSColin Finck  * Set the IID for a SafeArray.
1629c2c66affSColin Finck  *
1630c2c66affSColin Finck  * PARAMS
1631c2c66affSColin Finck  *  psa  [I] Array to set the IID from
1632c2c66affSColin Finck  *  guid [I] IID
1633c2c66affSColin Finck  *
1634c2c66affSColin Finck  * RETURNS
1635c2c66affSColin Finck  *  Success: S_OK. The IID is stored with the array
1636c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1637c2c66affSColin Finck  *
1638c2c66affSColin Finck  * NOTES
1639c2c66affSColin Finck  * See SafeArray.
1640c2c66affSColin Finck  */
SafeArraySetIID(SAFEARRAY * psa,REFGUID guid)1641c2c66affSColin Finck HRESULT WINAPI SafeArraySetIID(SAFEARRAY *psa, REFGUID guid)
1642c2c66affSColin Finck {
1643c2c66affSColin Finck   GUID* dest = (GUID*)psa;
1644c2c66affSColin Finck 
1645c2c66affSColin Finck   TRACE("(%p,%s)\n", psa, debugstr_guid(guid));
1646c2c66affSColin Finck 
1647c2c66affSColin Finck   if (!psa || !guid || !(psa->fFeatures & FADF_HAVEIID))
1648c2c66affSColin Finck     return E_INVALIDARG;
1649c2c66affSColin Finck 
1650c2c66affSColin Finck   dest[-1] = *guid;
1651c2c66affSColin Finck   return S_OK;
1652c2c66affSColin Finck }
1653c2c66affSColin Finck 
1654c2c66affSColin Finck /************************************************************************
1655c2c66affSColin Finck  *		SafeArrayGetIID (OLEAUT32.@)
1656c2c66affSColin Finck  *
1657c2c66affSColin Finck  * Get the IID from a SafeArray.
1658c2c66affSColin Finck  *
1659c2c66affSColin Finck  * PARAMS
1660c2c66affSColin Finck  *  psa   [I] Array to get the ID from
1661c2c66affSColin Finck  *  pGuid [O] Destination for the IID
1662c2c66affSColin Finck  *
1663c2c66affSColin Finck  * RETURNS
1664c2c66affSColin Finck  *  Success: S_OK. pRinfo contains the IID, or NULL if there was none.
1665c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1666c2c66affSColin Finck  *
1667c2c66affSColin Finck  * NOTES
1668c2c66affSColin Finck  * See SafeArray.
1669c2c66affSColin Finck  */
SafeArrayGetIID(SAFEARRAY * psa,GUID * pGuid)1670c2c66affSColin Finck HRESULT WINAPI SafeArrayGetIID(SAFEARRAY *psa, GUID *pGuid)
1671c2c66affSColin Finck {
1672c2c66affSColin Finck   GUID* src = (GUID*)psa;
1673c2c66affSColin Finck 
1674c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, pGuid);
1675c2c66affSColin Finck 
1676c2c66affSColin Finck   if (!psa || !pGuid || !(psa->fFeatures & FADF_HAVEIID))
1677c2c66affSColin Finck     return E_INVALIDARG;
1678c2c66affSColin Finck 
1679c2c66affSColin Finck   *pGuid = src[-1];
1680c2c66affSColin Finck   return S_OK;
1681c2c66affSColin Finck }
1682c2c66affSColin Finck 
1683c2c66affSColin Finck /************************************************************************
1684c2c66affSColin Finck  *		VectorFromBstr (OLEAUT32.@)
1685c2c66affSColin Finck  *
1686c2c66affSColin Finck  * Create a SafeArray Vector from the bytes of a BSTR.
1687c2c66affSColin Finck  *
1688c2c66affSColin Finck  * PARAMS
1689c2c66affSColin Finck  *  bstr [I] String to get bytes from
1690c2c66affSColin Finck  *  ppsa [O] Destination for the array
1691c2c66affSColin Finck  *
1692c2c66affSColin Finck  * RETURNS
1693c2c66affSColin Finck  *  Success: S_OK. ppsa contains the strings bytes as a VT_UI1 array.
1694c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1695c2c66affSColin Finck  *
1696c2c66affSColin Finck  * NOTES
1697c2c66affSColin Finck  * See SafeArray.
1698c2c66affSColin Finck  */
VectorFromBstr(BSTR bstr,SAFEARRAY ** ppsa)1699c2c66affSColin Finck HRESULT WINAPI VectorFromBstr(BSTR bstr, SAFEARRAY **ppsa)
1700c2c66affSColin Finck {
1701c2c66affSColin Finck   SAFEARRAYBOUND sab;
1702c2c66affSColin Finck 
1703c2c66affSColin Finck   TRACE("(%p,%p)\n", bstr, ppsa);
1704c2c66affSColin Finck 
1705c2c66affSColin Finck   if (!ppsa)
1706c2c66affSColin Finck     return E_INVALIDARG;
1707c2c66affSColin Finck 
1708c2c66affSColin Finck   sab.lLbound = 0;
1709c2c66affSColin Finck   sab.cElements = SysStringByteLen(bstr);
1710c2c66affSColin Finck 
1711c2c66affSColin Finck   *ppsa = SAFEARRAY_Create(VT_UI1, 1, &sab, 0);
1712c2c66affSColin Finck 
1713c2c66affSColin Finck   if (*ppsa)
1714c2c66affSColin Finck   {
1715c2c66affSColin Finck     memcpy((*ppsa)->pvData, bstr, sab.cElements);
1716c2c66affSColin Finck     return S_OK;
1717c2c66affSColin Finck   }
1718c2c66affSColin Finck   return E_OUTOFMEMORY;
1719c2c66affSColin Finck }
1720c2c66affSColin Finck 
1721c2c66affSColin Finck /************************************************************************
1722c2c66affSColin Finck  *		BstrFromVector (OLEAUT32.@)
1723c2c66affSColin Finck  *
1724c2c66affSColin Finck  * Create a BSTR from a SafeArray.
1725c2c66affSColin Finck  *
1726c2c66affSColin Finck  * PARAMS
1727c2c66affSColin Finck  *  psa   [I] Source array
1728c2c66affSColin Finck  *  pbstr [O] Destination for output BSTR
1729c2c66affSColin Finck  *
1730c2c66affSColin Finck  * RETURNS
1731c2c66affSColin Finck  *  Success: S_OK. pbstr contains the arrays data.
1732c2c66affSColin Finck  *  Failure: An HRESULT error code indicating the error.
1733c2c66affSColin Finck  *
1734c2c66affSColin Finck  * NOTES
1735c2c66affSColin Finck  *  psa must be a 1 dimensional array of a 1 byte type.
1736c2c66affSColin Finck  *
1737c2c66affSColin Finck  * NOTES
1738c2c66affSColin Finck  * See SafeArray.
1739c2c66affSColin Finck  */
BstrFromVector(SAFEARRAY * psa,BSTR * pbstr)1740c2c66affSColin Finck HRESULT WINAPI BstrFromVector(SAFEARRAY *psa, BSTR *pbstr)
1741c2c66affSColin Finck {
1742c2c66affSColin Finck   TRACE("(%p,%p)\n", psa, pbstr);
1743c2c66affSColin Finck 
1744c2c66affSColin Finck   if (!pbstr)
1745c2c66affSColin Finck     return E_INVALIDARG;
1746c2c66affSColin Finck 
1747c2c66affSColin Finck   *pbstr = NULL;
1748c2c66affSColin Finck 
1749c2c66affSColin Finck   if (!psa || psa->cbElements != 1 || psa->cDims != 1)
1750c2c66affSColin Finck     return E_INVALIDARG;
1751c2c66affSColin Finck 
1752c2c66affSColin Finck   *pbstr = SysAllocStringByteLen(psa->pvData, psa->rgsabound[0].cElements);
1753c2c66affSColin Finck   if (!*pbstr)
1754c2c66affSColin Finck     return E_OUTOFMEMORY;
1755c2c66affSColin Finck   return S_OK;
1756c2c66affSColin Finck }
1757