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