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