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