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